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 即合并的
本文作者:韩憨
本文链接:https://www.cnblogs.com/hanby/p/16044273.html
版权声明:本作品采用知识共享署名-非商业性使用-禁止演绎 2.5 中国大陆许可协议进行许可。
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 阿里最新开源QwQ-32B,效果媲美deepseek-r1满血版,部署成本又又又降低了!
· 开源Multi-agent AI智能体框架aevatar.ai,欢迎大家贡献代码
· Manus重磅发布:全球首款通用AI代理技术深度解析与实战指南
· 被坑几百块钱后,我竟然真的恢复了删除的微信聊天记录!
· AI技术革命,工作效率10个最佳AI工具