打开word
ActiveXComponent MsWordApp = new ActiveXComponent("Word.Application"); 当然这里也可以用其它创建EXCEL,PPT对象。所以机子上必须安装Office,不然产生不了该对象。安装WPS也没有用,Microsoft一直没有公布Office内部结构,所以只能安装Office。
设置word是可见或不可见(true:显示;false:不显示)
Dispatch.put(MsWordApp, "Visible", new Variant(Boolean));一般是false.
创建一个文档:
documents = Dispatch.get(MsWordApp, "Documents").toDispatch();
document = Dispatch.call(documents, "Add").toDispatch(); //新增
document = Dispatch.call(documents, "Open", filePath).toDispatch(); //打开
保存文档
Dispatch.call(document, "SaveAs", filename); //保存
退出文档
try{
if(document!=null){
//Dispatch.call(doc, "Close", new Variant(true));
Dispatch.invoke(document, "Close", Dispatch.Method, new Object[]{new Variant(true)}, new int[1]);
document = null;
}
if(MsWordApp!=null){
MsWordApp.invoke("Quit", new Variant[]{});
MsWordApp.safeRelease();
MsWordApp = null;
documents = null;
}
}catch(Exception ex){
ex.printStackTrace();
}
下面是对文档中的表格的操作:
动态创建表格
Dispatch tables = Dispatch.get(document, "Tables").toDispatch();
Dispatch range = Dispatch.get(selection, "Range").toDispatch();
Dispatch newTable = Dispatch.call(tables, "Add", range,new Variant(38), new Variant(16), new Variant(1)).toDispatch(); // 设置列数,栏数,表格外框宽度
在动态创建表格后,可能要根据实际要求合并表格:
合并表格:
Dispatch firstCell = Dispatch.call(table, "Cell", new Variant(firstCellRowIdx), new Variant(firstCellColIdx)).toDispatch();
Dispatch secondCell = Dispatch.call(table, "Cell", new Variant(secondCellRowIdx), new Variant(secondCellColIdx)).toDispatch();
Dispatch.call(firstCell, "Merge", secondCell);
只要慢慢调,最终是可以得到你要的表格,但是不推荐,建议用模板,先新建一个这样的表格,然后再打开文档。
读取表格数据
Dispatch table = Dispatch.call(tables, "Item", new Variant(1)).toDispatch(); //得到你想要的table,一个文档里面是一个Tables,选择你要的table.
Dispatch cell = Dispatch.call(table, "Cell", i, j).toDispatch();
Dispatch range = Dispatch.get(cell, "Range").toDispatch();
String data = Dispatch.get(range, "Text").getString();
往表格中写数据
Dispatch selection = Dispatch.get(MsWordApp, "Selection").toDispatch(); // 输入内容需要的对象
Dispatch.call(cell, "Select");
Dispatch.put(selection, "Text", data );
设置输入内容的文本格式
Dispatch font1 = Dispatch.get(selection, "Font").toDispatch(); // 字型格式化需要的对象
Dispatch.put(alignment, "Alignment", "3"); // (1:置中 2:靠右 3:靠左)
Dispatch.put(font1, "Bold", "1"); // 字型粗体
Dispatch.put(font1, "Color", "1,1,1,1");
Dispatch.put(font1, "Size", new Variant(10.5));
Dispatch.put(font, "Italic", "1"); //字型斜体
Dispatch.put(font1, "Name", new Variant("仿宋_GB2312"));
然后用selection对象添加数据就可以了
得到表格的行与列大小
Dispatch rows = Dispatch.get(table, "Rows").toDispatch();
Dispatch columns = Dispatch.get(table, "Columns").toDispatch();
int rowNum = Dispatch.get(rows, "Count").getInt();
int columnNum = Dispatch.get(columns, "Count");
jacob对段落的操作
Dispatch.call(selection, "MoveDown"); // 游标往下一行
Dispatch.call(selection, "TypeParagraph"); // 空一行段落
Dispatch.call(selection, "MoveRight"); // 光标移到最右边
Dispatch.call(selection, "InsertAfter", "XX说明:");//插入一个段落
另外如果需要部署到tomcat的时候,要把jacob**.dll放到 java.library.path中,不然会出现java.lang.UnsatisfiedLinkError:no jacob**.dll中, in java.library.path ,即使把dll放到system32下面也有可能出现这样的错误。这个时候要注意看下设置环境变量,如果是在myeclipse中,可以在tomcat设置下append to library path->Add Dir,不然需要在tomcat安装目录下面的bin目录下Copy一份jacob.dll。