jacob 操作word
1. 首先下载jacob-1.18.zip,解压后有两个文件jacob.jar 和 jacob.dll。需要把jacob.jar放到你工程的classpath中并且把jacob.dll放到jdk的bin目录下(D:\Program Files\Java\jdk1.8.0_101\bin)目录下或者系统的system32其他相应的目录下。
放到jdk/bin目录下,时常会出现jacob.dll already loaded in another classloader异常,于是,将jacob.dll文件放到了tomcat/bin目录下,目前还未发现问题......
2.下面是提供的工具类
1 package com.erqiao.rc.util;
2
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 /** */
9 /***
10 *
11 * @author BruceLeey
12 *
13 */
14 public class MSWordManager {
15 // word文档
16 private Dispatch doc = null;
17 // word运行程序对象
18 private ActiveXComponent word = null;
19 // 所有word文档集合
20 private Dispatch documents = null;
21 // 选定的范围或插入点
22 private static Dispatch selection = null;
23 // 设置是否保存后才退出的标志
24 private boolean saveOnExit = true;
25
26 public MSWordManager() {
27 ComThread.InitSTA();
28 if (word == null) {
29 word = new ActiveXComponent("Word.Application");
30 word.setProperty("Visible", new Variant(false));
31 }
32 if (documents == null)
33 documents = word.getProperty("Documents").toDispatch();
34 }
35
36 /** */
37 /**
38 * 设置退出时参数
39 *
40 * @param saveOnExit
41 * boolean true-退出时保存文件,false-退出时不保存文件
42 */
43 public void setSaveOnExit(boolean saveOnExit) {
44 this.saveOnExit = saveOnExit;
45 }
46
47 /** */
48 /**
49 * 创建一个新的word文档
50 *
51 */
52 public void createNewDocument() {
53 doc = Dispatch.call(documents, "Add").toDispatch();
54 selection = Dispatch.get(word, "Selection").toDispatch();
55 }
56
57 /** */
58 /**
59 * 打开一个已存在的文档
60 *
61 * @param docPath
62 */
63 public void openDocument(String docPath) {
64 closeDocument();
65 doc = Dispatch.call(documents, "Open", docPath).toDispatch();
66 selection = Dispatch.get(word, "Selection").toDispatch();
67 }
68
69 /** */
70 /**
71 * 把选定的内容或插入点向上移动
72 *
73 * @param pos
74 * 移动的距离
75 */
76 public void moveUp(int pos) {
77 if (selection == null)
78 selection = Dispatch.get(word, "Selection").toDispatch();
79 for (int i = 0; i < pos; i++)
80 Dispatch.call(selection, "MoveUp");
81
82 }
83
84 /** */
85 /**
86 * 在指定的单元格里填写数据
87 *
88 * @param tableIndex
89 * 文档中的第tIndex个Table,即tIndex为索引取
90 * @param cellRowIdx
91 * cell在Table第row行
92 * @param cellColIdx
93 * cell在Talbe第col列
94 * @param txt
95 * 填写的数据
96 */
97 public void moveToCell(int tableIndex, int cellRowIdx, int cellColIdx) {
98 // 所有表格
99 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
100 // 要填充的表格
101 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
102 Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx), new Variant(cellColIdx)).toDispatch();
103 Dispatch.call(cell, "Select");
104 Dispatch.call(selection, "MoveRight");
105 }
106
107 /**
108 * 插入分节符
109 */
110 public void InsertBreakNextPage(){
111 Dispatch.call(word, "Run", new Variant("InsertBreakWdSectionBreakNextPage"));
112 }
113
114 /**
115 * 新增分页符
116 */
117 public void InsertBreak(){
118 Dispatch.call(selection, "InsertBreak" , new Variant(7) );
119 }
120
121 /**
122 * 换行符
123 */
124 public void Enter(){
125 Dispatch.call(selection, "TypeParagraph");
126 }
127
128 /** */
129 /**
130 * 把选定的内容或者插入点向下移动
131 *
132 * @param pos
133 * 移动的距离
134 */
135 public void moveDown(int pos) {
136 if (selection == null)
137 selection = Dispatch.get(word, "Selection").toDispatch();
138 for (int i = 0; i < pos; i++)
139 Dispatch.call(selection, "MoveDown");
140 }
141
142 /** */
143 /**
144 * 把选定的内容或者插入点向左移动
145 *
146 * @param pos
147 * 移动的距离
148 */
149 public void moveLeft(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, "MoveLeft");
154 }
155 }
156
157 /** */
158 /**
159 * 把选定的内容或者插入点向右移动
160 *
161 * @param pos
162 * 移动的距离
163 */
164 public void moveRight(int pos) {
165 if (selection == null)
166 selection = Dispatch.get(word, "Selection").toDispatch();
167 for (int i = 0; i < pos; i++)
168 Dispatch.call(selection, "MoveRight");
169 }
170
171 /** */
172 /**
173 * 把插入点移动到文件首位置
174 *
175 */
176 public void moveStart() {
177 if (selection == null)
178 selection = Dispatch.get(word, "Selection").toDispatch();
179 Dispatch.call(selection, "HomeKey", new Variant(6));
180 }
181
182 /** */
183 /**
184 * 从选定内容或插入点开始查找文本
185 *
186 * @param toFindText
187 * 要查找的文本
188 * @return boolean true-查找到并选中该文本,false-未查找到文本
189 */
190 public boolean find(String toFindText) {
191 if (toFindText == null || toFindText.equals(""))
192 return false;
193 // 从selection所在位置开始查询
194 Dispatch find = word.call(selection, "Find").toDispatch();
195 // 设置要查找的内容
196 Dispatch.put(find, "Text", toFindText);
197 // 向前查找
198 Dispatch.put(find, "Forward", "True");
199 // 设置格式
200 Dispatch.put(find, "Format", "True");
201 // 大小写匹配
202 Dispatch.put(find, "MatchCase", "True");
203 // 全字匹配
204 Dispatch.put(find, "MatchWholeWord", "True");
205 // 查找并选中
206 return Dispatch.call(find, "Execute").getBoolean();
207 }
208
209 /** */
210 /**
211 * 把选定选定内容设定为替换文本
212 *
213 * @param toFindText
214 * 查找字符串
215 * @param newText
216 * 要替换的内容
217 * @return
218 */
219 public boolean replaceText(String toFindText, String newText) {
220 if (!find(toFindText))
221 return false;
222 Dispatch.put(selection, "Text", newText);
223 return true;
224 }
225
226 /** */
227 /**
228 * 全局替换文本
229 *
230 * @param toFindText
231 * 查找字符串
232 * @param newText
233 * 要替换的内容
234 */
235 public void replaceAllText(String toFindText, String newText) {
236 while (find(toFindText)) {
237 Dispatch.put(selection, "Text", newText);
238 Dispatch.call(selection, "MoveRight");
239 }
240 }
241
242 /** */
243 /**
244 * 在当前插入点插入字符串
245 *
246 * @param newText
247 * 要插入的新字符串
248 */
249 public void insertText(String newText) {
250 Dispatch.put(selection, "Text", newText);
251 }
252
253 /** */
254 /**
255 *
256 * @param toFindText
257 * 要查找的字符串
258 * @param imagePath
259 * 图片路径
260 * @return
261 */
262 public boolean replaceImage(String toFindText, String imagePath) {
263 if (!find(toFindText))
264 return false;
265 Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(), "AddPicture", imagePath);
266 return true;
267 }
268
269 /** */
270 /**
271 * 全局替换图片
272 *
273 * @param toFindText
274 * 查找字符串
275 * @param imagePath
276 * 图片路径
277 */
278 public void replaceAllImage(String toFindText, String imagePath) {
279 while (find(toFindText)) {
280 Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(), "AddPicture", imagePath);
281 Dispatch.call(selection, "MoveRight");
282 }
283 }
284
285 /** */
286 /**
287 * 在当前插入点插入图片
288 *
289 * @param imagePath
290 * 图片路径
291 */
292 public void insertImage(String imagePath) {
293 if (imagePath != "" && imagePath != null) {
294 Dispatch.call(Dispatch.get(selection, "InLineShapes").toDispatch(), "AddPicture", imagePath);
295 }
296 }
297
298 /** */
299 /**
300 * 合并单元格
301 *
302 * @param tableIndex
303 * @param fstCellRowIdx
304 * @param fstCellColIdx
305 * @param secCellRowIdx
306 * @param secCellColIdx
307 */
308 public void mergeCell(int tableIndex, int fstCellRowIdx, int fstCellColIdx, int secCellRowIdx, int secCellColIdx) {
309 // 所有表格
310 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
311 // 要填充的表格
312 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
313 Dispatch fstCell = Dispatch.call(table, "Cell", new Variant(fstCellRowIdx), new Variant(fstCellColIdx))
314 .toDispatch();
315 Dispatch secCell = Dispatch.call(table, "Cell", new Variant(secCellRowIdx), new Variant(secCellColIdx))
316 .toDispatch();
317 Dispatch.call(fstCell, "Merge", secCell);
318 }
319
320 /** */
321 /**
322 * 在指定的单元格里填写数据
323 *
324 * @param tableIndex
325 * 文档中的第tIndex个Table,即tIndex为索引取
326 * @param cellRowIdx
327 * cell在Table第row行
328 * @param cellColIdx
329 * cell在Talbe第col列
330 * @param txt
331 * 填写的数据
332 */
333 public void putTxtToCell(int tableIndex, int cellRowIdx, int cellColIdx, String txt) {
334 // 所有表格
335 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
336 // 要填充的表格
337 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
338 Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx), new Variant(cellColIdx)).toDispatch();
339 Dispatch.call(cell, "Select");
340 Dispatch.put(selection, "Text", txt);
341 }
342
343 /** */
344 /**
345 * 在指定的单元格里填写数据
346 *
347 * @param tableIndex
348 * @param cellRowIdx
349 * @param cellColIdx
350 * @param txt
351 */
352 public void putTxtToCellCenter(int tableIndex, int cellRowIdx, int cellColIdx, String txt) {
353 // 所有表格
354 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
355 // 要填充的表格
356 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
357 Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx), new Variant(cellColIdx)).toDispatch();
358 Dispatch.call(cell, "Select");
359 Dispatch alignment = Dispatch.get(selection, "ParagraphFormat").toDispatch();
360 Dispatch.put(alignment, "Alignment", "3");
361 Dispatch.put(selection, "Text", txt);
362 }
363
364 /**
365 *
366 * 得到当前文档的tables集合
367 */
368 public Dispatch getTables() throws Exception {
369 if (this.doc == null) {
370 throw new Exception("there is not a document can't be operate!!!");
371 }
372 return Dispatch.get(doc, "Tables").toDispatch();
373 }
374
375 /**
376 *
377 * 得到当前文档的表格数
378 *
379 * @param Dispatch
380 */
381 public int getTablesCount(Dispatch tables) throws Exception {
382 int count = 0;
383 try {
384 this.getTables();
385 } catch (Exception e) {
386 throw new Exception("there is not any table!!");
387 }
388 count = Dispatch.get(tables, "Count").getInt();
389 return count;
390 }
391
392 /** */
393 /**
394 * 在当前文档拷贝剪贴板数据
395 *
396 * @param pos
397 */
398 public void pasteExcelSheet(String pos) {
399 moveStart();
400 if (this.find(pos)) {
401 Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
402 Dispatch.call(textRange, "Paste");
403 }
404 }
405
406 /** */
407 /**
408 * 在当前文档指定的位置拷贝表格
409 *
410 * @param pos
411 * 当前文档指定的位置
412 * @param tableIndex
413 * 被拷贝的表格在word文档中所处的位置
414 */
415 public void copyTable(/*String pos, */int tableIndex) {
416 // 所有表格
417 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
418 // 要填充的表格
419 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
420 Dispatch range = Dispatch.get(table, "Range").toDispatch();
421 Dispatch.call(range, "Copy");
422 Dispatch.call(selection, "Paste");
423 // if (this.find(pos)) {
424 // Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
425 // Dispatch.call(textRange, "Paste");
426 // }
427 }
428
429 /** */
430 /**
431 * 在当前文档指定的位置拷贝来自另一个文档中的表格
432 *
433 * @param anotherDocPath
434 * 另一个文档的磁盘路径
435 * @param tableIndex
436 * 被拷贝的表格在另一格文档中的位置
437 * @param pos
438 * 当前文档指定的位置
439 */
440 public void copyTableFromAnotherDoc(String anotherDocPath, int tableIndex, String pos) {
441 Dispatch doc2 = null;
442 try {
443 doc2 = Dispatch.call(documents, "Open", anotherDocPath).toDispatch();
444 // 所有表格
445 Dispatch tables = Dispatch.get(doc2, "Tables").toDispatch();
446 // 要填充的表格
447 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
448 Dispatch range = Dispatch.get(table, "Range").toDispatch();
449 Dispatch.call(range, "Copy");
450 if (this.find(pos)) {
451 Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
452 Dispatch.call(textRange, "Paste");
453 }
454 } catch (Exception e) {
455 e.printStackTrace();
456 } finally {
457 if (doc2 != null) {
458 Dispatch.call(doc2, "Close", new Variant(saveOnExit));
459 doc2 = null;
460 }
461 }
462 }
463
464 /** */
465 /**
466 * 在当前文档指定的位置拷贝来自另一个文档中的图片
467 *
468 * @param anotherDocPath
469 * 另一个文档的磁盘路径
470 * @param shapeIndex
471 * 被拷贝的图片在另一格文档中的位置
472 * @param pos
473 * 当前文档指定的位置
474 */
475 public void copyImageFromAnotherDoc(String anotherDocPath, int shapeIndex, String pos) {
476 Dispatch doc2 = null;
477 try {
478 doc2 = Dispatch.call(documents, "Open", anotherDocPath).toDispatch();
479 Dispatch shapes = Dispatch.get(doc2, "InLineShapes").toDispatch();
480 Dispatch shape = Dispatch.call(shapes, "Item", new Variant(shapeIndex)).toDispatch();
481 Dispatch imageRange = Dispatch.get(shape, "Range").toDispatch();
482 Dispatch.call(imageRange, "Copy");
483 if (this.find(pos)) {
484 Dispatch textRange = Dispatch.get(selection, "Range").toDispatch();
485 Dispatch.call(textRange, "Paste");
486 }
487 } catch (Exception e) {
488 e.printStackTrace();
489 } finally {
490 if (doc2 != null) {
491 Dispatch.call(doc2, "Close", new Variant(saveOnExit));
492 doc2 = null;
493 }
494 }
495 }
496
497 /** */
498 /**
499 * 创建表格
500 *
501 * @param pos
502 * 位置
503 * @param cols
504 * 列数
505 * @param rows
506 * 行数
507 */
508 public void createTable(String pos, int numCols, int numRows) {
509 if (find(pos)) {
510 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
511 Dispatch range = Dispatch.get(selection, "Range").toDispatch();
512 Dispatch newTable = Dispatch.call(tables, "Add", range, new Variant(numRows), new Variant(numCols))
513 .toDispatch();
514 Dispatch.call(selection, "MoveRight");
515 }
516 }
517
518 /** */
519 /**
520 * 在指定行前面增加行
521 *
522 * @param tableIndex
523 * word文件中的第N张表(从1开始)
524 * @param rowIndex
525 * 指定行的序号(从1开始)
526 */
527 public void addTableRow(int tableIndex, int rowIndex) {
528 // 所有表格
529 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
530 // 要填充的表格
531 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
532 // 表格的所有行
533 Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
534 Dispatch row = Dispatch.call(rows, "Item", new Variant(rowIndex)).toDispatch();
535 Dispatch.call(rows, "Add", new Variant(row));
536 }
537
538 /** */
539 /**
540 * 在第1行前增加一行
541 *
542 * @param tableIndex
543 * word文档中的第N张表(从1开始)
544 */
545 public void addFirstTableRow(int tableIndex) {
546 // 所有表格
547 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
548 // 要填充的表格
549 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
550 // 表格的所有行
551 Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
552 Dispatch row = Dispatch.get(rows, "First").toDispatch();
553 Dispatch.call(rows, "Add", new Variant(row));
554 }
555
556 /** */
557 /**
558 * 在最后1行前增加一行
559 *
560 * @param tableIndex
561 * word文档中的第N张表(从1开始)
562 */
563 public void addLastTableRow(int tableIndex) {
564 // 所有表格
565 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
566 // 要填充的表格
567 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
568 // 表格的所有行
569 Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
570 Dispatch row = Dispatch.get(rows, "Last").toDispatch();
571 Dispatch.call(rows, "Add", new Variant(row));
572 }
573
574 /** */
575 /**
576 * 增加一行
577 *
578 * @param tableIndex
579 * word文档中的第N张表(从1开始)
580 */
581 public void addRow(int tableIndex) {
582 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
583 // 要填充的表格
584 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
585 // 表格的所有行
586 Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
587 Dispatch.call(rows, "Add");
588 }
589
590 /** */
591 /**
592 * 增加一列
593 *
594 * @param tableIndex
595 * word文档中的第N张表(从1开始)
596 */
597 public void addCol(int tableIndex) {
598 // 所有表格
599 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
600 // 要填充的表格
601 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
602 // 表格的所有行
603 Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
604 Dispatch.call(cols, "Add").toDispatch();
605 Dispatch.call(cols, "AutoFit");
606 }
607
608 /** */
609 /**
610 * 在指定列前面增加表格的列
611 *
612 * @param tableIndex
613 * word文档中的第N张表(从1开始)
614 * @param colIndex
615 * 制定列的序号 (从1开始)
616 */
617 public void addTableCol(int tableIndex, int colIndex) {
618 // 所有表格
619 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
620 // 要填充的表格
621 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
622 // 表格的所有行
623 Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
624 System.out.println(Dispatch.get(cols, "Count"));
625 Dispatch col = Dispatch.call(cols, "Item", new Variant(colIndex)).toDispatch();
626 // Dispatch col = Dispatch.get(cols, "First").toDispatch();
627 Dispatch.call(cols, "Add", col).toDispatch();
628 Dispatch.call(cols, "AutoFit");
629 }
630
631 /** */
632 /**
633 * 在第1列前增加一列
634 *
635 * @param tableIndex
636 * word文档中的第N张表(从1开始)
637 */
638 public void addFirstTableCol(int tableIndex) {
639 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
640 // 要填充的表格
641 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
642 // 表格的所有行
643 Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
644 Dispatch col = Dispatch.get(cols, "First").toDispatch();
645 Dispatch.call(cols, "Add", col).toDispatch();
646 Dispatch.call(cols, "AutoFit");
647 }
648
649 /** */
650 /**
651 * 在最后一列前增加一列
652 *
653 * @param tableIndex
654 * word文档中的第N张表(从1开始)
655 */
656 public void addLastTableCol(int tableIndex) {
657 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
658 // 要填充的表格
659 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
660 // 表格的所有行
661 Dispatch cols = Dispatch.get(table, "Columns").toDispatch();
662 Dispatch col = Dispatch.get(cols, "Last").toDispatch();
663 Dispatch.call(cols, "Add", col).toDispatch();
664 Dispatch.call(cols, "AutoFit");
665 }
666
667 /** */
668 /**
669 * 设置当前选定内容的字体
670 *
671 * @param boldSize
672 * @param italicSize
673 * @param underLineSize
674 * 下划线
675 * @param colorSize
676 * 字体颜色
677 * @param size
678 * 字体大小
679 * @param name
680 * 字体名称
681 */
682 public void setFont(boolean bold, boolean italic, boolean underLine, String colorSize, String size, String name) {
683 Dispatch font = Dispatch.get(selection, "Font").toDispatch();
684 Dispatch.put(font, "Name", new Variant(name));
685 Dispatch.put(font, "Bold", new Variant(bold));
686 Dispatch.put(font, "Italic", new Variant(italic));
687 Dispatch.put(font, "Underline", new Variant(underLine));
688 Dispatch.put(font, "Color", colorSize);
689 Dispatch.put(font, "Size", size);
690 }
691
692 public void setFontCenter(String name) {
693 Dispatch font = Dispatch.get(selection, "Font").toDispatch();
694 Dispatch alignment = Dispatch.get(selection, "ParagraphFormat").toDispatch();
695 Dispatch.put(alignment, "Alignment", "3");
696 Dispatch.call(selection, "TypeText", name);
697 }
698
699 /** */
700 /**
701 * 文件保存或另存为
702 *
703 * @param savePath
704 * 保存或另存为路径
705 */
706 public void save(String savePath) {
707 Dispatch.call(doc, "SaveAs", savePath); // 保存
708 /**//*
709 * Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(),
710 * "FileSaveAs", savePath);
711 */
712 }
713
714 /** */
715 /**
716 * 关闭当前word文档
717 *
718 */
719 public void closeDocument() {
720 if (doc != null) {
721 Dispatch.call(doc, "Save");
722 Dispatch.call(doc, "Close", new Variant(saveOnExit));
723 doc = null;
724 }
725 }
726
727 /** */
728 /**
729 * 关闭全部应用
730 *
731 */
732 public void close() {
733 closeDocument();
734 if (word != null) {
735 Dispatch.call(word, "Quit");
736 word = null;
737 }
738 selection = null;
739 documents = null;
740 ComThread.Release();
741 }
742
743 /** */
744 /**
745 * 打印当前word文档
746 *
747 */
748 public void printFile() {
749 if (doc != null) {
750 Dispatch.call(doc, "PrintOut");
751 }
752 }
753
754 /** */
755 /**
756 * 删除一行
757 *
758 * @param tableIndex
759 * word文档中的第N张表(从1开始)
760 */
761 public void delRow(int tableIndex) {
762 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
763 // 要填充的表格
764 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
765 // 表格的所有行
766 Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
767 Object temp1 = Dispatch.get(rows, "Count");
768 String temp2 = temp1.toString();
769 int count = Integer.parseInt(temp2);
770 while (count > 1) {
771 Dispatch row = Dispatch.get(rows, "Last").toDispatch();
772 Dispatch.call(row, "Delete");
773 rows = Dispatch.get(table, "Rows").toDispatch();
774 temp1 = Dispatch.get(rows, "Count");
775 temp2 = temp1.toString();
776 count = Integer.parseInt(temp2);
777 }
778 }
779
780 public void setProp(String sName, String sValue) {
781 Dispatch props = Dispatch.get(doc, "CustomDocumentProperties").toDispatch();
782 Dispatch prop = Dispatch.call(props, "Item", sName).toDispatch();
783 String sOldVal = Dispatch.get(prop, "Value").toString();
784 if (!sOldVal.equals(sValue))
785 Dispatch.put(prop, "Value", sValue);
786 }
787
788 /** */
789 /**
790 * @param nType:
791 * 1, number; 2,bool; 3,date; 4,str;
792 */
793 public void addProp(String sName, int nType, String sValue) {
794 Dispatch props = Dispatch.get(doc, "CustomDocumentProperties").toDispatch();
795 Dispatch prop = null;
796 try {
797 prop = Dispatch.call(props, "Item", sName).toDispatch();
798 } catch (Exception e) {
799 prop = null;
800 }
801 if (prop != null)
802 return;
803 // 1, number; 2,bool; 3,date; 4,str;
804 prop = Dispatch.call(props, "Add", sName, false, nType, sValue).toDispatch();
805 Dispatch.put(prop, "Value", sValue);
806 }
807
808 public String getProp(String sName) {
809 String sValue = null;
810 Dispatch props = Dispatch.get(doc, "CustomDocumentProperties").toDispatch();
811 Dispatch prop = Dispatch.call(props, "Item", sName).toDispatch();
812
813 sValue = Dispatch.get(prop, "Value").toString();
814 @SuppressWarnings("unused")
815 String sType = Dispatch.get(prop, "Type").toString();
816
817 try {
818 Dispatch prop0 = Dispatch.call(doc, "CustomDocumentProperties", sName).toDispatch();
819 sValue = Dispatch.get(prop0, "Value").toString();
820 } catch (Exception e) {
821 e.printStackTrace();
822 }
823 return sValue;
824 }
825
826 public void fack_change() {
827 Dispatch _sel = Dispatch.call(doc, "Range", 0, 0).toDispatch();
828 Dispatch.call(_sel, "InsertBefore", "A");
829 Dispatch.call(_sel, "Select");
830 Dispatch.call(_sel, "Delete");
831 }
832
833 /**
834 * 从第tIndex个Table中取出值第row行,第col列的值
835 *
836 * @param tableIndex
837 * 文档中的第tIndex个Table,即tIndex为索引取
838 * @param cellRowIdx
839 * cell在Table第row行
840 * @param cellColIdx
841 * cell在Talbe第col列
842 * @return cell单元值
843 * @throws Exception
844 */
845 public String getCellString(int tableIndex, int cellRowIdx, int cellColIdx) throws Exception {
846 // 所有表格
847 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
848 // 要取数据的表格
849 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
850 Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx), new Variant(cellColIdx)).toDispatch();
851 Dispatch.call(cell, "Select");
852 return Dispatch.get(selection, "Text").toString();
853 }
854
855 /**
856 * 从第tableIndex个Table中取出值第cellRowIdx行,第cellColIdx列的值
857 *
858 * @param tIndex
859 * 文档中的第tIndex个Table,即tIndex为索引取
860 * @param cellRowIdx
861 * cell在Table第row行
862 * @param cellColIdx
863 * cell在Talbe第col列
864 * @return cell单元值
865 * @throws Exception
866 */
867 public void getCellValue(int tableIndex, int cellRowIdx, int cellColIdx) throws Exception {
868
869 // 所有表格
870 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch();
871 // 要取数据的表格
872 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex)).toDispatch();
873 Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx), new Variant(cellColIdx)).toDispatch();
874 Dispatch.call(cell, "Select");
875 Dispatch.call(selection, "Copy");
876
877 }
878
879
880 }
3. 这里比较关键
需要调用宏来写代码,才能实现更多的功能
调用word宏
第一步,录制宏
在d盘根目录下(文档存放在哪里没有要求)新建一个word文档,名为test1.doc,打开,然后录制一段宏(具体录制哪类宏自便,调用时无需传参数即可,但是宏的保存方式要选择“所有文档(Normal)”,这样任何文档都可以调用这个宏了),宏名为macro1。
第二步,将test1.doc中宏macro1产生的影响撤销(比如那段宏是输入一段文字,那么就把这段文字删除)并保存,以便观察测试。
第三步,编写java调用代码
1 ActiveXComponent word=new ActiveXComponent("Word.Application");
2 Dispatch documents = word.getProperty("Documents").toDispatch();
3 Dispatch document = Dispatch.call(documents, "Open", "d:/test1.doc").toDispatch();//指定要打开的文档并且打开它
4 Dispatch.call(word, "Run", new Variant("macro1"));//在这个文档上运行宏
第四步,执行这段java代码
执行完成之后,可以发现被撤销的宏影响又回来了,说明宏调用成功。
第五步,高级特性
在相同目录下(文档存放目录没有规定)新建一个空白的word文档test2.doc,然后将以上代码修改为:
1 ActiveXComponent word=new ActiveXComponent("Word.Application");
2 Dispatch documents = word.getProperty("Documents").toDispatch();
3 Dispatch document = Dispatch.call(documents, "Open", "d:/test2.doc").toDispatch();//指定要打开的文档并且打开它
4 Dispatch.call(word, "Run", new Variant("macro1"));//在这个文档上运行宏
执行以上代码,可以发现,我们在test1.doc上录制的宏也可以在test2.doc上运行成功(当然选择宏保存时必须要保存到“所有文档(Normal)”中)。
调用excel宏
调用excel宏和调用word宏有点区别,因为excel不能将宏保存到“所有文档(Normal)”上,因此在调用宏的时候需要指明宏所在的具体文档,最后一条语句需要这么写:
1 Variant result = Dispatch.call(excel, "Run", new Variant("test.xls!Module1.test"),//这里需要说明宏所在的文档
2 new Variant(arg1),
3 new Variant(arg2));
4. 插入分节符代码这么来写;
首先录制一个宏,命名为InsertBreakWdSectionBreakNextPage

在程序中用jacob调用它。
Dispatch.call(app, "Run", new Variant("InsertBreakWdSectionBreakNextPage"));
参考:


浙公网安备 33010602011771号