本文主要是介绍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("执行完毕!");
}
}
这篇关于jacob实现根据模板生成文件并打印的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!