java jacob 多个word合并 页眉页脚怎么生成???

java jacob 多个word合并 页眉页脚怎么生成???、
生成出来的 页脚都不对...

clipboard.png

clipboard.png

clipboard.png

数量不对 还有重复的 萌新求解答....

String dest=filePath.substring(0,filePath.indexOf("."))+"-副本.doc";

FileChannel inputChannel = null;
FileChannel outputChannel = null;
try {
  inputChannel = new FileInputStream(filePath).getChannel();
  outputChannel = new FileOutputStream(dest).getChannel();
    outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
}catch (IOException e) {
  e.printStackTrace();
}
finally {
  try {
    outputChannel.close();
    inputChannel.close();
  } catch (IOException e) {
    e.printStackTrace();
  }

}
boolean flag=true;
//打开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) dest,
                  new Variant(false), new Variant(true) },
          new int[3]).toDispatch();

 



  //获得当前文档选定部分或光标所在位置
  Dispatch selection = app.getProperty("Selection").toDispatch();
  //光标定位到文档首位置
  Dispatch.call(selection, "HomeKey", new Variant(6));
  //在光标所在位置插入文本
  //光标定位到文档末尾位置
  Dispatch.call(selection, "EndKey", new Variant(6));
  Dispatch.call(selection, "TypeParagraph");
  //插入一个分页符
  Dispatch.call(selection, "InsertBreak");
  int i = 0;

  //追加文件
  for (TestingInstanceDetailAttach attach:attachList){
    i++;
    String path=attach.getUpload().getPath();
    if(StringUtils.isNotBlank(path)){
      File file = new File(path);

      if(file.exists()&&path.endsWith("doc")){
        //每一篇文档前添加报送类型、单位和标题

        //文档内容
        Dispatch.invoke(selection,
                "insertFile", Dispatch.Method, new Object[] {
                        (String)path, "",
                        new Variant(false), new Variant(false),
                        new Variant(false) }, new int[3]);
        //每一篇文档后添加分页符
        Dispatch.call(selection, "EndKey", new Variant(6));
        Dispatch.call(selection, "TypeParagraph");

      }
    }
  }



  //保存新的word文件
  File folder = new File(filePath.substring(0, filePath.lastIndexOf("/")+1));
  if(!folder.exists() && !folder.isDirectory()){
    folder.mkdirs();
  }
  Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method,
          new Object[] { filePath, new Variant(1) }, new int[3]);
  Variant f = new Variant(false);
  Dispatch.call((Dispatch) doc, "Close", f);
} catch (Exception e) {
  flag=false;
  throw new RuntimeException("合并word文件出错.原因:" + e);

} finally {
  app.invoke("Quit", new Variant[] {});
  if (!dest.endsWith(File.separator))
    dest = dest + File.separator;
  File dirFile = new File(dest);
  if (!dirFile.exists()) {
    System.out.println("删除目录失败:" + dest + "不存在!");
    return false;
  }else{
    dirFile.delete();
  }

}
return flag;
阅读 2.4k
撰写回答
你尚未登录,登录后可以
  • 和开发者交流问题的细节
  • 关注并接收问题和回答的更新提醒
  • 参与内容的编辑和改进,让解决方法与时俱进
推荐问题