java jacob 操作word 文档,进行写操作,如生成表格,添加 图片 jacob-1.15-M3-x86.dll copy 到c://windows/system32
引入jacob.jar
示例代码
[java] view plaincopy
1. 2. 3. 4. 5. 6. 7. 8. 9. 10. 11. 12. 13. 14. 15. 16. 17. 18. 19. 20. 21. 22. 23. 24. 25. 26. 27. 28. 29. 30. 31. 32. import java.io.File; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; class WordBean { // 代表一个word 程序 private ActiveXComponent MsWordApp = null; // 代表进行处理的word 文档 private Dispatch document = null; public WordBean() { // Open Word if we/'ve not done it already if (MsWordApp == null) { MsWordApp = new ActiveXComponent(\); } } // 设置是否在前台打开 word 程序 , public void setVisible(boolean visible) { MsWordApp.setProperty(\, new Variant(visible)); // 这一句作用相同 // Dispatch.put(MsWordApp, \ } // 创建一个新文档 public void createNewDocument() { // Find the Documents collection object maintained by Word // documents表示word的所有文档窗口,(word是多文档应用程序) Dispatch documents = Dispatch.get(MsWordApp, \).toDispatch(); // Call the Add method of the Documents collection to create // a new document to edit document = Dispatch.call(documents, \).toDispatch(); } // 打开一个存在的word文档,并用document 引用 引用它 public void openFile(String wordFilePath) { 33. 34. 35. 36. 37. 38. 39. 40. 41. 42. 43. 44. 45. 46. 47. 48. 49. 50. 51. 52. 53. 54. 55. 56. 57. 58. 59. 60. 61. 62. 63. 64. 65. 66. 67. 68. 69. 70. 71. 72. 73. 74. 75. 76. 77. // Find the Documents collection object maintained by Word // documents表示word的所有文档窗口,(word是多文档应用程序) Dispatch documents = Dispatch.get(MsWordApp, \).toDispatch(); document = Dispatch.call(documents, \, wordFilePath, new Variant(true)/* 是否进行转换ConfirmConversions */, new Variant(false)/* 是否只读 */).toDispatch(); // document = Dispatch.invoke(documents, \ // new Object[] { wordFilePath, new Variant(true), // new Variant(false) // }, new int[1]).toDispatch(); } // 向 document 中插入文本内容 public void insertText(String textToInsert) { // Get the current selection within Word at the moment. // a new document has just been created then this will be at // the top of the new doc 获得选 中的内容,如果是一个新创建的文件,因里面无内容,则光标应处于文件开头处 Dispatch selection = Dispatch.get(MsWordApp, \).toDispatch(); // 取消选中,应该就是移动光标 ,否则 新添加的内容会覆盖选中的内容 Dispatch.call(selection, \, new Variant(1), new Variant(1)); // Put the specified text at the insertion point Dispatch.put(selection, \, textToInsert); // 取消选中,应该就是移动光标 Dispatch.call(selection, \, new Variant(1), new Variant(1)); } // 向文档中添加 一个图片, public void insertJpeg(String jpegFilePath) { Dispatch selection = Dispatch.get(MsWordApp, \).toDispatch(); Dispatch image = Dispatch.get(selection, \).toDispatch(); Dispatch.call(image, \, jpegFilePath); } // 段落的处理,插入格式化的文本 public void insertFormatStr(String text) { Dispatch wordContent = Dispatch.get(document, \).toDispatch(); // 取得word文件的内容 Dispatch.call(wordContent, \, text);// 插入一个段落到最后 Dispatch paragraphs = Dispatch.get(wordContent, \) .toDispatch(); // 所有段落 int paragraphCount = Dispatch.get(paragraphs, \).changeType( Variant.VariantInt).getInt();// 一共的段落数 // 找到刚输入的段落,设置格式 Dispatch lastParagraph = Dispatch.call(paragraphs, \, new Variant(paragraphCount)).toDispatch(); // 最后一段(也就是刚插入的) // Range 对象表示文档中的一个连续范围,由一个起始字符位置和一个终止字符位置定义 Dispatch lastParagraphRange = Dispatch.get(lastParagraph, \) .toDispatch(); Dispatch font = Dispatch.get(lastParagraphRange, \).toDispatch(); 78. 79. 80. 81. 82. 83. 84. 85. 86. 87. 88. 89. 90. 91. 92. 93. 94. 95. 96. 97. 98. 99. Dispatch.put(font, \, new Variant(true)); // 设置为黑体 Dispatch.put(font, \, new Variant(true)); // 设置为斜体 Dispatch.put(font, \, new Variant(\宋体\)); // Dispatch.put(font, \, new Variant(12)); // 小四 Dispatch selection = Dispatch.get(MsWordApp, \).toDispatch(); Dispatch.call(selection, \);// 插入一个空行 Dispatch alignment = Dispatch.get(selection, \) .toDispatch();// 段落格式 Dispatch.put(alignment, \, \); // (1:置中 2:靠右 3:靠左) } // word 中在对表格进行遍历的时候 ,是先列后行 先column 后cell // 另外下标从1开始 public void insertTable(String tableTitle, int row, int column) { Dispatch selection = Dispatch.get(MsWordApp, \).toDispatch(); // 输入内容需要的对象 Dispatch.call(selection, \, tableTitle); // 写入标题内容 // 标题格行 Dispatch.call(selection, \); // 空一行段落 Dispatch.call(selection, \); // 空一行段落 Dispatch.call(selection, \); // 游标往下一行 // 建立表格 Dispatch tables = Dispatch.get(document, \).toDispatch(); // int count = Dispatch.get(tables, // \中的表格数量 100. // Dispatch table = Dispatch.call(tables, \ 101. // 1)).toDispatch();//文档中第一个表格 102. Dispatch range = Dispatch.get(selection, \).toDispatch();// /当前光标位置或者选中的区域 103. Dispatch newTable = Dispatch.call(tables, \, range, 104. new Variant(row), new Variant(column), new Variant(1)) 105. .toDispatch(); // 设置row,column,表格外框宽度 106. Dispatch cols = Dispatch.get(newTable, \).toDispatch(); // 此表的所有列, 107. int colCount = Dispatch.get(cols, \).changeType( 108. Variant.VariantInt).getInt();// 一共有多少列 实际上这个数==column 109. System.out.println(colCount + \列\); 110. for (int i = 1; i <= colCount; i++) { // 循环取出每一列 111. Dispatch col = Dispatch.call(cols, \, new Variant(i)) 112. .toDispatch(); 113. Dispatch cells = Dispatch.get(col, \).toDispatch();// 当前列中单元格 114. int cellCount = Dispatch.get(cells, \).changeType( 115. Variant.VariantInt).getInt();// 当前列中单元格数 实际上这个数等于row 116. for (int j = 1; j <= cellCount; j++) {// 每一列中的单元格数 117. // Dispatch cell = Dispatch.call(cells, \ 118. // Variant(j)).toDispatch(); //当前单元格 119. // Dispatch cell = Dispatch.call(newTable, \ 120. // Variant(j) , new Variant(i) ).toDispatch(); //取单元格的另一种方法 121. // Dispatch.call(cell, \选中当前单元格 122. // Dispatch.put(selection, \ 123. // \第\行,第\列\往选中的区域中填值,也就是往当前单元格填值 124. putTxtToCell(newTable, j, i, \第\ + j + \行,第\ + i + \列\);// 与上面四句的作用相同 125. } 126. } 127. } 128. /** */ 129. /** 130. * 在指定的单元格里填写数据 131. * 132. * @param tableIndex 133. * @param cellRowIdx 134. * @param cellColIdx 135. * @param txt 136. */ 137. public void putTxtToCell(Dispatch table, int cellRowIdx, int cellColIdx, 138. String txt) { 139. Dispatch cell = Dispatch.call(table, \, new Variant(cellRowIdx), 140. new Variant(cellColIdx)).toDispatch(); 141. Dispatch.call(cell, \); 142. Dispatch selection = Dispatch.get(MsWordApp, \).toDispatch(); // 输入内容需要的对象 143. Dispatch.put(selection, \, txt); 144. } 145. /** */ 146. /** 147. * 在指定的单元格里填写数据 148. * 149. * @param tableIndex 150. * @param cellRowIdx 151. * @param cellColIdx 152. * @param txt 153. */ 154. public void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx, 155. String txt) { 156. // 所有表格 157. Dispatch tables = Dispatch.get(document, \).toDispatch(); 158. // 要填充的表格 159. Dispatch table = Dispatch.call(tables, \, new Variant(tableIndex)) 160. .toDispatch(); 161. Dispatch cell = Dispatch.call(table, \, new Variant(cellRowIdx), 162. new Variant(cellColIdx)).toDispatch(); 163. Dispatch.call(cell, \); 164. Dispatch selection = Dispatch.get(MsWordApp, \).toDispatch(); // 输入内容需要的对象 165. Dispatch.put(selection, \, txt); 166. } 167. // 合并两个单元格 168. public void mergeCell(Dispatch cell1, Dispatch cell2) { 169. Dispatch.call(cell1, \, cell2); 170. } 171. public void mergeCell(Dispatch table, int row1, int col1, int row2, int col2) { 172. Dispatch cell1 = Dispatch.call(table, \, new Variant(row1), 173. new Variant(col1)).toDispatch(); 174. Dispatch cell2 = Dispatch.call(table, \, new Variant(row2), 175. new Variant(col2)).toDispatch(); 176. mergeCell(cell1, cell2); 177. } 178. public void mergeCellTest() { 179. Dispatch tables = Dispatch.get(document, \).toDispatch(); 180. int tableCount = Dispatch.get(tables, \).changeType( 181. Variant.VariantInt).getInt(); // document中的表格数量 182. Dispatch table = Dispatch.call(tables, \, new Variant(tableCount)) 183. .toDispatch();// 文档中最后一个table 184. mergeCell(table, 1, 1, 1, 2);// 将table 中x=1,y=1 与x=1,y=2的两个单元格合并 185. } 186. // ======================================================== 187. /** */ 188. /** 189. * 把选定的内容或光标插入点向上移动 190. * 191. * @param pos 192. * 移动的距离 193. */ 194. public void moveUp(int pos) { 195. Dispatch selection = Dispatch.get(MsWordApp, \).toDispatch(); // 输入内容需要的对象 196. for (int i = 0; i < pos; i++) { 197. // MoveDown MoveLeft moveRight 198. // moveStart ( Dispatch.call(selection, \ 199. // ) 200. // moveEnd Dispatch.call(selection, \ 201. Dispatch.call(selection, \); 202. } 203. } 204. /** */ 205. /** 206. * 从选定内容或插入点开始查找文本 207. * 208. * @param toFindText 209. * 要查找的文本 210. * @return boolean true-查找到并选中该文本,false-未查找到文本 211. */