posts - 41, comments - 15, trackbacks - 0, articles - 1
            BlogJava :: 首頁(yè) :: 新隨筆 :: 聯(lián)系 :: 聚合  :: 管理

          日歷

          <2013年3月>
          242526272812
          3456789
          10111213141516
          17181920212223
          24252627282930
          31123456

          文章檔案

          搜索

          •  

          將幾個(gè)word文件合并到一個(gè)word文件,需要下載jacob-1.17-M2.zip,將壓縮文件內(nèi)的jacob-1.17-M2-x64.dll放到windows/system32下,將jacob.jar放到項(xiàng)目中
          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");//啟動(dòng)word
            try {
             // 設(shè)置word不可見
             app.setProperty("Visible", new Variant(false));
             //獲得documents對(duì)象
             Object docs = app.getProperty("Documents").toDispatch();
             //打開第一個(gè)文件
             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文件出錯(cuò).原因:" + e);
            } finally {
             app.invoke("Quit", new Variant[] {});
            }
           }



          只有注冊(cè)用戶登錄后才能發(fā)表評(píng)論。


          網(wǎng)站導(dǎo)航:
           
          主站蜘蛛池模板: 措勤县| 东丽区| 宁德市| 邵武市| 弥渡县| 大安市| 江北区| 阿巴嘎旗| 汝南县| 新竹县| 锦州市| 太白县| 阿荣旗| 辽宁省| 怀来县| 榆树市| 新河县| 顺义区| 马边| 龙里县| 日照市| 图们市| 兴和县| 景泰县| 南丹县| 武冈市| 隆尧县| 牟定县| 平邑县| 麟游县| 淳安县| 米易县| 肥西县| 亚东县| 通辽市| 盘锦市| 修武县| 米易县| 吉首市| 基隆市| 右玉县|