java jacob 操作word 文档进行写操作.docx
- 文档编号:30273268
- 上传时间:2023-08-13
- 格式:DOCX
- 页数:43
- 大小:30.57KB
java jacob 操作word 文档进行写操作.docx
《java jacob 操作word 文档进行写操作.docx》由会员分享,可在线阅读,更多相关《java jacob 操作word 文档进行写操作.docx(43页珍藏版)》请在冰豆网上搜索。
javajacob操作word文档进行写操作
目录
第一部分Word文档自动追加表格内容2
第二部分javajacob操作word文档,进行写操作,如生成表格,添加图片3
第三部分28
第一部分Word文档自动追加表格内容
今天在做一个自动化生成SDD文档的小工具,通过Word的模板,前台通过Flex填入数据,最后将Word文档填写好。
以下是一部分代码,关于表格自动追加表格内容
Java代码
1.import java.util.ArrayList;
2.
3.import com.heavenlake.wordapi.Document;
4.
5.public class test
6.{
7. public test()
8. {
9. Document doc = null;
10. try {
11. doc = new Document();
12. doc.newDoc("e:
/test4.doc");
13. doc.insertln("测试文档");
14. doc.insertln("天池软件产品列表");
15. //doc.saveAs("e:
/test2.doc");
16. ArrayList tableData = new ArrayList(3);
17. String[] title = {
18. "序号", "代码", "名称","网址"};
19. tableData.add(title);
20. String[] field1 = {
21. "1", "751002", "天池软件自动化生产平台",""};
22. tableData.add(field1);
23. String[] field2 = {
24. "2", "751004", "产品数据管理系统",""};
25. tableData.add(field2);
26.
27. doc.insert(tableData,"流行型");
28.
29. doc.insertln();
30.
31.
32. ArrayList tableData1 = new ArrayList(3);
33.
34. String[] field3 = {
35. "1", "751002", "天池软件自动化生产平台",""};
36. tableData1.add(field3);
37. String[] field4 = {
38. "2", "751004", "产品数据管理系统",""};
39. tableData1.add(field4);
40. doc.replaceTable(1, 4, tableData1);
41.
42. } catch (Exception e)
43. {
44. e.printStackTrace();
45. } finally
46. {
47. try
48. {
49. if (doc !
= null)
50. doc.close(true);
51. } catch (Exception e)
52. {
53. e.printStackTrace();
54. }
55.
56. }
57.
58. }
59.
60. public static void main(String[] args)
61. {
62. test test1 = new test();
63. }
64.
65.}
第二部分javajacob操作word文档,进行写操作,如生成表格,添加图片
jacob-1.15-M3.zip
jacob-1.15-M3-x86.dll copy到c:
//windows/system32
引入jacob.jar
示例代码
viewplain
1.import java.io.File;
2.import com.jacob.activeX.ActiveXComponent;
3.import .Dispatch;
4.import .Variant;
5.class WordBean {
6. // 代表一个word 程序
7. private ActiveXComponent MsWordApp = null;
8. // 代表进行处理的word 文档
9. private Dispatch document = null;
10. public WordBean() {
11. // Open Word if we/'ve not done it already
12. if (MsWordApp == null) {
13. MsWordApp = new ActiveXComponent("Word.Application");
14. }
15. }
16. // 设置是否在前台打开 word 程序 ,
17. public void setVisible(boolean visible) {
18. MsWordApp.setProperty("Visible", new Variant(visible));
19. // 这一句作用相同
20. // Dispatch.put(MsWordApp, "Visible", new Variant(visible));
21. }
22. // 创建一个新文档
23. public void createNewDocument() {
24. // Find the Documents collection object maintained by Word
25. // documents表示word的所有文档窗口,(word是多文档应用程序)
26. Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
27. // Call the Add method of the Documents collection to create
28. // a new document to edit
29. document = Dispatch.call(documents, "Add").toDispatch();
30. }
31. // 打开一个存在的word文档,并用document 引用 引用它
32. public void openFile(String wordFilePath) {
33. // Find the Documents collection object maintained by Word
34. // documents表示word的所有文档窗口,(word是多文档应用程序)
35. Dispatch documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
36. document = Dispatch.call(documents, "Open", wordFilePath,
37. new Variant(true)/* 是否进行转换ConfirmConversions */,
38. new Variant(false)/* 是否只读 */).toDispatch();
39. // document = Dispatch.invoke(documents, "Open", Dispatch.Method,
40. // new Object[] { wordFilePath, new Variant(true),
41. // new Variant(false)
42. // }, new int[1]).toDispatch();
43. }
44. // 向 document 中插入文本内容
45. public void insertText(String textToInsert) {
46. // Get the current selection within Word at the moment.
47. // a new document has just been created then this will be at
48. // the top of the new doc 获得选 中的内容,如果是一个新创建的文件,因里面无内容,则光标应处于文件开头处
49. Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
50. // 取消选中,应该就是移动光标 ,否则 新添加的内容会覆盖选中的内容
51. Dispatch.call(selection, "MoveRight", new Variant
(1), new Variant
(1));
52. // Put the specified text at the insertion point
53. Dispatch.put(selection, "Text", textToInsert);
54. // 取消选中,应该就是移动光标
55. Dispatch.call(selection, "MoveRight", new Variant
(1), new Variant
(1));
56. }
57. // 向文档中添加 一个图片,
58. public void insertJpeg(String jpegFilePath) {
59. Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
60. Dispatch image = Dispatch.get(selection, "InLineShapes").toDispatch();
61. Dispatch.call(image, "AddPicture", jpegFilePath);
62. }
63. // 段落的处理,插入格式化的文本
64. public void insertFormatStr(String text) {
65. Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容
66. Dispatch.call(wordContent, "InsertAfter", text);// 插入一个段落到最后
67. Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs")
68. .toDispatch(); // 所有段落
69. int paragraphCount = Dispatch.get(paragraphs, "Count").changeType(
70. Variant.VariantInt).getInt();// 一共的段落数
71. // 找到刚输入的段落,设置格式
72. Dispatch lastParagraph = Dispatch.call(paragraphs, "Item",
73. new Variant(paragraphCount)).toDispatch(); // 最后一段(也就是刚插入的)
74. // Range 对象表示文档中的一个连续范围,由一个起始字符位置和一个终止字符位置定义
75. Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range")
76. .toDispatch();
77. Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch();
78. Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体
79. Dispatch.put(font, "Italic", new Variant(true)); // 设置为斜体
80. Dispatch.put(font, "Name", new Variant("宋体")); //
81. Dispatch.put(font, "Size", new Variant(12)); // 小四
82. Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch();
83. Dispatch.call(selection, "TypeParagraph");// 插入一个空行
84. Dispatch alignment = Dispatch.get(selection, "ParagraphFormat")
85. .toDispatch();// 段落格式
86. Dispatch.put(alignment, "Alignment", "2"); // (1:
置中 2:
靠右 3:
靠左)
87. }
88. // word 中在对表格进行遍历的时候 ,是先列后行 先column 后cell
89. // 另外下标从1开始
90. public void insertTable(String tableTitle, int row, int column) {
91. Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); // 输入内容需要的对象
92. Dispatch.call(selection, "TypeText", tableTitle); // 写入标题内容 // 标题格行
93. Dispatch.call(selection, "TypeParagraph"); // 空一行段落
94. Dispatch.call(selection, "TypeParagraph"); // 空一行段落
95. Dispatch.call(selection, "MoveDown"); // 游标往下一行
96. // 建立表格
97. Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
98. // int count = Dispatch.get(tables,
99. // "Count").changeType(Variant.VariantInt).getInt(); // document中的表格数量
100. // Dispatch table = Dispatch.call(tables, "Item", new Variant(
101. // 1)).toDispatch();//文档中第一个表格
102. Dispatch range = Dispatch.get(selection, "Range").toDispatch();// /当前光标位置或者选中的区域
103. Dispatch newTable = Dispatch.call(tables, "Add", range,
104. new Variant(row), new Variant(column), new Variant
(1))
105. .toDispatch(); // 设置row,column,表格外框宽度
106. Dispatch cols = Dispatch.get(newTable, "Columns").toDispatch(); // 此表的所有列,
107. int colCount = Dispatch.get(cols, "Count").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, "Item", new Variant(i))
112. .toDispatch();
113. Dispatch cells = Dispatch.get(col, "Cells").toDispatch();// 当前列中单元格
114. int cellCount = Dispatch.get(cells, "Count").changeType(
115. Variant.VariantInt).getInt();// 当前列中单元格数 实际上这个数等于row
116. for (int j = 1; j <= cellCount; j++) {// 每一列中的单元格数
117. // Dispatch cell = Dispatch.call(cells, "Item", new
118. // Variant(j)).toDispatch(); //当前单元格
119. // Dispatch cell = Dispatch.call(newTable, "Cell", new
120. // Variant(j) , new Variant(i) ).toDispatch(); //取单元格的另一种方法
121. // Dispatch.call(cell, "Select");//选中当前单元格
122. // Dispatch.put(selection, "Text",
123. // "第"+j+"行,第"+i+"列");//往选中的区域中填值,也就是往当前单元格填值
124. putTxtToCell(newTable, j, i, "第" + j + "行,第" + i + "列");// 与上面四句的作用相同
125. }
126. }
- 配套讲稿:
如PPT文件的首页显示word图标,表示该PPT已包含配套word讲稿。双击word图标可打开word文档。
- 特殊限制:
部分文档作品中含有的国旗、国徽等图片,仅作为作品整体效果示例展示,禁止商用。设计者仅对作品中独创性部分享有著作权。
- 关 键 词:
- java jacob 操作word 文档进行写操作 操作 word 文档 进行
![提示](https://static.bdocx.com/images/bang_tan.gif)