jacob 将jsp中的数据导出到word,并以表格形式展示 请问,如何将jsp中或者数据库中的内容以表格的形式导出到word中,我目前采用jacob.jar,做为工具包,请问如何实现?谢谢 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 我们项目就用jacob,是以模板形式来实现的JacobDOT.java /** * 根据模板、数据生成word文件 * @param inputPath 模板文件(包含路径) * @param outPath 输出文件(包含路径) * @param data 数据包(包含要填充的字段、对应的数据) */ public void toWord(String inputPath, String outPath, HashMap data) { //XbrlBfConfig xbrlBfConfig=XbrlBfConfig.getInstance(); System.out.println("......word导出进行中......"); String oldText; Object newValue; //存放读取的需要合并单元格的表格 String[] merge = (String[]) data.get("merge"); String[] mergeCell = (String[]) data.get("mergeCell"); //去除 data.remove("merge"); data.remove("mergeCell"); try { doc = open(inputPath); Dispatch selection = select(); Iterator keys = data.keySet().iterator(); //先填充word中的表格 while (keys.hasNext()) { oldText = (String) keys.next(); newValue = data.get(oldText); if (oldText.startsWith("$table$") || newValue instanceof List) { replaceTable(selection, oldText, (List) newValue); } } //处理表格中合并单元格 //if(xbrlBfConfig.getCompanyCode().equals("hx")) { doMerge(data,merge); mergeTableCell(data, mergeCell); //} // 删除word里面的空table if (delTBCount != null) { Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); // Comparator c=new Comparator(); // Collections.sort(delTBCount); Collections.sort(delTBCount, new java.util.Comparator() { public int compare(Object object1, Object object2) { Integer obj1 = new Integer((String) object1); Integer obj2 = new Integer((String) object2); return obj1.compareTo(obj2); } }); int del = 0; for (int i = 0; i < delTBCount.size(); i++) {// if (hasReplace != null) {// if (hasReplace.contains((String) delTBCount.get(i))) {// //表格有替换过,不删除// // continue;// }// } int tbIndex = Integer.parseInt((String) delTBCount.get(i)); Dispatch table = Dispatch.call(tables, "Item", new Variant(tbIndex - del)).toDispatch(); Dispatch.call(table, "Delete"); del++; //删除空表格的单位 replaceText(selection, "$金额单位:人民币元@" + tbIndex + "$", ""); replaceText(selection, "$单位:人民币元@" + tbIndex + "$", ""); replaceText(selection, "$单位:份@" + tbIndex + "$", ""); replaceText(selection, "$份额单位:份@" + tbIndex + "$", ""); } } if (hasReplace != null) { for (int i = 0; i < hasReplace.size(); i++) { int tbIndex = Integer.parseInt((String) hasReplace.get(i)); replaceText(selection, "$金额单位:人民币元@" + tbIndex + "$", "金额单位:人民币元"); replaceText(selection, "$单位:人民币元@" + tbIndex + "$", "单位:人民币元"); replaceText(selection, "$单位:份@" + tbIndex + "$", "单位:份"); replaceText(selection, "$份额单位:份@" + tbIndex + "$", "份额单位:份"); } } System.out.println("......表格处理完毕......"); delTBCount = null; System.out.println("...... 字符处理中 ......"); //彻底处理完word中的表格之后替换其他的变量 keys = data.keySet().iterator(); while (keys.hasNext()) { oldText = (String) keys.next(); newValue = data.get(oldText); replaceAll(selection, oldText, newValue); } replaceText(selection, "$mergeTable$", ""); //进行第二次替换,在第一次替换中,可能会出现“$mergeTable$$mergeTable$”,需要替换第二次才能够彻底替换完毕 replaceText(selection, "$mergeTable$", ""); System.out.println("......字符处理完毕......"); System.out.println("||||||Word導出成功||||||"); // Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); // // // // if(tableIndex!=null) { // String tbIndex[] = tableIndex.split("-"); // System.out.println(tbIndex.length+"=================="); // for (int i = 0; i < tbIndex.length; i++) { // Dispatch.call(Dispatch.call(tables, "Item", new Variant(new // Integer(tbIndex[i]).intValue() -i)).toDispatch(), "Delete"); // } // } // 取得活动窗体对象 Dispatch ActiveWindow = word.getProperty("ActiveWindow").toDispatch(); // 取得活动窗格对象 Dispatch ActivePane = Dispatch.get(ActiveWindow, "ActivePane").toDispatch(); // 取得视窗对象 Dispatch View = Dispatch.get(ActivePane, "View").toDispatch(); XbrlBfConfig xbrlBfConfig=XbrlBfConfig.getInstance(); if ("yes".equals(xbrlBfConfig.getFirstPageNotNeedHeader())) { //首页无页眉; Dispatch.put(View, "SeekView", "10");//光标标至首页页脚处 Dispatch.call(selection, "MoveDown");//光标下移一个回车位(此时光标标至第二页页眉处) Dispatch.call(selection, "MoveDown");//光标下移一个回车位 Dispatch.put(selection, "Text", pageHead); }else { //页眉从首页开始 Dispatch.put(View, "SeekView", "9"); Dispatch.call(selection, "MoveDown"); Dispatch.put(selection, "Text", pageHead); } save(outPath); } catch (Exception e) { e.printStackTrace(); // debug.println("toword[Java2Word]------------操作word文件失败!"+e.getMessage(),true); } finally { if (doc != null) { JacobDOT.pageHead = null; close(doc); } }}public static void main(String[] args) { JacobDOT test = new JacobDOT(); HashMap data = new HashMap(); ArrayList table = new ArrayList(); table.add(new String[] {"1", "2", "3", "4", "5"}); table.add(new String[] {"current1", "11", "22", "33", "44"}); table.add(new String[] {"current1", "21", "22", "23", "24"}); data.put("table$4@" + 1, table); test.toWord("d:/Temp/test.dot", "d:/Temp/dot.doc", data);} 你能把test.dot发给我看看吗?万分感谢!! 请问,如何用jacob工具包在word中字段名?谢谢 jacobdot是按照需求自己做吧 是啊,我想结合他的dot看看这个例子!! java调用存储过程问题。。。。。。。。。。。。。。。。。。。。。。。 有想转Android的朋友吗 java开发经验1-2年 要做一个js解析xml的小程序,哪位大侠给点建议啊 Spring问题,急! 问个小问题 小妹学编程,在线求助。。。 实际项目中访问XML,java中用那个API Xerces Or JavaXML API啊! 用JBOSS做服务器,安全性或稳定性有问题吗? 新人关于循环的问题 java cache技术 请教:spring mvc 框架原理
JacobDOT.java
/**
* 根据模板、数据生成word文件
* @param inputPath 模板文件(包含路径)
* @param outPath 输出文件(包含路径)
* @param data 数据包(包含要填充的字段、对应的数据)
*/
public void toWord(String inputPath, String outPath, HashMap data) {
//XbrlBfConfig xbrlBfConfig=XbrlBfConfig.getInstance();
System.out.println("......word导出进行中......");
String oldText;
Object newValue;
//存放读取的需要合并单元格的表格
String[] merge = (String[]) data.get("merge");
String[] mergeCell = (String[]) data.get("mergeCell");
//去除
data.remove("merge");
data.remove("mergeCell");
try {
doc = open(inputPath);
Dispatch selection = select();
Iterator keys = data.keySet().iterator();
//先填充word中的表格
while (keys.hasNext()) {
oldText = (String) keys.next();
newValue = data.get(oldText);
if (oldText.startsWith("$table$") || newValue instanceof List) {
replaceTable(selection, oldText, (List) newValue);
}
}
//处理表格中合并单元格
//if(xbrlBfConfig.getCompanyCode().equals("hx")) {
doMerge(data,merge);
mergeTableCell(data, mergeCell);
//}
// 删除word里面的空table
if (delTBCount != null) {
Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
// Comparator c=new Comparator();
// Collections.sort(delTBCount);
Collections.sort(delTBCount, new java.util.Comparator() {
public int compare(Object object1, Object object2) {
Integer obj1 = new Integer((String) object1);
Integer obj2 = new Integer((String) object2);
return obj1.compareTo(obj2);
}
});
int del = 0;
for (int i = 0; i < delTBCount.size(); i++) {
// if (hasReplace != null) {
// if (hasReplace.contains((String) delTBCount.get(i))) {
// //表格有替换过,不删除
//
// continue;
// }
// }
int tbIndex = Integer.parseInt((String) delTBCount.get(i));
Dispatch table = Dispatch.call(tables, "Item", new Variant(tbIndex - del)).toDispatch();
Dispatch.call(table, "Delete");
del++;
//删除空表格的单位
replaceText(selection, "$金额单位:人民币元@" + tbIndex + "$", "");
replaceText(selection, "$单位:人民币元@" + tbIndex + "$", "");
replaceText(selection, "$单位:份@" + tbIndex + "$", "");
replaceText(selection, "$份额单位:份@" + tbIndex + "$", "");
}
}
if (hasReplace != null) {
for (int i = 0; i < hasReplace.size(); i++) {
int tbIndex = Integer.parseInt((String) hasReplace.get(i));
replaceText(selection, "$金额单位:人民币元@" + tbIndex + "$", "金额单位:人民币元");
replaceText(selection, "$单位:人民币元@" + tbIndex + "$", "单位:人民币元");
replaceText(selection, "$单位:份@" + tbIndex + "$", "单位:份");
replaceText(selection, "$份额单位:份@" + tbIndex + "$", "份额单位:份");
}
}
System.out.println("......表格处理完毕......");
delTBCount = null;
System.out.println("...... 字符处理中 ......");
//彻底处理完word中的表格之后替换其他的变量
keys = data.keySet().iterator();
while (keys.hasNext()) {
oldText = (String) keys.next();
newValue = data.get(oldText);
replaceAll(selection, oldText, newValue);
}
replaceText(selection, "$mergeTable$", "");
//进行第二次替换,在第一次替换中,可能会出现“$mergeTable$$mergeTable$”,需要替换第二次才能够彻底替换完毕
replaceText(selection, "$mergeTable$", "");
System.out.println("......字符处理完毕......");
System.out.println("||||||Word導出成功||||||");
// Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
//
//
//
// if(tableIndex!=null) {
// String tbIndex[] = tableIndex.split("-");
// System.out.println(tbIndex.length+"==================");
// for (int i = 0; i < tbIndex.length; i++) {
// Dispatch.call(Dispatch.call(tables, "Item", new Variant(new
// Integer(tbIndex[i]).intValue() -i)).toDispatch(), "Delete");
// }
// } // 取得活动窗体对象
Dispatch ActiveWindow = word.getProperty("ActiveWindow").toDispatch();
// 取得活动窗格对象
Dispatch ActivePane = Dispatch.get(ActiveWindow, "ActivePane").toDispatch();
// 取得视窗对象
Dispatch View = Dispatch.get(ActivePane, "View").toDispatch();
XbrlBfConfig xbrlBfConfig=XbrlBfConfig.getInstance();
if ("yes".equals(xbrlBfConfig.getFirstPageNotNeedHeader())) {
//首页无页眉;
Dispatch.put(View, "SeekView", "10");//光标标至首页页脚处
Dispatch.call(selection, "MoveDown");//光标下移一个回车位(此时光标标至第二页页眉处)
Dispatch.call(selection, "MoveDown");//光标下移一个回车位
Dispatch.put(selection, "Text", pageHead);
}else {
//页眉从首页开始
Dispatch.put(View, "SeekView", "9");
Dispatch.call(selection, "MoveDown");
Dispatch.put(selection, "Text", pageHead);
}
save(outPath);
} catch (Exception e) {
e.printStackTrace();
// debug.println("toword[Java2Word]------------操作word文件失败!"+e.getMessage(),true);
} finally {
if (doc != null) {
JacobDOT.pageHead = null;
close(doc);
}
}
}
public static void main(String[] args) {
JacobDOT test = new JacobDOT();
HashMap data = new HashMap();
ArrayList table = new ArrayList();
table.add(new String[] {"1", "2", "3", "4", "5"});
table.add(new String[] {"current1", "11", "22", "33", "44"});
table.add(new String[] {"current1", "21", "22", "23", "24"});
data.put("table$4@" + 1, table);
test.toWord("d:/Temp/test.dot", "d:/Temp/dot.doc", data);}
dot是按照需求自己做吧