JAVA 使用Jacob合并word文档

注意:这个只能在windos下使用,linux不支持

JACOB-JavaCOMBridge标准的操作word、excel工具包

下载jacob-1.18-x64.dll

64位windos对应的jacob-1.18-x64.dll到java目录的bin下面

 

 

 

class类中引入
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

如果有easypoi的Variant,先注掉,不然会报错。

 

引入合并方法

public static void uniteDoc(List fileList, String savepaths) {
if (fileList.size() == 0 || fileList == null) {
return;
}
//打开word
ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
try {
// 设置word不可见
app.setProperty("Visible", new Variant(false));
//获得documents对象
Object docs = app.getProperty("Documents").toDispatch();
//打开第一个文件
Dispatch doc = Dispatch.invoke(
(Dispatch) docs,
"Open",
Dispatch.Method,
new Object[]{(String) fileList.get(0),
new Variant(false), new Variant(true)},
new int[3]).toDispatch();
//追加文件
for (int i = 1; i < fileList.size(); i++) {
// Dispatch wordContent = Dispatch.get(doc, "Content").toDispatch(); // 取得word文件的内容
// Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落
// int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数
// Dispatch lastParagraph = Dispatch.call(paragraphs, "Item",new Variant(paragraphCount)).toDispatch(); // 最后一段
Dispatch.call(app.getProperty("Selection").toDispatch(), "HomeKey", new Variant(6));
Dispatch.invoke(app.getProperty("Selection").toDispatch(),
"insertFile", Dispatch.Method, new Object[]{
(String) fileList.get(i), "",
new Variant(false), new Variant(false),
new Variant(false)}, new int[3]);
}
//保存新的word文件
System.out.println(savepaths);
//FileUtil.buildDir(savepaths);
Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method,
new Object[]{savepaths, new Variant(1)}, new int[3]);
Variant f = new Variant(false);
Dispatch.call((Dispatch) doc, "Close", f);
} catch (Exception e) {
throw new RuntimeException("合并word文件出错.原因:" + e);
} finally {
app.invoke("Quit", new Variant[]{});
}
}

 

测试方法

@RequestMapping(value = "/bicthh", method = RequestMethod.GET)
public void exportBicthh(HttpServletRequest request, HttpServletResponse response) throws Exception {
List<Map<String, Object>> list1 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Map<String, Object> map = new HashMap<>();
map.put("name", "我是小明" + i);
list1.add(map);
}
List<Map<String, Object>> list2 = new ArrayList<>();
for (int i = 0; i < 5; i++) {
Map<String, Object> map = new HashMap<>();
map.put("name", "我是小韩" + i);
list2.add(map);
}
//----------------------------------------------
try {
XWPFDocument doc1 = WordExportUtil
.exportWord07("C:\\Users\\Administrator\\Desktop\\测试模板表.docx", list1);
XWPFDocument doc2 = WordExportUtil
.exportWord07("C:\\Users\\Administrator\\Desktop\\测试模板表.docx", list2);
FileOutputStream fos1 = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\测试结果表1.docx");
FileOutputStream fos2 = new FileOutputStream("C:\\Users\\Administrator\\Desktop\\测试结果表2.docx");
doc1.write(fos1);
doc2.write(fos2);
fos1.close();
fos2.close();
} catch (Exception e) {
e.printStackTrace();
}

List<String> list = new ArrayList<>();
list.add("C:\\Users\\Administrator\\Desktop\\测试结果表1.docx");
list.add("C:\\Users\\Administrator\\Desktop\\测试结果表2.docx");
uniteDoc(list, "C:\\Users\\Administrator\\Desktop\\测试结果表3.docx");
}

模板

 

 

结果1

 

 

结果2

 

 结果3 即合并的

 

posted @ 2022-03-23 14:28  韩憨  阅读(636)  评论(0编辑  收藏  举报
//看板娘