`

jacob操作word文档(三)

 
阅读更多

有需求要进行页面打印,要打印word文档中的内容,看到jacob可以把word文档转换为html,正好可以在客户端打印该html页面,满足页面打印的需要!

把代码粘出来与大家分享!

Java代码  收藏代码
  1. ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word  
  2.         try {  
  3.             app.setProperty("Visible"new Variant(false));  
  4.         //设置word不可见  
  5.             Dispatch docs = app.getProperty("Documents").toDispatch();  
  6.             Dispatch doc = Dispatch.invoke(docs,"Open",Dispatch.Method,new Object[]  
  7.             { docfile, new Variant(false),new Variant(true) }, new int[1]).toDispatch();  
  8.             // 打开word文件 docfile为word文件的路径  
  9.                        Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {htmlfile,  
  10.             new Variant(8) }, new int[1]);  
  11.             // 作为html格式保存到临时文件 htmlfile为要保存的html文件的路径  
  12.                         Dispatch.call(doc, "Close"new Variant(false));  
  13.         } catch (Exception e) {  
  14.             e.printStackTrace();  
  15.         } finally {  
  16.             app.invoke("Quit"new Variant[]{});//退出  
  17.         }  
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics