《Jacob操作office文档.docx》由会员分享,可在线阅读,更多相关《Jacob操作office文档.docx(16页珍藏版)》请在三一办公上搜索。
1、Jacob操作office文档Jacob操作office文档 jacob 操作 word 1. 2. 3. 4. 5. 6. 7. 8. 9. public boolean doc2pdf(String srcFilePath, String pdfFilePath) ActiveXComponent app = null; Dispatch doc = null; try ComThread.InitSTA; app = new ActiveXComponent(Word.Application); app.setProperty(Visible, false); Dispatch docs
2、 = app.getProperty(Documents).toDispatch; doc = Dispatch.invoke(docs, Open, Dispatch.Method, 10. new Object srcFilePath, 11. new Variant(false), 12. new Variant(true),/是否只读 13. new Variant(false), 14. new Variant(pwd) , 15. new int1).toDispatch; 16. / Dispatch.put(doc, Compatibility, false); /兼容性检查,
3、为特定值false不正确 17. Dispatch.put(doc, RemovePersonalInformation, false); 18. Dispatch.call(doc, ExportAsFixedFormat, pdfFilePath, wdFormatPDF); / word保存为pdf格式宏,值为17 19. 20. return true; / set flag true; 21. catch (ComFailException e) 22. return false; 23. catch (Exception e) 24. return false; 25. final
4、ly 26. if (doc != null) 27. Dispatch.call(doc, Close, false); 28. 29. if (app != null) 30. app.invoke(Quit, 0); 31. 32. ComThread.Release; 33. 34. Jacob操作ppt 1. public boolean ppt2pdf(String srcFilePath, String pdfFilePath) 2. 3. 4. 5. 6. 7. 8. 9. ActiveXComponent app = null; Dispatch ppt = null; tr
5、y ComThread.InitSTA; app = new ActiveXComponent(PowerPoint.Application); Dispatch ppts = app.getProperty(Presentations).toDispatch; / 因POWER.EXE的发布规则为同步,所以设置为同步发布 10. ppt = Dispatch.call(ppts, Open, srcFilePath, true,/ ReadOnly 11. true,/ Untitled指定文件是否有标题 12. false/ WithWindow指定文件是否可见 13. ).toDispa
6、tch; 14. 15. Dispatch.call(ppt, SaveAs, pdfFilePath, ppSaveAsPDF); /ppSaveAsPDF为特定值32 16. 17. return true; / set flag true; 18. catch (ComFailException e) 19. return false; 20. catch (Exception e) 21. return false; 22. finally 23. if (ppt != null) 24. Dispatch.call(ppt, Close); 25. 26. if (app != nu
7、ll) 27. app.invoke(Quit); 28. 29. ComThread.Release; 30. 31. Jacob操作Excel 1. 2. 3. 4. 5. 6. 7. 8. 9. package com; import java.io.ObjectInputStream.GetField; import java.util.ArrayList; import java.util.Date; import java.util.List; import com.jacob.activeX.ActiveXComponent; import .ComThread; import
8、.Dispatch; 10. import .Variant; 11. 12. 13. public class ready 14. private static ActiveXComponent xl = null; /Excel对象(防止打开多个) 15. private static Dispatch workbooks = null; /工作簿对象 16. private Dispatch workbook = null; /具体工作簿 17. private Dispatch sheets = null;/ 获得sheets集合对象 18. private Dispatch curr
9、entSheet = null;/ 当前sheet 19. /* 20. * 打开excel文件 21. * param filepath 文件路径名称 22. * param visible 是否显示打开 23. * param readonly 是否只读方式打开 24. */ 25. private void OpenExcel(String filepath, boolean visible) 26. try 27. initComponents; /清空原始变量 28. ComThread.InitSTA; 29. if(xl=null) 30. xl = new ActiveXCom
10、ponent(Excel.Application); /Excel对象 31. xl.setProperty(Visible, new Variant(visible);/设置是否显示打开excel 32. if(workbooks=null) 33. workbooks = xl.getProperty(Workbooks).toDispatch; /打开具体工作簿 34. workbook = Dispatch.invoke(workbooks, Open, Dispatch.Method, 35. new Object srcFilePath, 36. new Variant(false
11、), / 是否以只读方式打开 37. new Variant(true), 38. 1, 39. pwd , /输入密码pwd,若有密码则进行匹配,无则直接打开 40. new int1).toDispatch; 41. catch (Exception e) 42. e.printStackTrace; 43. releaseSource; 44. 45. 46. /* 47. * 工作簿另存为 48. * param filePath 另存为的路径 49. * 例如 SaveAs=D:TEST/c.xlsx 50. */ 51. private void SaveAs(String fil
12、ePath) 52. Dispatch.call(workbook, SaveAs,filePath); 53. 54. /* 55. * 关闭excel文档 56. * param f 含义不明 57. */ 58. private void CloseExcel(boolean f) 59. try 60. Dispatch.call(workbook, Save); 61. Dispatch.call(workbook, Close, new Variant(f); 62. catch (Exception e) 63. e.printStackTrace; 64. finally 65
13、. releaseSource; 66. 67. 68. /* 69. * 初始化 70. * */ 71. private void initComponents 72. workbook = null; 73. currentSheet = null; 74. sheets = null; 75. 76. /* 77. * 释放资源 78. */ 79. private static void releaseSource 80. if(xl!=null) 81. xl.invoke(Quit, new Variant ); 82. xl = null; 83. 84. workbooks
14、= null; 85. ComThread.Release; 86. System.gc; 87. 88. /* 89. * 得到当前sheet 90. * return 91. */ 92. private Dispatch getCurrentSheet 93. currentSheet = Dispatch.get(workbook, ActiveSheet).toDispatch; 94. return currentSheet; 95. 96. /* 97. * 修改当前工作表的名字 98. * param newName 99. */ 100. private void modif
15、yCurrentSheetName(String newName) 101. Dispatch.put(getCurrentSheet, name, newName); 102. 103. 104. /* 105. * 得到当前工作表的名字 106. * return 107. */ 108. private String getCurrentSheetName(Dispatch sheets) 109. return Dispatch.get(sheets, name).toString; 110. 111. /* 112. * 通过工作表名字得到工作表 113. * param name
16、sheetName 114. * return 115. */ 116. private Dispatch getSheetByName(String name) 117. return Dispatch.invoke(getSheets, Item, Dispatch.Get, new Objectname, new int1).toDispatch; 118. 119. /* 120. * 得到sheets的集合对象 121. * return 122. */ 123. private Dispatch getSheets 124. if(sheets=null) 125. sheets
17、= Dispatch.get(workbook, sheets).toDispatch; 126. return sheets; 127. 128. /* 129. * 通过工作表索引得到工作表(第一个工作簿index为1) 130. * param index 131. * return sheet对象 132. */ 133. private Dispatch getSheetByIndex(Integer index) 134. return Dispatch.invoke(getSheets, Item, Dispatch.Get, new Objectindex, new int1)
18、.toDispatch; 135. 136. 137. /* 138. * 得到sheet的总数 139. * return 140. */ 141. private int getSheetCount 142. int count = Dispatch.get(getSheets, count).toInt; 143. return count; 144. 145. /* 146. * 给所有的sheet添加背景 147. * param filepath 图片路径 148. */ 149. public void setBlackGroudPrituce(String filepath)
19、150. 151. int num=this.getSheetCount; 152. for (int i = 1; i = num; i+) 153. Dispatch sheets=this.getSheetByIndex(i); 154. Dispatch.call(sheets,SetBackgroundPicture,filepath); 155. 156. 157. /* 158. * 添加新的工作表(sheet),并且隐藏 159. */ 160. public void addSheet(String name) 161. / for (int i = 1; i = this.
20、getSheetCount; i+) 162. / Dispatch sheets=this.getSheetByIndex(i); 163. / if(name.equals(this.getCurrentSheetName(sheets) 164. / 165. / return false; 166. / 167. / 168. currentSheet=Dispatch.get(Dispatch.get(workbook, sheets).toDispatch, add).toDispatch; 169. / Dispatch.put(currentSheet,Name,name);
21、170. Dispatch.put(currentSheet, Visible, new Boolean(false); 171. System.out.println(插入信息为:+name); 172. 173. /* 174. * 得到工作薄的名字 175. * return 176. */ 177. private String getWorkbookName 178. if(workbook=null) 179. return null; 180. return Dispatch.get(workbook, name).toString; 181. 182. /* 183. * 获取
22、所有表名 184. */ 185. public List findSheetName 186. 187. int num=this.getSheetCount; 188. List list=new ArrayList; 189. for (int i = 1; i 26) 318. 319. int multiple=(cnum)/26; 320. int remainder=(cnum)%26; 321. char mchar=(char)(multiple+64); 322. char rchar=(char)(remainder+64); 323. cposition=mchar+rchar; 324. 325. else 326. 327. cposition=(char)(cnum+64)+; 328.