java调用模版生成word文件.docx
文本预览下载声明
package com.env.util;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import com.jacob.activeX.ActiveXComponent;
import .ComThread;
import .Dispatch;
import .Variant;
public class JavaToword
{
private boolean saveOnExit;
/**
word 文档 */
Dispatch doc = null;
/**
word 运行程序对象
*/
private ActiveXComponent word;
/**
所有 word 文档
*/
private Dispatch documents;
private Dispatch selection;
/**
构造函数
*/
public JavaToword()
{
saveOnExit = true;
if(word==null){
word = new ActiveXComponent(Word.Application);
word.setProperty(Visible, new Variant(false));
}
if(documents==null){
documents = word.getProperty(Documents).toDispatch();
}
}
/**
打开文件
@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 sele
显示全部