jacob 操作文档Word以及表格(转帖)

本文介绍如何通过Jacob库在Java中实现对Word文档的操作,包括文档的创建、打开、保存及关闭,同时还详细讲解了如何操作文档中的表格,如创建、合并单元格、读取与写入数据等。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

打开word

ActiveXComponent MsWordApp = new ActiveXComponent("Word.Application"); 当然这里也可以用其它创建EXCELPPT对象。所以机子上必须安装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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值