jacob合并几个word文件到一个word文件

Posted on

jacob合并几个word文件到一个word文件

 因项目需要将几个word文件合并到一个word文件,后面附项目运用的jar包jacob-1.9

jacob运用中,需要将附件内的jacob.dll放到windows/system32下

 直接上代码:

Java代码 收藏代码

  1. public static void main(String[] args) {
  2. List list = new ArrayList();
  3. String file1= "D:\file1.doc";
  4. String file2= "D:\file2.doc";
  5. String file3= "D:\file3.doc";
  6. list.add(file1);
  7. list.add(file2);
  8. list.add(file3);
  9. uniteDoc(list,"d:\file.doc");
  10. }
  11. public static void uniteDoc(List fileList, String savepaths) {
  12. if (fileList.size() == 0 || fileList == null) {
  13. return;
  14. }
  15. //打开word
  16. ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word
  17. try {
  18. // 设置word不可见
  19. app.setProperty("Visible", new Variant(false));
  20. //获得documents对象
  21. Object docs = app.getProperty("Documents").toDispatch();
  22. //打开第一个文件
  23. Object doc = Dispatch
  24. .invoke(
  25. (Dispatch) docs,
  26. "Open",
  27. Dispatch.Method,
  28. new Object[] { (String) fileList.get(0),
  29. new Variant(false), new Variant(true) },
  30. new int[3]).toDispatch();
  31. //追加文件
  32. for (int i = 1; i < fileList.size(); i++) {
  33. Dispatch.invoke(app.getProperty("Selection").toDispatch(),
  34. "insertFile", Dispatch.Method, new Object[] {
  35. (String) fileList.get(i), "",
  36. new Variant(false), new Variant(false),
  37. new Variant(false) }, new int[3]);
  38. }
  39. //保存新的word文件
  40. Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method,
  41. new Object[] { savepaths, new Variant(1) }, new int[3]);
  42. Variant f = new Variant(false);
  43. Dispatch.call((Dispatch) doc, "Close", f);
  44. } catch (Exception e) {
  45. throw new RuntimeException("合并word文件出错.原因:" + e);
  46. } finally {
  47. app.invoke("Quit", new Variant[] {});
  48. }
  49. }

public static void main(String[] args) {

        List list  = new ArrayList();
        String file1= "D:\\file1.doc";

        String file2= "D:\\file2.doc";
        String file3= "D:\\file3.doc";

        list.add(file1);
        list.add(file2);

        list.add(file3);
        uniteDoc(list,"d:\\file.doc");

}
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();

        //打开第一个文件
        Object 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.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文件

        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[] {});

    }
}
希望本站内容对您有点用处,有什么疑问或建议请在后面留言评论
转载请注明作者(RobinChia)和出处 It so life ,请勿用于任何商业用途