java - jacob - 操作word - 文档,进行写操作(2)
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字,全部文档内容请下载后查看。喜欢就下载吧 ……
相关推荐:
- [高等教育]公司协助某村精准扶贫工作总结.doc
- [高等教育]高二生物知识点总结(全)
- [高等教育]苏教版数学三年级下册《解决问题的策略
- [高等教育]仪器分析课程学习心得
- [高等教育]2017年五邑大学数学与计算科学学院333
- [高等教育]人教版七年级下册语文第四单元测试题(
- [高等教育]2018年秋七年级英语上册Unit7Howmuchar
- [高等教育]2017年八年级下数学教学工作小结
- [高等教育]湖南省怀化市2019届高三统一模拟考试(
- [高等教育]四年级下册科学_基础训练及答案教材
- [高等教育]城郊煤矿西风井管路伸缩器更换施工安全
- [高等教育]昆八中20182019学年度上学期期末考试
- [高等教育]项目部各类人员任命书
- [高等教育]上市公司经营水务产业的模式
- [高等教育]人教版高二化学第一学期第三章水溶液中
- [高等教育]【中考物理第一轮复习资料】四.压强与
- [高等教育]金坑水电站报废改建工程机电设备更新改
- [高等教育]高中生物教学工作计划简易版
- [高等教育]2017年西华大学攀枝花学院(联合办学)44
- [高等教育]最新整理超短爆笑英文小笑话大全
- 优秀教师继续教育学习心得体会
- 阳历到阴历的转换
- 留守儿童教育案例分析
- 华师17春秋学期《玩教具制作与环境布置
- 测速传感器新型安装装置的现场应用
- 人教版小学数学三年级下册第四单元
- 创业个人意向书
- 山东省潍坊市2012年高考仿真试题(三)
- [恒心][好卷速递]四川省成都外国语学校
- 多少人错把好转反应当成了病情加重处理
- 中外广播电视史复习资料整理
- 江苏省扬州市江都区宜陵镇中学2014-201
- 工程造价专业毕业实习报告
- 广西师范学院心理与教育统计
- aympkrq基于 - asp的博客网站设计与开
- 建筑业外出经营相关流程操作(营改增后
- 人治 德治 法治
- [精华篇]常识判断专项训练题库
- 中国共产党为什么要实行民主集中
- 小学数学第三册第一单元试卷(A、B、C




