版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、java操作word的表格 最近项目中需要把提交的页面表单的数据动态写在word模板中,简单的写了个工具类。里面有怎眼操作word中表格的内容,可以在word中已有的表格后面添加行并且可以增加内容。/* * title WordBean.java * package com.sinosoft.bjppb_print.utils.word * description 文件描述 * author lijf * update 2012-7-26 下午04:17:10 * version V1.0 */package com.sinosoft.bjppb_print.utils.word;import
2、 java.util.Map;import com.jacob.activeX.ActiveXComponent; import .Dispatch; import .Variant; public class WordBean / word文档 static Dispatch doc; / word运行程序对象 private ActiveXComponent word; / 所有word文档集合 private Dispatch documents; / 选定的范围或插入点 private Dispatch selection; private boolean saveOnExit = t
3、rue; public WordBean() throws Exception if (word = null) word = new ActiveXComponent("Word.Application"); word.setProperty("Visible", new Variant(false); / 不可见打开word word.setProperty("AutomationSecurity", new Variant(3); / 禁用宏 if (documents = null) documents = word.getP
4、roperty("Documents").toDispatch(); /* * 重载,制定文档用户名称,主要用于痕迹保留时候显示对应的用户 * param username * throws Exception */ public WordBean(String username) throws Exception if (word = null) word = new ActiveXComponent("Word.Application"); word.setProperty("UserName", new Variant(user
5、name); word.setProperty("Visible", new Variant(true); / 不可见打开word word.setProperty("AutomationSecurity", new Variant(3); / 禁用宏 if (documents = null) documents = word.getProperty("Documents").toDispatch(); /* * * 设置退出时参数 * * param saveOnExit * * boolean true-退出时保存文件,fals
6、e-退出时不保存文件 * */ public void setSaveOnExit(boolean saveOnExit) this.saveOnExit = saveOnExit; /* * * 创建一个新的word文档 */ public void createNewDocument() doc = Dispatch.call(documents, "Add").toDispatch(); selection = Dispatch.get(word, "Selection").toDispatch(); /* * * 打开一个已存在的文档 * * *
7、 * param docPath * */ public void openDocument(String docPath) closeDocument(); doc = Dispatch.call(documents, "Open", docPath).toDispatch(); Dispatch.put(doc, "TrackRevisions", new Variant(true); Dispatch.put(doc, "PrintRevisions", new Variant(true); Dispatch.put(doc,
8、"ShowRevisions", new Variant(false); selection = Dispatch.get(word, "Selection").toDispatch(); /* * * 只读 打开一个保护文档, * param docPath-文件全名 * * param pwd-密码 * */ public void openDocumentOnlyRead(String docPath, String pwd) throws Exception closeDocument(); doc = Dispatch.callN(docume
9、nts, "Open", new Object docPath, new Variant(false),new Variant(true), new Variant(true), pwd, "",new Variant(false) ).toDispatch(); selection = Dispatch.get(word, "Selection").toDispatch(); public void openDocument(String docPath, String pwd) throws Exception closeDocu
10、ment(); doc = Dispatch.callN( documents, "Open", new Object docPath, new Variant(false), new Variant(false), new Variant(true), pwd ).toDispatch(); selection = Dispatch.get(word, "Selection").toDispatch(); /* * * 把选定的内容或者插入点向右移动 * * * * param pos * * 移动的距离 * */ public void moveRi
11、ght(int pos) if (selection = null) selection = Dispatch.get(word, "Selection").toDispatch(); for (int i = 0; i < pos; i+) Dispatch.call(selection, "MoveRight"); /* * * 把插入点移动到文件首位置 * * */ public void moveStart() if (selection = null) selection = Dispatch.get(word, "Select
12、ion").toDispatch(); Dispatch.call(selection, "HomeKey", new Variant(6); /* * * 从选定内容或插入点开始查找文本 * * * * param toFindText * * 要查找的文本 * * return boolean true-查找到并选中该文本,false-未查找到文本 * */ SuppressWarnings("static-access") public boolean find(String toFindText) Dispatch selection
13、= Dispatch.get(word, "Selection").toDispatch(); / 输入内容需要的对象 if (toFindText = null | toFindText.equals("") return false; / 从selection所在位置开始查询 Dispatch find = word.call(selection, "Find").toDispatch(); / 设置要查找的内容 Dispatch.put(find, "Text", toFindText); / 向前查找 Di
14、spatch.put(find, "Forward", "True"); / 设置格式 Dispatch.put(find, "Format", "True"); / 大小写匹配 Dispatch.put(find, "MatchCase", "True"); / 全字匹配 Dispatch.put(find, "MatchWholeWord", "True"); / 查找并选中 return Dispatch.call(find, &
15、quot;Execute").getBoolean(); /* 246. * * 把选定选定内容设定为替换文本 * * 247. * 248. * param toFindText * 249. * 查找字符串 * 250. * param newText * 251. * 要替换的内容 * 252. * return 253. * 254. */ public boolean replaceText(String toFindText, String newText,Dispatch selection) if (!find(toFindText) return false; Di
16、spatch.put(selection, "Text", newText); return true; /* 269. * * 全局替换文本 * * 270. * 271. * param toFindText * 272. * 查找字符串 * 273. * param newText * 274. * 要替换的内容 275. * 276. */ public void replaceAllText(String toFindText, String newText) while (find(toFindText) Dispatch selection = Dispatc
17、h.get(word, "Selection").toDispatch(); / 输入内容需要的对象 Dispatch.put(selection, "Text", newText); Dispatch.call(selection, "MoveRight"); /* * * param tableIndex * param cellRowIdx * param cellColIdx * return * author lijf * description 获取表格的某个单元格内容 * update 2012-7-27 下午04:22
18、:15 */ public String getTxtFromCell(int tableIndex, int cellRowIdx, int cellColIdx) / 所有表格 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); / 要填充的表格 Dispatch table = Dispatch.call(tables, "Item", new Variant(tableIndex) .toDispatch(); Dispatch cell = Dispatch.call(tabl
19、e, "Cell", new Variant(cellRowIdx), new Variant(cellColIdx).toDispatch(); Dispatch.call(cell, "Select"); String ret = "" ret = Dispatch.get(selection, "Text").toString(); ret = ret.substring(0, ret.length() - 1); / 去掉最后的回车符; return ret; /* * author lijf * desc
20、ription 关闭打开的word * update 2012-7-27 下午04:21:10 */ public void closeDocument() if (doc != null) / Dispatch.call(doc, "ShowRevisions", new Variant(saveOnExit); Dispatch.call(doc, "Save"); Dispatch.call(doc, "Close", new Variant(saveOnExit); doc = null; /* * 增加一行 * * para
21、m tableIndex * word文档中的第N张表(从1开始) */ public void addRow( int tableIndex) Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch(); / 要填充的表格 Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex) .toDispatch(); / 表格的所有行 Dispatch rows = Dispatch.get(table, "
22、;Rows" ).toDispatch(); Dispatch.call(rows, "Add" ).toDispatch(); System.out.println("添加一行"); /* * * param tableIndex 第N个表格 * return * author lijf * description 获取第N个表格的总行数 * update 2012-7-27 下午04:05:47 */ public int getRowCount(int tableIndex) int count=0; Dispatch tables =
23、Dispatch.get(doc, "Tables" ).toDispatch(); / 要填充的表格 Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex).toDispatch(); Dispatch rows = Dispatch.get(table, "Rows" ).toDispatch(); count=Dispatch.get(rows, "Count").getInt(); return count; /* *
24、* param tableIndex 第N个表格 * param cell 一行的单元格数量 * param object 填充内容的对象 * author lijf * description 向第N个表格中添加一行数据 * update 2012-7-27 下午03:25:45 */ public void addRowData(int tableIndex,int cell ,Object object,String strs) addRow(tableIndex);/添加一行 int rowIndex=getRowCount(tableIndex); Dispatch tables =
25、 Dispatch.get(doc, "Tables" ).toDispatch(); / 要填充的表格 Dispatch table = Dispatch.call(tables, "Item" , new Variant(tableIndex).toDispatch(); /为当前行添加内容 for(int i=1;i<=cell;i+) putTxtToCell(table,rowIndex,i,strsi-1); / 打开一个存在的word文档,并用document 引用 引用它 public void openFile(String wo
26、rdFilePath) Dispatch documents = Dispatch.get(word, "Documents").toDispatch(); doc = Dispatch.call(documents, "Open", wordFilePath, new Variant(true)/* 是否进行转换ConfirmConversions */, new Variant(false)/* 是否只读 */).toDispatch(); /* * * param tableTitle * param row * param column * au
27、thor lijf * description 创建一个table * update 2012-7-27 下午03:07:53 */ public void insertTable(String tableTitle, int row, int column) Dispatch selection = Dispatch.get(word, "Selection").toDispatch(); / 输入内容需要的对象 Dispatch.call(selection, "TypeText", tableTitle); / 写入标题内容 / 标题格行 Disp
28、atch.call(selection, "TypeParagraph"); / 空一行段落 Dispatch.call(selection, "TypeParagraph"); / 空一行段落 Dispatch.call(selection, "MoveDown"); / 游标往下一行 / 建立表格 Dispatch tables = Dispatch.get(doc, "Tables").toDispatch(); / int count = Dispatch.get(tables, / "Count
29、").changeType(Variant.VariantInt).getInt(); / document中的表格数量 / Dispatch table = Dispatch.call(tables, "Item", new Variant( / 1).toDispatch();/文档中第一个表格 Dispatch range = Dispatch.get(selection, "Range").toDispatch();/ /当前光标位置或者选中的区域 Dispatch newTable = Dispatch.call(tables, &q
30、uot;Add", range, new Variant(row), new Variant(column), new Variant(1) .toDispatch(); / 设置row,column,表格外框宽度 Dispatch cols = Dispatch.get(newTable, "Columns").toDispatch(); / 此表的所有列, int colCount = Dispatch.get(cols,"Count").getInt(); /.changeType(Variant.VariantInt).getInt()
31、;/ 一共有多少列 实际上这个数=column System.out.println(colCount + "列"); for (int i = 1; i <= colCount; i+) / 循环取出每一列 Dispatch col = Dispatch.call(cols, "Item", new Variant(i) .toDispatch(); Dispatch cells = Dispatch.get(col, "Cells").toDispatch();/ 当前列中单元格 int cellCount = Dispat
32、ch.get(cells, "Count").getInt();/ 当前列中单元格数 实际上这个数等于row for (int j = 1; j <= cellCount; j+) / 每一列中的单元格数 / Dispatch cell = Dispatch.call(cells, "Item", new / Variant(j).toDispatch(); /当前单元格 / Dispatch cell = Dispatch.call(newTable, "Cell", new / Variant(j) , new Varian
33、t(i) ).toDispatch(); /取单元格的另一种方法 / Dispatch.call(cell, "Select");/选中当前单元格 / Dispatch.put(selection, "Text", / "第"+j+"行,第"+i+"列");/往选中的区域中填值,也就是往当前单元格填值 putTxtToCell(newTable, j, i, "第" + j + "行,第" + i + "列");/ 与上面四句的作用相同 /* * 在指定的单元格里填写数据 * * param tableIndex * param cellRowIdx * param cellColIdx * param txt */ public void putTxtToCell(Dispatch table, int cellRowIdx, int cellColIdx, String txt) Dispatch cell = Dispatch.call(table, "Cell", new Variant(cellRowIdx)
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026年医疗行业患者数据隐私保护下的AI私有化方案
- 2026年ECMO插管型号选择与血管匹配性评估指南
- 2026年固态电池产品认定管理办法与申报流程
- 广东省揭阳市揭西县重点中学2025-2026学年初三下学期3月质量检测试题化学试题试卷含解析
- 2026年光明科学城松山湖科学城与港澳科研资源对接规范
- 山东省济宁市汶上县市级名校2026年初三下学期开学质检化学试题含解析
- 2026届内蒙古准格尔旗第四中学初三下学期第二次诊断性考试化学试题含解析
- 2026年湖北省武汉市武昌区第四十六中学初三最后一次模拟(三模)生物试题含解析
- 2026年山东省青岛市即墨市初三9月调研考试化学试题理试题含解析
- 2026届福建省三明建宁县联考初三下学期第二次学情调研生物试题试卷含解析
- 2026河南三门峡市辖区法院省核定聘用制书记员招聘74人考试参考题库及答案解析
- 2026 年三八妇女节 普法宣传方案 课件
- 【新教材】人教PEP版(2024)四年级下册英语 Unit 1 Class rules A Lets talk 教案
- 第一单元 考虑目的和对象(课件)语文新教材统编版八年级下册
- 2026年春季小学科学人教鄂教版(2024)二年级下册教学计划含进度表
- 2026年乌兰察布职业学院单招综合素质考试题库及答案详解(各地真题)
- 2025年江西工业贸易职业技术学院单招职业技能考试题库带答案解析
- 2025年内蒙古机电职业技术学院单招职业适应性测试题库带答案解析
- 高频海事局面试题及答案
- 2025年四川省高考化学真题卷含答案解析
- 公路水运工程施工企业(主要负责人和安全生产管理人员)考核大纲及模拟题库
评论
0/150
提交评论