• 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 即合并的

  • 相关阅读:
    目标检测网络CenterNet详解(四)
    样本不均衡问题
    目标检测网络Faster RCNN详解(一)
    SpringCloud学习总结(八)——服务调用Feign
    OpenFeign(2020-10-13)
    Feign真正正确的使用方法
    微服务实战SpringCloud之Feign简介及使用
    spring cloud gateway网关和负载均衡框架ribbon实战
    Studio 3T 破解
    JVM 垃圾回收?全面详细安排!
  • 原文地址:https://www.cnblogs.com/hanby/p/16044273.html
Copyright © 2020-2023  润新知