您的位置:首页 > 其它

jacob合并多个word文件和插入分页符

2013-05-22 12:31 555 查看
//批量多个word文件合并成一个word文件

  public void uniteDoc(ArrayList fileList, String savepaths,ArrayList filenameList) {
   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) filenameList.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) filenameList.get(i), "",
                    new Variant(false), new Variant(false),
                    new Variant(false) }, new int[3]);
           Dispatch selection = Dispatch.get(app, "Selection").toDispatch(); 

   //插入分页符

           Dispatch.call(selection,  "InsertBreak" ,  new Variant(7) );
       }
       //保存新的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[] {});
   }
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: