java jacob word

来源:互联网 发布:南昌云迈网络做什么的 编辑:程序博客网 时间:2024/05/21 23:33

JACOB是一个 JAVA到微软的COM接口的桥梁。使用JACOB允许任何JVM访问COM对象,从而使JAVA应用程序能够调用COM对象。如果你要对 MS Word、Excel 进行处理,JACOB 是一个好的选择。JACOB目前已经成为sourceforge(http://sourceforge.net/projects/jacob- project/)的一个开源项目,本文使用的版本是1.10.1。


因为在项目中用到了这个技术,但是在网上没有查到很符合题目的文章,经过我自己的探索,总结写出了这篇文章。


这篇文章可能不能完全满足你的要求,你也可以按照我的探索方法进行探索:参阅VBA操作Office的组件的书籍,然后参考下面的Tip完成需要的功能。文章最后附完整的测试代码。


生成WORD文档的简单讲解:


1. 初始化com的线程,非常重要,否则第二次创建com对象的时候会出现can’t co-create object异常 (参见jacob的帮助文档),完成操作com组件后要调用 realease方法

ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法


2. 初始化word应用程序,新建一个空白文档,取得文档内容对象//Instantiate objWord //Declare word object

ActiveXComponent objWord = new ActiveXComponent("Word.Application");


//Assign a local word object

Dispatch wordObject = (Dispatch) objWord.getObject();


//Create a Dispatch Parameter to show the document that is opened

Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见

Tip:设置一个对象的属性的时候,利用Dispatch的put方法,给属性赋值。上面这行语句相当于vb的 wordObject.Visible = true 语句


//Instantiate the Documents Property

Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序)


//Add a new word document, Current Active Document

Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档

Tip:调用一个对象的方法的时候,利用Dispatch的call方法,上面的语句相当于vb的document = documents.Add() 语句。


Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容

Tip:取得一个对象的成员变量(属性)时利用Dispatch的get方法,上面的语句相当于vb的wordContent = document.Content语句


3. 取得word文档的内容后,可以对其内容进行操作

Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落


4. 设置刚插入的段落的文字格式

Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落

int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数


// 找到刚输入的段落,设置格式

Dispatch lastParagraph = Dispatch.call(paragraphs, "Item",

new Variant(paragraphCount)).

toDispatch(); // 最后一段

Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range").

toDispatch();

Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch();

Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体

Dispatch.put(font, "Italic", new Variant(true)); // 设置为斜体

Dispatch.put(font, "Name", new Variant("宋体")); //

Dispatch.put(font, "Size", new Variant(12)); //小四

注意:如果想插入一个新的空白行,也需要设置段落的文字格式,否则新插入行的文字格式会于刚插入的段落的格式相同。


5. 将当前文档保存

Dispatch.call(document, "SaveAs", new Variant("C:

abc.doc")); // 保存一个新文档


6. 释放COM线程

ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理


完整测试代码:(StudyJacob.java 附件中有本文章和java源文件)

import com.jacob.activeX.ActiveXComponent;

import com.jacob.com.Dispatch;

import com.jacob.com.Variant;

import com.jacob.com.ComThread;


public class StudyJacob {


public static void main(String[] args) {

ComThread.InitSTA();// 初始化com的线程,非常重要!!使用结束后要调用 realease方法


//Instantiate objWord //Declare word object

ActiveXComponent objWord = new ActiveXComponent("Word.Application");


//Assign a local word object

Dispatch wordObject = (Dispatch) objWord.getObject();


//Create a Dispatch Parameter to show the document that is opened

Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见


//Instantiate the Documents Property

Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序)


//Add a new word document, Current Active Document

Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档


Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得word文件的内容


Dispatch.call(wordContent, "InsertAfter", "这里是一个段落的内容");//插入一个段落


Dispatch paragraphs = Dispatch.get(wordContent, "Paragraphs").toDispatch(); // 所有段落

int paragraphCount = Dispatch.get(paragraphs, "Count").toInt(); // 一共的段落数


// 找到刚输入的段落,设置格式

Dispatch lastParagraph = Dispatch.call(paragraphs, "Item",

new Variant(paragraphCount)).

toDispatch(); // 最后一段

Dispatch lastParagraphRange = Dispatch.get(lastParagraph, "Range").

toDispatch();

Dispatch font = Dispatch.get(lastParagraphRange, "Font").toDispatch();

Dispatch.put(font, "Bold", new Variant(true)); // 设置为黑体

Dispatch.put(font, "Italic", new Variant(true)); // 设置为斜体

Dispatch.put(font, "Name", new Variant("宋体")); //

Dispatch.put(font, "Size", new Variant(12)); //小四


Dispatch.call(document, "SaveAs", new Variant("C:

abc.doc")); // 保存一个新文档

ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理

}


}

原创粉丝点击