使用jacob操作word文档

使用jacob要记住用ActiveXComponent调用word的quit方法

1 新建空白的word文档,加入内容,并保存

public class NewAWord
{
  public NewAWord(){
    // 初始化com的线程,非常重要!!使用结束后要调用 release方法
    ComThread.InitSTA();
    // 初始化word应用程序,新建一个空白文档,取得文档内容对象
    ActiveXComponent objWord = new ActiveXComponent("Word.Application");
    Dispatch wordObject = (Dispatch) objWord.getObject();
    Dispatch.put((Dispatch) wordObject, "Visible", new Variant(true));// new Variant(true)表示word应用程序可见
    //Tip:设置一个对象的属性的时候,利用Dispatch的put方法,给属性赋值。上面这行语句相当于vb的 wordObject.Visible = true 语句
    Dispatch documents = objWord.getProperty("Documents").toDispatch(); //documents表示word的所有文档窗口,(word是多文档应用程序)
    Dispatch document = Dispatch.call(documents, "Add").toDispatch(); // 使用Add命令创建一个新文档,用Open命令可以打开一个现有文档
    //Tip:调用一个对象的方法的时候,利用Dispatch的call方法,上面的语句相当于vb的document = documents.Add() 语句。
    Dispatch wordContent = Dispatch.get(document, "Content").toDispatch(); // 取得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)); //小四
    Dispatch.call(document, "SaveAs", new Variant("F:/abc.doc")); // 保存一个新文档
    //注意用ActiveXComponent调用word的quit方法
    objWord.invoke("Quit", new Variant[] {});
    ComThread.Release();//释放com线程。根据jacob的帮助文档,com的线程回收不由java的垃圾回收器处理
 }
 public static void main(String[] args) {
    new NewAWord(); 
 }
}
 
 ActiveXComponent是Dispatch的子类
Dispatch's Object represents MS level dispatch object.
 
关于jacob线程的文章
 Single Threaded Apartment (STA)
Main STA
COM requires that if there is any Apartment threaded component in your application, then the first STA created is tagged as the Main STA. COM uses the Main STA to create all the Apartment threaded components that are created from an MTA thread. The problem is that if you have already created an STA, then COM will pick that as the Main STA, and if you ever exit that thread - the whole application will exit.
Com要求如果在你的应用里有任何Apartment线程,那么,首先创建的STA会标记成Main STA。COM用Main STA来创建所有的Apartment线程,问题是,假如你已经创建一个STA,那么COM会选择它做为MainSTA,并且假如你退出这个线程,那么整个应用会退出。
 
保存不同的格式
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { mhtPath,
            new Variant(9) }, new int[1]);
 
默认 docx
7 保存成txt
8保存成html
9 保存成mht
 
dispatch
put 设置参数
invoke 调用方法 返回信息在调用参数里
call 调用命令 返回信息在返回值里
ActiveXComponet
setProperty
设置控件的属性,
使用jacob关键在于了解ActiveX控件本身
 
word application的使用手册在word帮助的开发人员手册里可查询。
 

Variant.DEFAULT用于可选参数

 
posted @ 2011-10-21 16:30  gmartincn  阅读(2569)  评论(0编辑  收藏  举报