关闭

用jacob实现word文件的追加

2199人阅读 评论(0) 收藏 举报

/*
 * 创建日期 2005-6-6
 *
 * 合并word文件
 */
package com.gao;

import java.util.ArrayList;
import java.util.List;

import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;

/**
 * @author gaoyongqiang
 *
 * TODO 要更改此生成的类型注释的模板,请转至 窗口 - 首选项 - Java - 代码样式 - 代码模板
 */
public class UniteWord {
    public static void uniteDoc(List fileList, String savepaths) {
        if (fileList.size() == 0 || fileList == null) {
            return;
        }
        //打开word
        ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word

        try {
            // 设置word不可见
            app.setProperty("Visible", new Variant(false));
            //获得documents对象
            Object docs = app.getProperty("Documents").toDispatch();
            //打开第一个文件
            Object doc = Dispatch
                    .invoke(
                            (Dispatch) docs,
                            "Open",
                            Dispatch.Method,
                            new Object[] { (String) fileList.get(0),
                                    new Variant(false), new Variant(true) },
                            new int[3]).toDispatch();
            //追加文件
            for (int i = 1; i < fileList.size(); i++) {

                Dispatch.invoke(app.getProperty("Selection").toDispatch(),
                        "insertFile", Dispatch.Method, new Object[] {
                                (String) fileList.get(i), "",
                                new Variant(false), new Variant(false),
                                new Variant(false) }, new int[3]);
            }

            //保存新的word文件
            Dispatch.invoke((Dispatch) doc, "SaveAs", Dispatch.Method,
                    new Object[] { savepaths, new Variant(1) }, new int[3]);

            Variant f = new Variant(false);
            Dispatch.call((Dispatch) doc, "Close", f);

        } catch (Exception e) {
            throw new RuntimeException("合并word文件出错.原因:" + e);
        } finally {
            app.invoke("Quit", new Variant[] {});
        }
      
    }

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:4633次
    • 积分:79
    • 等级:
    • 排名:千里之外
    • 原创:3篇
    • 转载:1篇
    • 译文:0篇
    • 评论:0条
    文章存档