教学文库网 - 权威文档分享云平台
您的当前位置:首页 > 精品文档 > 高等教育 >

java - jacob - 操作word - 文档,进行写操作(2)

来源:网络收集 时间:2026-07-17
导读: 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

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, \).toDispatch(); 50. // 取消选中,应该就是移动光标 ,否则 新添加的内容会覆盖选中的内容 51. Dispatch.call(selection, \, new Variant(1), new Variant(1)); 52. // Put the specified text at the insertion point 53. Dispatch.put(selection, \, textToInsert); 54. // 取消选中,应该就是移动光标 55. Dispatch.call(selection, \, new Variant(1), new Variant(1)); 56. } 57. // 向文档中添加 一个图片, 58. public void insertJpeg(String jpegFilePath) { 59. Dispatch selection = Dispatch.get(MsWordApp, \).toDispatch(); 60. Dispatch image = Dispatch.get(selection, \).toDispatch(); 61. Dispatch.call(image, \, jpegFilePath); 62. } 63. // 段落的处理,插入格式化的文本 64. public void insertFormatStr(String text) { 65. Dispatch wordContent = Dispatch.get(document, \).toDispatch(); // 取得word文件的内容 66. Dispatch.call(wordContent, \, text);// 插入一个段落到最后 67. Dispatch paragraphs = Dispatch.get(wordContent, \hs\) 68. .toDispatch(); // 所有段落 69. int paragraphCount = Dispatch.get(paragraphs, \).changeType( 70. Variant.VariantInt).getInt();// 一共的段落数 71. // 找到刚输入的段落,设置格式 72. Dispatch lastParagraph = Dispatch.call(paragraphs, \, 73. new Variant(paragraphCount)).toDispatch(); // 最后一段(也就是刚插入的) 74. // Range 对象表示文档中的一个连续范围,由一个起始字符位置和一个终止字符位置定义 75. Dispatch lastParagraphRange = Dispatch.get(lastParagraph, \) 76. .toDispatch(); 77. Dispatch font = Dispatch.get(lastParagraphRange, \).toDispatch(); 78. Dispatch.put(font, \, new Variant(true)); // 设置为黑体 79. Dispatch.put(font, \, new Variant(true)); // 设置为斜体 80. Dispatch.put(font, \, new Variant(\宋体\)); // 81. Dispatch.put(font, \, new Variant(12)); // 小四 82. Dispatch selection = Dispatch.get(MsWordApp, \).toDispatch(); 83. Dispatch.call(selection, \);// 插入一个空行 84. Dispatch alignment = Dispatch.get(selection, \rmat\) 85. .toDispatch();// 段落格式 86. Dispatch.put(alignment, \, \); // (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, \).toDispatch(); // 输入内容需要的对象 92. Dispatch.call(selection, \, tableTitle); // 写入标题内容 // 标题格行 93. Dispatch.call(selection, \); // 空一行段落 94. Dispatch.call(selection, \); // 空一行段落 95. Dispatch.call(selection, \); // 游标往下一行 96. // 建立表格 97. Dispatch tables = Dispatch.get(document, \).toDispatch(); 98. // int count = Dispatch.get(tables, 99. // \ocument中的表格数量 100. // Dispatch table = Dispatch.call(tables, \riant( 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, \new 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 s …… 此处隐藏:1375字,全部文档内容请下载后查看。喜欢就下载吧 ……

java - jacob - 操作word - 文档,进行写操作(2).doc 将本文的Word文档下载到电脑,方便复制、编辑、收藏和打印
本文链接:https://www.jiaowen.net/wendang/607438.html(转载请注明文章来源)
Copyright © 2020-2025 教文网 版权所有
声明 :本网站尊重并保护知识产权,根据《信息网络传播权保护条例》,如果我们转载的作品侵犯了您的权利,请在一个月内通知我们,我们会及时删除。
客服QQ:78024566 邮箱:78024566@qq.com
苏ICP备19068818号-2
Top
× 游客快捷下载通道(下载后可以自由复制和排版)
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
VIP包月下载
特价:29 元/月 原价:99元
低至 0.3 元/份 每月下载150
全站内容免费自由复制
注:下载文档有可能出现无法下载或内容有问题,请联系客服协助您处理。
× 常见问题(客服时间:周一到周五 9:30-18:00)