打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
jacob
  1. package mqdms.com.other;    
  2. import java.io.File;   
  3. import com.jacob.activeX.ActiveXComponent;   
  4. import com.jacob.com.ComThread;   
  5. import com.jacob.com.Dispatch;   
  6. import com.jacob.com.Variant;   
  7. /*  
  8.  * 注意word转pdf要安装虚拟打印机,且要配置  
  9.  * 使用jacob框架,把dll文件放到jre/bin目录下  
  10.  */  
  11. public class Test {   
  12. private  ActiveXComponent wordCom = null;     
  13. private  Object wordDoc = null;     
  14. private  final Variant False = new Variant(false);     
  15. private  final Variant True = new Variant(true);     
  16.   
  17. public static final int EXCEL_HTML = 44;    
  18. public static final int WORD_HTML = 8;    
  19.   
  20.   
  21. /**    
  22.       * 打开word文档   
  23.       *     
  24.       *   
  25. @param filePath word文档   
  26.       *   
  27.  
  28. @return 返回word文档对象    
  29. */     
  30.     
  31. public  boolean openWord(String filePath) {     
  32.  //建立ActiveX部件    
  33.   
  34. wordCom = new ActiveXComponent("Word.Application");     
  35. try {     
  36. //返回wrdCom.Documents的Dispatch    
  37. Dispatch wrdDocs = wordCom.getProperty("Documents").toDispatch();     
  38. //调用wrdCom.Documents.Open方法打开指定的word文档,返回wordDoc    
  39. wordDoc = Dispatch.invoke(wrdDocs, "Open", Dispatch.Method,     
  40. new Object[] { filePath }, new int[1]).toDispatch();     
  41. return true;     
  42.   }    
  43. catch (Exception ex) {     
  44.             ex.printStackTrace();    
  45.         }    
  46. return false;    
  47.     }    
  48. /**    
  49.        * 关闭word文档   
  50. */     
  51. public  void closeWord(boolean saveOnExit) {    
  52. if (wordCom!=null) {    
  53. //关闭word文件    
  54. //Dispatch.call(wordDoc, "Close", new Variant(saveOnExit));   
  55. wordCom.invoke("Quit",new Variant[]{});    
  56. //wordCom.invoke("Quit",new Variant[0]);   
  57. wordCom=null;    
  58. //释放在程序线程中引用的其它com,比如Adobe PDFDistiller   
  59.             ComThread.Release();    
  60.         }   
  61.     }    
  62. /**    
  63.       * 将word文档打印为PS文件后,使用Distiller将PS文件转换为PDF文件  
  64.       *     
  65. @param   sourceFilePath    
  66.       *          源文件路径  
  67.       *   
  68. @param   destinPSFilePath    
  69.       *          首先生成的PS文件路径  
  70.       *   
  71. @param   destinPDFFilePath    
  72.       *          生成PDF文件路径   
  73. */     
  74. public  void docToPDF(String sourceFilePath, String destinPSFilePath,     
  75.                     String destinPDFFilePath) {    
  76. if (!openWord(sourceFilePath)) {     
  77.       closeWord(true);     
  78.       return;     
  79.    }    
  80. //建立Adobe Distiller的com对象    
  81. ActiveXComponent distiller = new ActiveXComponent("PDFDistiller.PDFDistiller.1");    
  82. try {     
  83. //设置当前使用的打印机,我的Adobe Distiller打印机名字为 "Adobe PDF"    
  84. wordCom.setProperty("ActivePrinter"new Variant("Adobe PDF"));    
  85. //设置printout的参数,将word文档打印为postscript文档。现在只使用了前5个参数,假如要使用更多的话可以参考MSDN的office开发相关api     
  86. //是否在后台运行    
  87.   Variant Background = False;     
  88. //是否追加打印    
  89.   Variant Append = False;     
  90. //打印所有文档    
  91.   int wdPrintAllDocument = 0;     
  92.   Variant Range = new Variant(wdPrintAllDocument);     
  93. //输出的postscript文件的路径    
  94.   Variant OutputFileName = new Variant(destinPSFilePath);     
  95.   Dispatch.callN((Dispatch) wordDoc,    
  96. "PrintOut"new Variant[] {     
  97.           Background, Append, Range, OutputFileName });    
  98.          System.out.println("由word文档打印为ps文档成功!");     
  99. //调用Distiller对象的FileToPDF方法所用的参数,具体内容参考Distiller Api手册     
  100.  //作为输入的ps文档路径    
  101.  Variant inputPostScriptFilePath = new Variant(destinPSFilePath);     
  102.  //作为输出的pdf文档的路径    
  103.  Variant outputPDFFilePath = new Variant(destinPDFFilePath);     
  104.  //定义FileToPDF方法要使用adobe pdf设置文件的路径,在这里没有赋值表示并不使用pdf配置文件    
  105. Variant PDFOption = new Variant("");     
  106.  //调用FileToPDF方法将ps文档转换为pdf文档    
  107. Dispatch.callN(distiller, "FileToPDF"new Variant[] {inputPostScriptFilePath, outputPDFFilePath, PDFOption });    
  108.  System.out.println("由ps文档转换为pdf文档成功!");     
  109.  }    
  110. catch (Exception ex) {     
  111.              ex.printStackTrace();    
  112.         }    
  113. finally {     
  114. closeWord(true);     
  115.         }    
  116.     }      
  117.   
  118.   
  119. public static void excelToHtml(String xlsfile, String htmlfile)    
  120. {    
  121. ActiveXComponent app = new ActiveXComponent("Excel.Application"); // 启动word    
  122. try    
  123. {    
  124. app.setProperty("Visible"new Variant(false));    
  125. Dispatch excels = app.getProperty("Workbooks").toDispatch();    
  126. Dispatch excel = Dispatch.invoke(    
  127. excels,    
  128. "Open",    
  129. Dispatch.Method,    
  130. new Object[] { xlsfile, new Variant(false),    
  131. new Variant(true) }, new int[1]).toDispatch();    
  132. Dispatch.invoke(excel, "SaveAs", Dispatch.Method, new Object[] {    
  133. htmlfile, new Variant(EXCEL_HTML) }, new int[1]);    
  134. Variant f = new Variant(false);    
  135. Dispatch.call(excel, "Close", f);    
  136. }    
  137. catch (Exception e)    
  138. {    
  139. e.printStackTrace();    
  140. }    
  141. finally    
  142. {    
  143. app.invoke("Quit"new Variant[] {});    
  144. }    
  145. }    
  146.   
  147.   
  148. public static void wordToHtml(String docfile, String htmlfile)    
  149. {    
  150. ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word    
  151. try    
  152. {    
  153. app.setProperty("Visible"new Variant(false));    
  154. Dispatch docs = app.getProperty("Documents").toDispatch();    
  155. Dispatch doc = Dispatch.invoke(    
  156. docs,    
  157. "Open",    
  158. Dispatch.Method,    
  159. new Object[] { docfile, new Variant(false),    
  160. new Variant(true) }, new int[1]).toDispatch();    
  161. Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] {    
  162. htmlfile, new Variant(WORD_HTML) }, new int[1]);    
  163. Variant f = new Variant(false);    
  164. Dispatch.call(doc, "Close", f);    
  165. }    
  166. catch (Exception e)    
  167. {    
  168. e.printStackTrace();    
  169. }    
  170. finally    
  171. {    
  172. app.invoke("Quit"new Variant[] {});    
  173. }    
  174. }    
  175.   
  176.   
  177.  public static void main(String[] argv) {     
  178.    // Test d2p = new Test();   
  179. //d2p.docToPDF("D:\\员工假期申请单(员工).doc", "D:\\员工假期申请单(员工).ps", "D:\\员工假期申请单.pdf");   
  180.    //excelToHtml("D:\\通讯录2011.02.21.xls","D:\\通讯录.html");   
  181.  // wordToHtml("F:\\资料\\员工登记表.doc", "F:\\资料\\员工登记表.html");   
  182. //boolean   success   =   (new   File("D:\\test.ps")).delete();   
  183. //if(success){   
  184. //    System.out.println("删除打印机文件成功");   
  185. //            }   
  186.     }    
  187. }  
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
【热】打开小程序,算一算2024你的财运
java jacob调用打印,word,excel横向打印
Excel转换成html
请问怎么实现使用jacob拷贝一个文档的部分内容(不是段落)到另一个文档中保存?
java将Word/Excel/PDF文件转换成HTML整理
用java将Word文档转Html
用Java巧把Word转换成Html文件
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服