jacob的使用方法总结

用jacob做了一个东西,就是对word进行操作,开始时费了好大劲,后来总算是有点思路。现将已试验过的方法总结如下。

还有一点就是所用的JAR文件和DLL文件好像比较特殊,JDK换来换去就用了JDK1.6,jacob.jar为1.9的,dll为2005年2月26日的。

有什么问题可以在此留言,大家一起交流。

  1. import com.jacob.activeX.ActiveXComponent;
  2. import com.jacob.com.Dispatch;
  3. import com.jacob.com.Variant;
  4. import com.jacob.com.ComThread;
  5. public class StudyJacob {
  6. // word文档
  7. private Dispatch doc = null ;
  8. // word运行程序对象
  9. private ActiveXComponent word = null ;
  10. // 所有word文档集合
  11. private Dispatch documents = null ;
  12. // 选定的范围或插入点
  13. private static Dispatch selection = null ;
  14. // 设置是否保存后才退出的标志
  15. private boolean saveOnExit = true ;
  16. /**
  17. * word中的当前文档
  18. */
  19. private static Dispatch document = null ;
  20. // private static Dispatch textFrame = null;
  21. //
  22. /**
  23. * 所有表格
  24. */
  25. private Dispatch tables;
  26. /**
  27. * 当前表格
  28. */
  29. private Dispatch table;
  30. /**
  31. * 当前单元格
  32. */
  33. private Dispatch cell;
  34. /**
  35. * 当前表格中的所有行
  36. */
  37. private Dispatch rows;
  38. /**
  39. * 表格中的某一行
  40. */
  41. private Dispatch row;
  42. /**
  43. * 表格中的所有列
  44. */
  45. private Dispatch columns;
  46. /**
  47. * 表格中的某一列
  48. */
  49. private Dispatch column;
  50. /**
  51. * 打开word时同时要打开的文档,不指定时将新建一个空白文档
  52. */
  53. // private File openDoc;
  54. private static Dispatch shapes;
  55. private static Dispatch shape;
  56. private static Dispatch textRange;
  57. private static Dispatch textframe;
  58. private Dispatch range;
  59. private Dispatch paragraphs;
  60. private Dispatch paragraph;
  61. // constructor
  62. public StudyJacob() {
  63. if (word == null ) {
  64. word = new ActiveXComponent( "Word.Application" );
  65. word.setProperty( "Visible" , new Variant( true ));
  66. }
  67. if (documents == null ) {
  68. documents = word.getProperty( "Documents").toDispatch();
  69. }
  70. }
  71. /**
  72. * 创建一个新的word文档
  73. *
  74. */
  75. public void createNewDocument() {
  76. doc = Dispatch.call(documents, "Add" ).toDispatch();
  77. selection = Dispatch.get(word, "Selection" ).toDispatch();
  78. }
  79. /**
  80. * 打开一个已存在的文档
  81. *
  82. * @param docPath
  83. */
  84. public void openDocument(String docPath) {
  85. if ( this .doc != null ) {
  86. this .closeDocument();
  87. }
  88. doc = Dispatch.call(documents, "Open", docPath).toDispatch();
  89. selection = Dispatch.get(word, "Selection" ).toDispatch();
  90. }
  91. /**
  92. * 关闭当前word文档
  93. *
  94. */
  95. public void closeDocument() {
  96. if (doc != null ) {
  97. Dispatch.call(doc, "Save" );
  98. Dispatch.call(doc, "Close" , new Variant(saveOnExit));
  99. doc = null ;
  100. }
  101. }
  102. /**
  103. * 关闭全部应用
  104. *
  105. */
  106. public void close() {
  107. closeDocument();
  108. if (word != null ) {
  109. Dispatch.call(word, "Quit" );
  110. word = null ;
  111. }
  112. selection = null ;
  113. documents = null ;
  114. }
  115. /**
  116. * 把插入点移动到文件首位置
  117. *
  118. */
  119. public void moveStart() {
  120. if (selection == null )
  121. selection = Dispatch.get(word, "Selection").toDispatch();
  122. Dispatch.call(selection, "HomeKey" , new Variant( 6 ));
  123. }
  124. /**
  125. * 在当前插入点插入字符串
  126. *
  127. * @param newText
  128. * 要插入的新字符串
  129. */
  130. public void insertText(String newText) {
  131. Dispatch.put(selection, "Text" , newText);
  132. }
  133. /**
  134. * 在当前插入点插入图片
  135. *
  136. * @param imagePath
  137. * 图片路径
  138. */
  139. public void insertImage(String imagePath) {
  140. Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
  141. "AddPicture" , imagePath);
  142. }
  143. /**
  144. * 把选定的内容或者插入点向下移动
  145. *
  146. * @param pos
  147. * 移动的距离
  148. */
  149. public void moveDown( int pos) {
  150. if (selection == null )
  151. selection = Dispatch.get(word, "Selection").toDispatch();
  152. for ( int i = 0 ; i < pos; i++)
  153. Dispatch.call(selection, "MoveDown" );
  154. }
  155. /**
  156. * 把选定的内容或插入点向上移动
  157. *
  158. * @param pos
  159. * 移动的距离
  160. */
  161. public void moveUp( int pos) {
  162. if (selection == null )
  163. selection = Dispatch.get(word, "Selection").toDispatch();
  164. for ( int i = 0 ; i < pos; i++)
  165. Dispatch.call(selection, "MoveUp" );
  166. }
  167. /**
  168. * 把选定的内容或者插入点向左移动
  169. *
  170. * @param pos
  171. * 移动的距离
  172. */
  173. public void moveLeft( int pos) {
  174. if (selection == null )
  175. selection = Dispatch.get(word, "Selection").toDispatch();
  176. for ( int i = 0 ; i < pos; i++) {
  177. Dispatch.call(selection, "MoveLeft" );
  178. }
  179. }
  180. /**
  181. * 把选定的内容或者插入点向右移动
  182. *
  183. * @param pos
  184. * 移动的距离
  185. */
  186. public void moveRight( int pos) {
  187. if (selection == null )
  188. selection = Dispatch.get(word, "Selection").toDispatch();
  189. for ( int i = 0 ; i < pos; i++)
  190. Dispatch.call(selection, "MoveRight" );
  191. }
  192. /**
  193. * 文件保存或另存为
  194. *
  195. * @param savePath
  196. * 一定要记得加上扩展名 .doc 保存或另存为路径
  197. */
  198. public void save(String savePath) {
  199. Dispatch.call(
  200. (Dispatch) Dispatch.call(word, "WordBasic").getDispatch(),
  201. "FileSaveAs" , savePath);
  202. }
  203. /**
  204. * 从第tIndex个Table中取出值第row行,第col列的值
  205. *
  206. * @param tableIndex
  207. * 文档中的第tIndex个Table,即tIndex为索引取
  208. * @param cellRowIdx
  209. * cell在Table第row行
  210. * @param cellColIdx
  211. * cell在Talbe第col列
  212. * @return cell单元值
  213. * @throws Exception
  214. */
  215. public String getCellString( int tableIndex, int cellRowIdx, int cellColIdx)
  216. throws Exception {
  217. // 所有表格
  218. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  219. // 要取数据的表格
  220. Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
  221. .toDispatch();
  222. Dispatch cell = Dispatch.call(table, "Cell" , new Variant(cellRowIdx),
  223. new Variant(cellColIdx)).toDispatch();
  224. Dispatch.call(cell, "Select" );
  225. return Dispatch.get(selection, "Text" ).toString();
  226. }
  227. /**
  228. * 从第tableIndex个Table中取出值第cellRowIdx行,第cellColIdx列的值
  229. *
  230. * @param tIndex
  231. * 文档中的第tIndex个Table,即tIndex为索引取
  232. * @param cellRowIdx
  233. * cell在Table第row行
  234. * @param cellColIdx
  235. * cell在Talbe第col列
  236. * @return cell单元值
  237. * @throws Exception
  238. */
  239. public void getCellValue( int tableIndex, int cellRowIdx, int cellColIdx)
  240. throws Exception {
  241. // 所有表格
  242. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  243. // 要取数据的表格
  244. Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
  245. .toDispatch();
  246. Dispatch cell = Dispatch.call(table, "Cell" , new Variant(cellRowIdx),
  247. new Variant(cellColIdx)).toDispatch();
  248. Dispatch.call(cell, "Select" );
  249. Dispatch.call(selection, "Copy" );
  250. }
  251. /**
  252. * 在当前光标处做粘贴
  253. */
  254. public void paste() {
  255. Dispatch.call(selection, "Paste" );
  256. }
  257. /**
  258. * 在当前光标处添加图片
  259. *
  260. * @param imgPath
  261. * 图片的地址
  262. */
  263. public void addImage(String imgPath) {
  264. if (imgPath != "" && imgPath != null ) {
  265. Dispatch image = Dispatch.get(selection, "InLineShapes")
  266. .toDispatch();
  267. Dispatch.call(image, "AddPicture" , imgPath);
  268. }
  269. }
  270. /**
  271. * 在指定的单元格里填写数据
  272. *
  273. * @param tableIndex
  274. * 文档中的第tIndex个Table,即tIndex为索引取
  275. * @param cellRowIdx
  276. * cell在Table第row行
  277. * @param cellColIdx
  278. * cell在Talbe第col列
  279. * @param txt
  280. * 填写的数据
  281. */
  282. public void putTxtToCell( int tableIndex, int cellRowIdx, int cellColIdx,
  283. String txt) {
  284. // 所有表格
  285. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  286. // 要填充的表格
  287. Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
  288. .toDispatch();
  289. Dispatch cell = Dispatch.call(table, "Cell" , new Variant(cellRowIdx),
  290. new Variant(cellColIdx)).toDispatch();
  291. Dispatch.call(cell, "Select" );
  292. Dispatch.put(selection, "Text" , txt);
  293. }
  294. /**
  295. *
  296. * 得到当前文档的tables集合
  297. */
  298. public Dispatch getTables() throws Exception {
  299. if ( this .doc == null ) {
  300. throw new Exception("there is not a document can't be operate!!!" );
  301. }
  302. return Dispatch.get(doc, "Tables" ).toDispatch();
  303. }
  304. /**
  305. *
  306. * 得到当前文档的表格数
  307. *
  308. * @param Dispatch
  309. */
  310. public int getTablesCount(Dispatch tables) throws Exception {
  311. int count = 0 ;
  312. try {
  313. this .getTables();
  314. } catch (Exception e) {
  315. throw new Exception( "there is not any table!!" );
  316. }
  317. count = Dispatch.get(tables, "Count" ).toInt();
  318. return count;
  319. }
  320. /**
  321. * 在当前文档指定的位置拷贝表格
  322. *
  323. * @param pos
  324. * 当前文档指定的位置
  325. * @param tableIndex
  326. * 被拷贝的表格在word文档中所处的位置
  327. */
  328. public void copyTable(String pos, int tableIndex) {
  329. // 所有表格
  330. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  331. // 要填充的表格
  332. Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
  333. .toDispatch();
  334. Dispatch range = Dispatch.get(table, "Range").toDispatch();
  335. Dispatch.call(range, "Copy" );
  336. Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
  337. Dispatch.call(textRange, "Paste" );
  338. }
  339. /**
  340. * 在当前文档指定的位置拷贝来自另一个文档中的表格
  341. *
  342. * @param anotherDocPath
  343. * 另一个文档的磁盘路径
  344. * @param tableIndex
  345. * 被拷贝的表格在另一格文档中的位置
  346. * @param pos
  347. * 当前文档指定的位置
  348. */
  349. public void copyTableFromAnotherDoc(String anotherDocPath, int tableIndex,
  350. String pos) {
  351. Dispatch doc2 = null ;
  352. try {
  353. doc2 = Dispatch.call(documents, "Open", anotherDocPath)
  354. .toDispatch();
  355. // 所有表格
  356. Dispatch tables = Dispatch.get(doc2, "Tables").toDispatch();
  357. // 要填充的表格
  358. Dispatch table = Dispatch.call(tables, "Item" ,
  359. new Variant(tableIndex)).toDispatch();
  360. Dispatch range = Dispatch.get(table, "Range").toDispatch();
  361. Dispatch.call(range, "Copy" );
  362. Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
  363. moveDown( 1 );
  364. Dispatch.call(textRange, "Paste" );
  365. } catch (Exception e) {
  366. e.printStackTrace();
  367. } finally {
  368. if (doc2 != null ) {
  369. Dispatch.call(doc2, "Close" , new Variant(saveOnExit));
  370. doc2 = null ;
  371. }
  372. }
  373. }
  374. /**
  375. * 在当前文档拷贝剪贴板数据
  376. *
  377. * @param pos
  378. */
  379. public void pasteExcelSheet(String pos) {
  380. moveStart();
  381. Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
  382. Dispatch.call(textRange, "Paste" );
  383. }
  384. /**
  385. * 选中dispatch对象
  386. *
  387. * @param dispatch(分配,派遣)
  388. */
  389. private void select(Dispatch dispatch) {
  390. Dispatch.call(dispatch, "Select" );
  391. }
  392. /**
  393. * 在当前文档指定的位置拷贝来自另一个文档中的图片
  394. *
  395. * @param anotherDocPath
  396. * 另一个文档的磁盘路径
  397. * @param shapeIndex
  398. * 被拷贝的图片在另一格文档中的位置
  399. * @param pos
  400. * 当前文档指定的位置
  401. * @throws com.jacob.com.ComFailException
  402. * Invoke of: Item Source: Microsoft Word 若shapeIndex不存在
  403. */
  404. public void copyImageFromAnotherDoc(String anotherDocPath, int shapeIndex,
  405. String pos) {
  406. Dispatch doc2 = null ;
  407. try {
  408. doc2 = Dispatch.call(documents, "Open", anotherDocPath)
  409. .toDispatch();
  410. Dispatch shapes = Dispatch.get(doc2, "InLineShapes").toDispatch();
  411. Dispatch shape = Dispatch.call(shapes, "Item" ,
  412. new Variant(shapeIndex)).toDispatch();
  413. Dispatch imageRange = Dispatch.get(shape, "Range").toDispatch();
  414. Dispatch.call(imageRange, "Copy" );
  415. Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
  416. moveDown( 4 );
  417. Dispatch.call(textRange, "Paste" );
  418. } catch (Exception e) {
  419. e.printStackTrace();
  420. } finally {
  421. if (doc2 != null ) {
  422. Dispatch.call(doc2, "Close" , new Variant(saveOnExit));
  423. doc2 = null ;
  424. }
  425. }
  426. }
  427. /**
  428. * 打印当前word文档
  429. *
  430. * @throws Exception
  431. * com.jacob.com.ComFailException: Invoke of: PrintOut Source:
  432. * Microsoft Word 若无打印机
  433. */
  434. public void printFile() {
  435. if (doc != null ) {
  436. Dispatch.call(doc, "PrintOut" );
  437. }
  438. }
  439. /**
  440. * 打印文本,反选,在文本后换行<br>
  441. * 警告:使用了Home, End来取消选中
  442. *
  443. * @param s
  444. */
  445. public void println(String s) {
  446. write(s);
  447. goToEnd();
  448. cmd( "TypeParagraph" );
  449. }
  450. /**
  451. * 执行某条宏指令
  452. *
  453. * @param cmd
  454. */
  455. private void cmd(String cmd) {
  456. Dispatch.call(selection, cmd);
  457. }
  458. /**
  459. * 按下Ctrl + End键
  460. */
  461. public void goToEnd() {
  462. Dispatch.call(selection, "EndKey" , "6" );
  463. }
  464. /**
  465. * 反选,再打印文本<br>
  466. * 警告:使用了Home, End来取消选中
  467. */
  468. public void print(String s) {
  469. goToEnd();
  470. write(s);
  471. }
  472. /**
  473. * 不反选, 直接输出文本
  474. *
  475. * @param s
  476. */
  477. public void write(String s) {
  478. Dispatch.put(selection, "Text" , s);
  479. }
  480. /**
  481. * 反选,在文本后换行<br>
  482. * 警告:使用了Home, End来取消选中
  483. */
  484. public void println() {
  485. home();
  486. end();
  487. cmd( "TypeParagraph" );
  488. }
  489. /**
  490. * 按下Home键
  491. */
  492. public void home() {
  493. Dispatch.call(selection, "HomeKey" , "5" );
  494. }
  495. /**
  496. * 按下End键
  497. */
  498. public void end() {
  499. Dispatch.call(selection, "EndKey" , "5" );
  500. }
  501. /**
  502. * 按下Ctrl + Home键
  503. */
  504. public void goToBegin() {
  505. Dispatch.call(selection, "HomeKey" , "6" );
  506. }
  507. /**
  508. * 设置指定表格指定列的列宽
  509. *
  510. * @param tableIndex
  511. * @param columnWidth
  512. * @param columnIndex
  513. */
  514. public void setColumnWidth( int tableIndex, float columnWidth,
  515. int columnIndex) {
  516. this .getTable(tableIndex);
  517. this .setColumnWidth(columnWidth, columnIndex);
  518. }
  519. /**
  520. * 查找表
  521. *
  522. * @param tableIndex
  523. * @return
  524. */
  525. public Dispatch getTable( int tableIndex) {
  526. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  527. Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
  528. .toDispatch();
  529. return table;
  530. }
  531. public Dispatch getShapes() throws Exception {
  532. return Dispatch.get(doc, "Shapes" ).toDispatch();
  533. }
  534. public int getShapesCount() throws Exception {
  535. int count = 0 ;
  536. count = Dispatch.get(shapes, "Count" ).toInt();
  537. return count;
  538. }
  539. public Dispatch getShape( int tIndex) throws Exception {
  540. return Dispatch.call(shapes, "Item" , new Variant(tIndex)).toDispatch();
  541. // return Dispatch.invoke(shapes,"item",Dispatch.Method,new Object[]{
  542. // new Integer(tIndex)},new int[1]).toDispatch();
  543. }
  544. public Dispatch getTextFrame() throws Exception {
  545. return Dispatch.get(shape, "TextFrame" ).toDispatch();
  546. }
  547. public Dispatch getTextRange() throws Exception {
  548. return Dispatch.get(textframe, "TextRange").toDispatch();
  549. }
  550. /**
  551. * 设置当前表格指定列的列宽
  552. *
  553. * @param columnWidth
  554. * @param columnIndex
  555. * @throws 如果不是整齐的表格不能使用
  556. */
  557. public void setColumnWidth( float columnWidth, int columnIndex) {
  558. if (columnWidth < 11 ) {
  559. columnWidth = 120 ;
  560. }
  561. if (columns == null || column == null ) {
  562. this .getColumns();
  563. this .getColumn(columnIndex);
  564. }
  565. Dispatch.put(column, "Width" , new Variant(columnWidth));
  566. }
  567. /**
  568. * 设置指定表格指定列的背景色
  569. *
  570. * @param tableIndex
  571. * @param columnIndex
  572. * @param color
  573. * 取值范围 0 < color < 17 默认:16 浅灰色 1:黑色 2:蓝色 3:浅蓝 ...............
  574. */
  575. public void setColumnBgColor( int tableIndex, int columnIndex, int color) {
  576. this .getTable(tableIndex);
  577. this .setColumnBgColor(columnIndex, color);
  578. }
  579. /**
  580. * 设置当前表格指定列的背景色
  581. *
  582. * @param columnIndex
  583. * @param color
  584. * 取值范围 0 < color < 17 默认:16 浅灰色 1:黑色 2:蓝色 3:浅蓝 ...............
  585. */
  586. public void setColumnBgColor( int columnIndex, int color) {
  587. this .getColumn(columnIndex);
  588. Dispatch shading = Dispatch.get(column, "Shading").toDispatch();
  589. if (color > 16 || color < 1 )
  590. color = 16 ;
  591. Dispatch
  592. .put(shading, "BackgroundPatternColorIndex" , new Variant(color));
  593. }
  594. /**
  595. * 初始化 com 线程
  596. */
  597. public void initCom() {
  598. ComThread.InitSTA();
  599. }
  600. /**
  601. * 释放 com 线程资源 com 的线程回收不由 java 垃圾回收机制回收
  602. */
  603. public void releaseCom() {
  604. ComThread.Release();
  605. }
  606. /**
  607. * 设置当前表格指定行的背景色
  608. *
  609. * @param rowIndex
  610. * @param color
  611. * 取值范围 0 < color < 17 默认:16 浅灰色 1:黑色 2:蓝色 3:浅蓝 ...............
  612. */
  613. public void setRowBgColor( int rowIndex, int color) {
  614. this .getRow(rowIndex);
  615. Dispatch shading = Dispatch.get(row, "Shading").toDispatch();
  616. if (color > 16 || color < 1 )
  617. color = 16 ;
  618. Dispatch
  619. .put(shading, "BackgroundPatternColorIndex" , new Variant(color));
  620. }
  621. /**
  622. * 设置指定表格的指定行的背景色
  623. *
  624. * @param tableIndex
  625. * @param rowIndex
  626. * @param color
  627. * 取值范围 0 < color < 17 默认:16 浅灰色 1:黑色 2:蓝色 3:浅蓝 ...............
  628. */
  629. public void setRowBgColor( int tableIndex, int rowIndex, int color) {
  630. this .getTable(tableIndex);
  631. this .setRowBgColor(rowIndex, color);
  632. }
  633. /**
  634. * 设置当前选定内容的字体
  635. *
  636. * @param isBold
  637. * 是否为粗体
  638. * @param isItalic
  639. * 是否为斜体
  640. * @param isUnderLine
  641. * 是否带下划线
  642. * @param color
  643. * rgb 字体颜色 例如:红色 255,0,0
  644. * @param size
  645. * 字体大小 12:小四 16:三号
  646. * @param name
  647. * 字体名称 例如:宋体,新宋体,楷体,隶书
  648. */
  649. public void setFont( boolean isBold, boolean isItalic, boolean isUnderLine,
  650. String color, String size, String name) {
  651. Dispatch font = Dispatch.get(selection, "Font").toDispatch();
  652. Dispatch.put(font, "Name" , new Variant(name));
  653. Dispatch.put(font, "Bold" , new Variant(isBold));
  654. Dispatch.put(font, "Italic" , new Variant(isItalic));
  655. Dispatch.put(font, "Underline" , new Variant(isUnderLine));
  656. Dispatch.put(font, "Color" , color);
  657. Dispatch.put(font, "Size" , size);
  658. }
  659. /**
  660. * 恢复默认字体 不加粗,不倾斜,没下划线,黑色,小四号字,宋体
  661. */
  662. public void clearFont() {
  663. this .setFont( false , false , false , "0,0,0" , "12", "宋体" );
  664. }
  665. /**
  666. * 对当前段落进行格式化
  667. *
  668. * @param align
  669. * 设置排列方式 默认:居左 0:居左 1:居中 2:居右 3:两端对齐 4:分散对齐
  670. * @param lineSpace
  671. * 设置行间距 默认:1.0 0:1.0 1:1.5 2:2.0 3:最小值 4:固定值
  672. */
  673. public void setParaFormat( int align, int lineSpace) {
  674. if (align < 0 || align > 4 ) {
  675. align = 0 ;
  676. }
  677. if (lineSpace < 0 || lineSpace > 4 ) {
  678. lineSpace = 0 ;
  679. }
  680. Dispatch alignment = Dispatch.get(selection, "ParagraphFormat" )
  681. .toDispatch();
  682. Dispatch.put(alignment, "Alignment" , align);
  683. Dispatch.put(alignment, "LineSpacingRule" , new Variant(lineSpace));
  684. }
  685. /**
  686. * 还原段落默认的格式 左对齐,行间距:1.0
  687. */
  688. public void clearParaFormat() {
  689. this .setParaFormat( 0 , 0 );
  690. }
  691. /**
  692. * 创建表格
  693. *
  694. * @param pos
  695. * 位置
  696. * @param cols
  697. * 列数
  698. * @param rows
  699. * 行数
  700. */
  701. public void createTable(String pos, int numCols, int numRows) {
  702. if (find(pos)) {
  703. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  704. Dispatch range = Dispatch.get(selection, "Range").toDispatch();
  705. Dispatch newTable = Dispatch.call(tables, "Add", range,
  706. new Variant(numRows), new Variant(numCols)).toDispatch();
  707. Dispatch.call(selection, "MoveRight" );
  708. }
  709. }
  710. /**
  711. * 在指定行前面增加行
  712. *
  713. * @param tableIndex
  714. * word文件中的第N张表(从1开始)
  715. * @param rowIndex
  716. * 指定行的序号(从1开始)
  717. */
  718. public void addTableRow( int tableIndex, int rowIndex) {
  719. // 所有表格
  720. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  721. // 要填充的表格
  722. Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
  723. .toDispatch();
  724. // 表格的所有行
  725. Dispatch rows = Dispatch.get(table, "Rows" ).toDispatch();
  726. Dispatch row = Dispatch.call(rows, "Item" , new Variant(rowIndex))
  727. .toDispatch();
  728. Dispatch.call(rows, "Add" , new Variant(row));
  729. }
  730. /**
  731. * 在第1行前增加一行
  732. *
  733. * @param tableIndex
  734. * word文档中的第N张表(从1开始)
  735. */
  736. public void addFirstTableRow( int tableIndex) {
  737. // 所有表格
  738. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  739. // 要填充的表格
  740. Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
  741. .toDispatch();
  742. // 表格的所有行
  743. Dispatch rows = Dispatch.get(table, "Rows" ).toDispatch();
  744. Dispatch row = Dispatch.get(rows, "First" ).toDispatch();
  745. Dispatch.call(rows, "Add" , new Variant(row));
  746. }
  747. /**
  748. * 在最后1行前增加一行
  749. *
  750. * @param tableIndex
  751. * word文档中的第N张表(从1开始)
  752. */
  753. public void addLastTableRow( int tableIndex) {
  754. // 所有表格
  755. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  756. // 要填充的表格
  757. Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
  758. .toDispatch();
  759. // 表格的所有行
  760. Dispatch rows = Dispatch.get(table, "Rows" ).toDispatch();
  761. Dispatch row = Dispatch.get(rows, "Last" ).toDispatch();
  762. Dispatch.call(rows, "Add" , new Variant(row));
  763. }
  764. /**
  765. * 增加一行
  766. *
  767. * @param tableIndex
  768. * word文档中的第N张表(从1开始)
  769. */
  770. public void addRow( int tableIndex) {
  771. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  772. // 要填充的表格
  773. Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
  774. .toDispatch();
  775. // 表格的所有行
  776. Dispatch rows = Dispatch.get(table, "Rows" ).toDispatch();
  777. Dispatch.call(rows, "Add" );
  778. }
  779. /**
  780. * 增加一列
  781. *
  782. * @param tableIndex
  783. * word文档中的第N张表(从1开始)
  784. */
  785. public void addCol( int tableIndex) {
  786. // 所有表格
  787. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  788. // 要填充的表格
  789. Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
  790. .toDispatch();
  791. // 表格的所有行
  792. Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
  793. Dispatch.call(cols, "Add" ).toDispatch();
  794. Dispatch.call(cols, "AutoFit" );
  795. }
  796. /**
  797. * 在指定列前面增加表格的列
  798. *
  799. * @param tableIndex
  800. * word文档中的第N张表(从1开始)
  801. * @param colIndex
  802. * 制定列的序号 (从1开始)
  803. */
  804. public void addTableCol( int tableIndex, int colIndex) {
  805. // 所有表格
  806. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  807. // 要填充的表格
  808. Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
  809. .toDispatch();
  810. // 表格的所有行
  811. Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
  812. System.out.println(Dispatch.get(cols, "Count" ));
  813. Dispatch col = Dispatch.call(cols, "Item" , new Variant(colIndex))
  814. .toDispatch();
  815. // Dispatch col = Dispatch.get(cols, "First").toDispatch();
  816. Dispatch.call(cols, "Add" , col).toDispatch();
  817. Dispatch.call(cols, "AutoFit" );
  818. }
  819. /**
  820. * 在第1列前增加一列
  821. *
  822. * @param tableIndex
  823. * word文档中的第N张表(从1开始)
  824. */
  825. public void addFirstTableCol( int tableIndex) {
  826. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  827. // 要填充的表格
  828. Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
  829. .toDispatch();
  830. // 表格的所有行
  831. Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
  832. Dispatch col = Dispatch.get(cols, "First" ).toDispatch();
  833. Dispatch.call(cols, "Add" , col).toDispatch();
  834. Dispatch.call(cols, "AutoFit" );
  835. }
  836. /**
  837. * 在最后一列前增加一列
  838. *
  839. * @param tableIndex
  840. * word文档中的第N张表(从1开始)
  841. */
  842. public void addLastTableCol( int tableIndex) {
  843. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  844. // 要填充的表格
  845. Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex))
  846. .toDispatch();
  847. // 表格的所有行
  848. Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
  849. Dispatch col = Dispatch.get(cols, "Last" ).toDispatch();
  850. Dispatch.call(cols, "Add" , col).toDispatch();
  851. Dispatch.call(cols, "AutoFit" );
  852. }
  853. /**
  854. * 从选定内容或插入点开始查找文本
  855. *
  856. * @param toFindText
  857. * 要查找的文本
  858. * @return boolean true-查找到并选中该文本,false-未查找到文本
  859. */
  860. public boolean find(String toFindText) {
  861. if (toFindText == null || toFindText.equals( "" ))
  862. return false ;
  863. // 从selection所在位置开始查询
  864. Dispatch find = word.call(selection, "Find" ).toDispatch();
  865. // 设置要查找的内容
  866. Dispatch.put(find, "Text" , toFindText);
  867. // 向前查找
  868. Dispatch.put(find, "Forward" , "True" );
  869. // 设置格式
  870. Dispatch.put(find, "Format" , "True" );
  871. // 大小写匹配
  872. Dispatch.put(find, "MatchCase" , "True" );
  873. // 全字匹配
  874. Dispatch.put(find, "MatchWholeWord" , "True" );
  875. // 查找并选中
  876. return Dispatch.call(find, "Execute" ).getBoolean();
  877. }
  878. /**
  879. * 把选定选定内容设定为替换文本
  880. *
  881. * @param toFindText
  882. * 查找字符串
  883. * @param newText
  884. * 要替换的内容
  885. * @return
  886. */
  887. public boolean replaceText(String toFindText, String newText) {
  888. if (!find(toFindText))
  889. return false ;
  890. Dispatch.put(selection, "Text" , newText);
  891. return true ;
  892. }
  893. %E
  894. /**
  895. * 全局替换文本
  896. *
  897. * @param toFindText
  898. * 查找字符串
  899. * @param newText
  900. * 要替换的内容
  901. */
  902. public void replaceAllText(String toFindText, String newText) {
  903. while (find(toFindText)) {
  904. Dispatch.put(selection, "Text" , newText);
  905. Dispatch.call(selection, "MoveRight" );
  906. }
  907. }
  908. /**
  909. *
  910. * @param toFindText
  911. * 要查找的字符串
  912. * @param imagePath
  913. * 图片路径
  914. * @return 此函数将字符串替换成图片
  915. */
  916. public boolean replaceImage(String toFindText, String imagePath) {
  917. if (!find(toFindText))
  918. return false ;
  919. Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
  920. "AddPicture" , imagePath);
  921. return true ;
  922. }
  923. /**
  924. * 全局替换图片
  925. *
  926. * @param toFindText
  927. * 查找字符串
  928. * @param imagePath
  929. * 图片路径
  930. */
  931. public void replaceAllImage(String toFindText, String imagePath) {
  932. while (find(toFindText)) {
  933. Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(),
  934. "AddPicture" , imagePath);
  935. Dispatch.call(selection, "MoveRight" );
  936. }
  937. }
  938. // ////////////////////////////////////////////////////////
  939. // //////////////////////////////////////////////////////////
  940. // /////////////////////////////////////////////////////
  941. // ///////////////////////////////////////////////////
  942. // //////////////////////////////////////////////////
  943. /**
  944. * 设置当前表格线的粗细 w范围:1<w<13 超过范围设为:w=6
  945. *
  946. * @param w
  947. */
  948. /*
  949. * private void setTableBorderWidth(int w) { if (w > 13 || w < 2) { w = 6; }
  950. * Dispatch borders = Dispatch.get(table, "Borders").toDispatch(); Dispatch
  951. * border = null;
  952. *
  953. * /** 设置表格线的粗细 1:代表最上边一条线 2:代表最左边一条线 3:最下边一条线 4:最右边一条线 5:除最上边最下边之外的所有横线
  954. * 6:除最左边最右边之外的所有竖线 7:从左上角到右下角的斜线 8:从左下角到右上角的斜线
  955. */
  956. /*
  957. * for (int i = 1; i < 7; i++) { border = Dispatch.call(borders, "Item", new
  958. * Variant(i)) .toDispatch(); Dispatch.put(border, "LineWidth", new
  959. * Variant(w)); Dispatch.put(border, "Visible", new Variant(true)); } }
  960. *
  961. *
  962. *
  963. * /** 复制表的最后一行到剪切板
  964. *
  965. * @param tableIndex
  966. */
  967. /*
  968. * public void copyLastRow(int tableIndex) {
  969. * getRow(getRowsCount(tableIndex)); Dispatch.call(row, "select");
  970. * Dispatch.call(selection, "Copy"); }
  971. *
  972. * /** 复制表的最后一行并粘贴到下一行(包括行中的数据)
  973. *
  974. * @param tableIndex 表的索引 @param times 粘贴的次数
  975. */
  976. /*
  977. * public void duplicateLastRow(int tableIndex, int times) {
  978. * this.copyLastRow(tableIndex); for (int i = 0; i < times; i++) {
  979. * Dispatch.call(selection, "Paste"); } }
  980. *
  981. * /** 查找当前行表格所有行中的某一行
  982. *
  983. * @param rowIndex @return
  984. */
  985. public Dispatch getRow( int rowIndex) {
  986. if (rows == null )
  987. this .getRows();
  988. row = Dispatch.invoke(rows, "item" , Dispatch.Method,
  989. new Object[] { new Integer(rowIndex) }, new int [ 1 ])
  990. .toDispatch();
  991. return row;
  992. }
  993. public int getRowsCount() {
  994. if (rows == null )
  995. this .getRows();
  996. return Dispatch.get(rows, "Count" ).getInt();
  997. }
  998. /**
  999. * 得到当前表格的所有的列
  1000. *
  1001. * @return
  1002. */
  1003. // 需要找到Dispatch对象,这里的Variant(1)不行一定要做成变量
  1004. public Dispatch getColumns() {
  1005. // this.getTables();
  1006. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  1007. Dispatch table = Dispatch.call(tables, "Item" , new Variant( 1 ))
  1008. .toDispatch();
  1009. return this .columns = Dispatch.get(table, "Columns").toDispatch();
  1010. }
  1011. /**
  1012. * 得到当前表格的某一列
  1013. *
  1014. * @param index
  1015. * 列索引
  1016. * @return
  1017. */
  1018. public Dispatch getColumn( int columnIndex) {
  1019. if (columns == null )
  1020. this .getColumns();
  1021. return this .column = Dispatch.call(columns, "Item" ,
  1022. new Variant(columnIndex)).toDispatch();
  1023. }
  1024. /**
  1025. * 得到当前表格的列数
  1026. *
  1027. * @return
  1028. */
  1029. public int getColumnsCount() {
  1030. this .getColumns();
  1031. return Dispatch.get(columns, "Count" ).toInt();
  1032. }
  1033. /**
  1034. * 得到指定表格的列数
  1035. *
  1036. * @param tableIndex
  1037. * @return
  1038. */
  1039. public int getColumnsCount( int tableIndex) {
  1040. this .getTable(tableIndex);
  1041. return this .getColumnsCount();
  1042. }
  1043. /**
  1044. * 得到表的行数
  1045. *
  1046. * @param tableIndex
  1047. * @return
  1048. */
  1049. public int getRowsCount( int tableIndex) {
  1050. this .getTable(tableIndex);
  1051. return this .getRowsCount();
  1052. }
  1053. /**
  1054. * 设置当前表格的所有行的行高
  1055. *
  1056. * @param rowHeight
  1057. */
  1058. public void setRowHeight( float rowHeight) {
  1059. if (rowHeight > 0 ) {
  1060. if (rows == null )
  1061. this .getRows();
  1062. Dispatch.put(rows, "Height" , new Variant(rowHeight));
  1063. }
  1064. }
  1065. /**
  1066. * 设置指定表格的所有行的行高
  1067. *
  1068. * @param tableIndex
  1069. * @param rowHeight
  1070. */
  1071. public void setRowHeight( int tableIndex, float rowHeight) {
  1072. this .getRows(tableIndex);
  1073. this .setRowHeight(rowHeight);
  1074. }
  1075. /**
  1076. * 设置当前表格指定行的行高
  1077. *
  1078. * @param rowHeight
  1079. * @param rowIndex
  1080. */
  1081. public void setRowHeight( float rowHeight, int rowIndex) {
  1082. if (rowHeight > 0 ) {
  1083. if (rows == null || row == null ) {
  1084. this .getRows();
  1085. this .getRow(rowIndex);
  1086. }
  1087. Dispatch.put(row, "Height" , new Variant(rowHeight));
  1088. }
  1089. }
  1090. /**
  1091. * 设置指定表格的指定行的行高
  1092. *
  1093. * @param tableIndex
  1094. * @param rowHeight
  1095. * @param rowIndex
  1096. */
  1097. public void setRowHeight( int tableIndex, float rowHeight, int rowIndex) {
  1098. this .getTable(tableIndex);
  1099. this .setRowHeight(rowHeight, rowIndex);
  1100. }
  1101. /**
  1102. * 设置当前表格的所有列的列宽
  1103. *
  1104. * @param columnWidth
  1105. * 列宽 取值范围:10<columnWidth 默认值:120
  1106. */
  1107. public void setColumnWidth( float columnWidth) {
  1108. if (columnWidth < 11 ) {
  1109. columnWidth = 120 ;
  1110. }
  1111. if (columns == null )
  1112. this .getColumns();
  1113. Dispatch.put(columns, "Width" , new Variant(columnWidth));
  1114. }
  1115. /**
  1116. * 设置指定表格的所有列的列宽
  1117. *
  1118. * @param tableIndex
  1119. * @param columnWidth
  1120. */
  1121. public void setColumnWidth( int tableIndex, float columnWidth) {
  1122. this .getColumns(tableIndex);
  1123. this .setColumnWidth(columnWidth);
  1124. }
  1125. /**
  1126. * 得到指定表格的多有列
  1127. *
  1128. * @param tableIndex
  1129. * @return
  1130. */
  1131. public Dispatch getColumns( int tableIndex) {
  1132. getTable(tableIndex);
  1133. return this .getColumns();
  1134. }
  1135. /**
  1136. * 复制表的某一行
  1137. *
  1138. * @param tableIndex
  1139. * @param rowIndex
  1140. */
  1141. public void copyRow( int tableIndex, int rowIndex) {
  1142. getTable(tableIndex);
  1143. getRows();
  1144. row = getRow(rowIndex);
  1145. Dispatch.call(row, "Select" );
  1146. Dispatch.call(selection, "Copy" );
  1147. }
  1148. /**
  1149. * 查找表的全部行
  1150. *
  1151. * @param tableIndex
  1152. * @return
  1153. */
  1154. public Dispatch getRows( int tableIndex) {
  1155. getTable(tableIndex);
  1156. return this .getRows();
  1157. }
  1158. /**
  1159. * 查找当前表的全部行
  1160. *
  1161. * @return
  1162. *
  1163. *
  1164. */
  1165. // 需要找到Dispatch对象,这里的Variant(1)不行一定要做成变量
  1166. public Dispatch getRows() {
  1167. Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
  1168. Dispatch table = Dispatch.call(tables, "Item" , new Variant( 2 ))
  1169. .toDispatch();
  1170. rows = Dispatch.get(table, "rows" ).toDispatch();
  1171. return rows;
  1172. }
  1173. /**
  1174. * 查找指定表格的单元格
  1175. *
  1176. * @param tableIndex
  1177. * @param cellRow
  1178. * @param cellColumn
  1179. * @return
  1180. */
  1181. public Dispatch getCell( int tableIndex, int cellRow, int cellColumn) {
  1182. getTable(tableIndex);
  1183. return getCell(cellRow, cellColumn);
  1184. }
  1185. /**
  1186. * 查找当前所在表的某单元格
  1187. *
  1188. * @param cellRow
  1189. * @param cellColumn
  1190. * @return
  1191. * @throws Dispatch
  1192. * object expected
  1193. */
  1194. public Dispatch getCell( int cellRow, int cellColumn) {
  1195. return cell = Dispatch.call(table, "Cell" , new Variant(cellRow),
  1196. new Variant(cellColumn)).toDispatch();
  1197. }
  1198. /**
  1199. * 保存文档并退出程序
  1200. *
  1201. * @param fileName
  1202. * 保存的文件名称
  1203. * @param isSave
  1204. * 是否保存修改
  1205. * @throws Exception
  1206. */
  1207. /*
  1208. * public void saveDocAndExit(File fileName, boolean isSave) throws
  1209. * Exception { if (fileName != null) { if (!fileName.exists()) {
  1210. * fileName.createNewFile(); } Dispatch wordBasic = (Dispatch)
  1211. * Dispatch.call(word, "WordBasic").getDispatch();
  1212. * Dispatch.invoke(wordBasic, "FileSaveAs", Dispatch.Method, new Object[] {
  1213. * fileName.getPath(), new Variant(true), new Variant(false) }, new int[1]); }
  1214. *
  1215. * Dispatch.call(document, "Close", new Variant(isSave));
  1216. * Dispatch.call(word, "Quit");
  1217. *
  1218. * word = null; documents = null; document = null; selection = null; }
  1219. */
  1220. /**
  1221. * 合并当前表格指定的单元格 如果需要一次合并几个单元格只需要指出第一个单元格和最后一个单元格
  1222. *
  1223. * @param fstCellRowIndex
  1224. * 第一个单元格的行索引
  1225. * @param fstCellColIndex
  1226. * 第一个单元格的列索引
  1227. * @param secCellRowIndex
  1228. * 第二个单元格的行索引
  1229. * @param secCellColIndex
  1230. * 第二个单元格的列索引
  1231. */
  1232. public void mergeCell( int fstCellRowIndex, int fstCellColIndex,
  1233. int secCellRowIndex, int secCellColIndex) {
  1234. Dispatch fstCell = Dispatch.call(table, "Cell" ,
  1235. new Variant(fstCellRowIndex), new Variant(fstCellColIndex))
  1236. .toDispatch();
  1237. Dispatch secCell = Dispatch.call(table, "Cell" ,
  1238. new Variant(secCellRowIndex), new Variant(secCellColIndex))
  1239. .toDispatch();
  1240. Dispatch.call(fstCell, "Merge" , secCell);
  1241. }
  1242. /**
  1243. * 合并当前表格指定的列
  1244. *
  1245. * @param columnIndex
  1246. * 列索引
  1247. */
  1248. public void mergeColumn( int columnIndex) {
  1249. this .getColumn(columnIndex);
  1250. Dispatch cells = Dispatch.get(column, "Cells").toDispatch();
  1251. Dispatch.call(cells, "Merge" );
  1252. }
  1253. /**
  1254. * 合并当前表格的指定的行
  1255. *
  1256. * @param rowIndex
  1257. */
  1258. public void mergeRow( int rowIndex) {
  1259. this .getRow(rowIndex);
  1260. Dispatch cells = Dispatch.get(row, "Cells" ).toDispatch();
  1261. Dispatch.call(cells, "Merge" );
  1262. }
  1263. /**
  1264. * 合并指定表格的指定的行
  1265. *
  1266. * @param tableIndex
  1267. * @param rowIndex
  1268. * 行索引
  1269. */
  1270. public void mergeRow( int tableIndex, int rowIndex) {
  1271. this .getTable(tableIndex);
  1272. this .mergeRow(rowIndex);
  1273. }
  1274. /**
  1275. * 合并指定表格的指定的列
  1276. *
  1277. * @param tableIndex
  1278. * @param columnIndex
  1279. */
  1280. public void mergeColumn( int tableIndex, int columnIndex) {
  1281. this .getTable(tableIndex);
  1282. this .mergeColumn(columnIndex);
  1283. }
  1284. /**
  1285. * 合并指定表格的指定的单元格
  1286. *
  1287. * @param tableIndex
  1288. * @param fstCellRowIndex
  1289. * @param fstCellColIndex
  1290. * @param secCellRowIndex
  1291. * @param secCellColIndex
  1292. */
  1293. public void mergeCell( int tableIndex, int fstCellRowIndex,
  1294. int fstCellColIndex, int secCellRowIndex, int secCellColIndex) {
  1295. this .getTable(tableIndex);
  1296. this.mergeCell(fstCellRowIndex, fstCellColIndex, secCellRowIndex,
  1297. secCellColIndex);
  1298. }
  1299. public Dispatch getRangeParagraphs() throws Exception {
  1300. return Dispatch.get(range, "Paragraphs" ).toDispatch();
  1301. }
  1302. public Dispatch getParagraph( int tIndex) throws Exception {
  1303. return Dispatch.call(paragraphs, "Item" , new Variant(tIndex))
  1304. .toDispatch();
  1305. }
  1306. public Dispatch getParagraphRange() throws Exception {
  1307. return Dispatch.get(paragraph, "range" ).toDispatch();
  1308. }
  1309. public String getRangeText() throws Exception {
  1310. return Dispatch.get(range, "Text" ).toString();
  1311. }
  1312. public int getParagraphsCount() throws Exception {
  1313. int count = 0 ;
  1314. count = Dispatch.get(paragraphs, "Count" ).toInt();
  1315. return count;
  1316. }
  1317. public static void main(String[] args) {
  1318. long time1 = System.currentTimeMillis();
  1319. int i = 0 ;
  1320. ComThread.InitSTA(); // 初始化com的线程,非常重要!!使用结束后要调用 realease方法
  1321. // Instantiate objWord //Declare word object
  1322. ActiveXComponent objWord = new ActiveXComponent("Word.Application" );
  1323. // Assign a local word object
  1324. Dispatch wordObject = (Dispatch) objWord.getObject();
  1325. // Create a Dispatch Parameter to show the document that is opened
  1326. Dispatch.put((Dispatch) wordObject, "Visible" , new Variant( true )); // new
  1327. // Variant(true)表示word应用程序可见
  1328. // Instantiate the Documents Property
  1329. Dispatch documents = objWord.getProperty( "Documents").toDispatch(); // documents表示word的所有文档窗口,(word是多文档应用程序)
  1330. // Add a new word document, Current Active Document
  1331. // Dispatch document = Dispatch.call(documents, "Add").toDispatch(); //
  1332. // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档
  1333. for ( int n = 0 ; n <= 10 ; n++) {
  1334. Dispatch document = Dispatch.call(documents, "Open" , "C://ABC.doc" )
  1335. .toDispatch();
  1336. Dispatch wordContent = Dispatch.get(document, "Content")
  1337. .toDispatch(); // 取得word文件的内容
  1338. Dispatch.call(wordContent, "InsertAfter" , "这里是一个段落的内容" ); // 插入一个段落
  1339. Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs" )
  1340. .toDispatch(); // 所有段落
  1341. int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数
  1342. // 找到刚输入的段落,设置格式
  1343. Dispatch lastParagraph = Dispatch.call(paragraphs, "Item" ,
  1344. new Variant(paragraphCount)).toDispatch(); // 最后一段
  1345. Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range" )
  1346. .toDispatch();
  1347. Dispatch font = Dispatch.get(lastParagraphRange, "Font")
  1348. .toDispatch();
  1349. Dispatch.put(font, "Bold" , new Variant( true )); // 设置为黑体
  1350. Dispatch.put(font, "Italic" , new Variant( true )); // 设置为斜体
  1351. Dispatch.put(font, "Name" , new Variant( "宋体" )); //
  1352. Dispatch.put(font, "Size" , new Variant( 18 )); // 小四
  1353. Dispatch frames = Dispatch.call(wordContent, "Frames").toDispatch();
  1354. int frameCount = Dispatch.call(frames, "Count").toInt();
  1355. System.out.println( "" + frameCount + n + "/n" );
  1356. Dispatch.call(document, "SaveAs" , new Variant("C://" + (i++)+ " abc.doc")); // 保存一个新文档
  1357. Dispatch.call(document, "Close" , new Variant( true));
  1358. }
  1359. //
  1360. // end for
  1361. Dispatch.call(objWord, "Quit" );
  1362. ComThread.Release(); // 释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
  1363. long time2 = System.currentTimeMillis();
  1364. double time3 = (time2 - time1) / 1000.0 ;
  1365. System.out.println( "/n" + time3 + " 秒." );
  1366. }
  1367. }