jacob实现根据模板生成文件并打印

/*
 * Created on 2007-7-27 上午10:07:03 by OliverLee
 */
package com.oliver;

import java.util.HashMap;
import java.util.Iterator;

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

public class Java2word
{
    private boolean saveOnExit;

    /**
     * word文档
     */
    Dispatch doc = null;

    /**
     * word运行程序对象
     */
    final static ActiveXComponent word;

    /**
     * 所有word文档
     */
    final static Dispatch documents;

    static
    {
        word = new ActiveXComponent("Word.Application");
        word.setProperty("Visible", new Variant(false));
        documents = word.getProperty("Documents").toDispatch();
    }

    /**
     * 构造函数
     */
    public Java2word()
    {
        saveOnExit = true;
    }

    /**
     * 打开文件
     *
     * @param inputDoc
     *            String 要打开的文件,全路径
     * @return Dispatch 打开的文件
     */
    public Dispatch open(String inputDoc)
    {
        return Dispatch.call(documents, "Open", inputDoc).toDispatch();
    }

    /**
     * 选定内容
     *
     * @return Dispatch 选定的范围或插入点
     */
    public Dispatch select()
    {
        return word.getProperty("Selection").toDispatch();
    }

    /**
     * 把插入点移动到文件首位置
     *
     * @param selection
     *            Dispatch 插入点
     */
    public void moveStart(Dispatch selection)
    {
        Dispatch.call(selection, "HomeKey", new Variant(6));
    }

    /**
     * 从选定内容或插入点开始查找文本
     *
     * @param selection
     *            Dispatch 选定内容
     * @param toFindText
     *            String 要查找的文本
     * @return boolean true-查找到并选中该文本,false-未查找到文本
     */
    public boolean find(Dispatch selection, String toFindText)
    {
        //从selection所在位置开始查询
        Dispatch find = word.call(selection, "Find").toDispatch();
        //设置要查找的内容
        Dispatch.put(find, "Text", toFindText);
        //向前查找
        Dispatch.put(find, "Forward", "True");
        //设置格式
        Dispatch.put(find, "Format", "True");
        //大小写匹配
        Dispatch.put(find, "MatchCase", "True");
        //全字匹配
        Dispatch.put(find, "MatchWholeWord", "True");
        //查找并选中
        return Dispatch.call(find, "Execute").getBoolean();
    }

    /**
     * 把选定内容替换为设定文本
     *
     * @param selection
     *            Dispatch 选定内容
     * @param newText
     *            String 替换为文本
     */
    public void replace(Dispatch selection, String newText)
    {
        //设置替换文本
        Dispatch.put(selection, "Text", newText);
    }

    /**
     * 全局替换
     *
     * @param selection
     *            Dispatch 选定内容或起始插入点
     * @param oldText
     *            String 要替换的文本
     * @param newText
     *            String 替换为文本
     */
    public void replaceAll(Dispatch selection, String oldText, Object replaceObj)
    {
        //移动到文件开头
        moveStart(selection);

        String newText = (String) replaceObj;
        while (find(selection, oldText))
        {
            replace(selection, newText);
            Dispatch.call(selection, "MoveRight");
        }
    }

    /**
     * 保存文件
     *
     * @param outputPath
     *            String 输出文件(包含路径)
     */
    public void save(String outputPath)
    {
        Dispatch.call(Dispatch.call(word, "WordBasic").getDispatch(),
                "FileSaveAs", outputPath);
    }

    /**
     * 关闭文件
     *
     * @param document
     *            Dispatch 要关闭的文件
     */
    public void close(Dispatch doc)
    {
        Dispatch.call(doc, "Close", new Variant(saveOnExit));
    }

    /**
     * 根据模板、数据生成word文件
     *
     * @param inputPath
     *            String 模板文件(包含路径)
     * @param outPath
     *            String 输出文件(包含路径)
     * @param data
     *            HashMap 数据包(包含要填充的字段、对应的数据)
     */
    public void toWord(String inputPath, String outPath, HashMap data)
    {
        String oldText;
        Object newValue;
        try
        {
            doc = open(inputPath);

            Dispatch selection = select();

            Iterator keys = data.keySet().iterator();
            while (keys.hasNext())
            {
                oldText = (String) keys.next();
                newValue = data.get(oldText);

                replaceAll(selection, oldText, newValue);
            }
            save(outPath);
        } catch (Exception e)
        {
            System.out.println("操作word文件失败!" + e);
            e.printStackTrace();
        } finally
        {
            if (doc != null)
                close(doc);
        }

    }
   
    public void printing(String inPath,String outPath,HashMap map){
  
        toWord(inPath, outPath, map);
       
        ActiveXComponent objWord = new ActiveXComponent("Word.Application");
        Dispatch wordObject = (Dispatch) objWord.getObject();
        Dispatch.put((Dispatch) wordObject, "Visible", new Variant(false));
        Dispatch documents = objWord.getProperty("Documents").toDispatch();
        Dispatch document = Dispatch.call(documents, "Open", outPath).toDispatch();
       
        Dispatch.call(document, "PrintOut");
      
        Dispatch.call(document, "Close", new Variant(false));
        objWord.invoke("Quit",new Variant[0]);
        word.invoke("Quit",new Variant[0]);
       
    }
   
    /**
     * @param args
     */
    public static void main(String[] args)
    {
        //*****************************************
        HashMap map = new HashMap();
        map.put("{姓名}","某某人");
        map.put("{证件号码}","0155");
        map.put("{日期}","公元前2000年");
        map.put("{月份}","13");
        map.put("{笔数}","168");
        map.put("{金额}","999999999");
        map.put("{取现金额}","888888");
        map.put("{外币金额}","10000");
        map.put("{卡号}","543211234567");
        //******************************************
        Java2word app = new Java2word();
        app.printing("C:\\报案书_国际卡.doc","C:\\报案书_国际卡2.doc",map);

        System.out.println("执行完毕!");

    }

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值