JAVA调用模版生成WORD文件
合集下载
相关主题
- 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
- 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
- 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
public Dispatch select() {
return word.getProperty("Selection").toDispatch(); }
/**
* 把插入点移动到文件首位置
*
* @param selection
*
Dispatch 插入点
*/
public void moveStart(Dispatch selection)
*/
public boolean find(Dispatch selection, String toFindText)
{
//从 selection 所在位置开始查询
Dispatch find = word.call(selection, "Find").toDispatch();
//设置要查找的内容
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 com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant;
Dispatch.put((Dispatch) wordObject, "Visible", new Variant(false)); Dispatch documents = objWord.getProperty("Documents").toDispatch(); Dispatch document = Dispatch.call(documents, "Open", outPath).toDispatch();
/**
* 保存文件
*
* @param outputPath
*
String 输出文件(包含路径)
*/
public void save(String outputPath)
{
Dispatch.call((Dispatch) Dispatch.call(word, "WordBasic").getDispatch(),
} ComThread.Release(); }
/**
* 当前表格
*/
private Dispatch table;
// 选定的范围或插入点
// private static Dispatch selection = null ;
/**
* 创建新表格
*/
public Dispatch createNewTable(int rowCount,int colCount,int width){
}
/////////////////////////////////////////////以下部分为动态导出增加部分 /**
* 打开一个 word */ public void openWord(String inPath){
doc = open(inPath); } /**
* 替换模版中对应的字符串 * @param data */ public void replaceTem(Map data){
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){
*
Dispatch 选定内容或起始插入点
* @param oldText
*
String 要替换的文本
* @param newText
*
String 替换为文本
*/
public void replaceAll(Dispatch selection, String oldText, Object replaceObj)
*
String 要打开的文件,全路径
* @return Dispatch 打开的文件
*/
public Dispatch open(String inputDoc)
{
return Dispatch.call(documents, "Open", inputDoc).toDispatch();
}
/** * 选定内容 * * @return Dispatch 选定的范围或插入点 */
"FileSaveAs", outputPath);
}
/**
* 关闭文件
*
* @param document
*
Dispatch 要关闭的文件
*/
public void close(Dispatch doc)
{
Dispatch.call(doc, "Close", new Variant(saveOnExit));
private Dispatch documents;
private Dispatch selection;
/** * 构造函数 */
public JavaToword() {
saveOnExit = true; if(word==null){ word = new ActiveXComponent("Word.Application");
String oldText; Object newValue; Dispatch selection = select();
Iterator keys = data.keySet().iterator(); while (keys.hasNext()) {
oldText = (String) keys.next(); newValue = data.get(oldText);
Dispatch.call(document, "PrintOut");
Dispatch.call(document, "Close", new Variant(false)); objWord.invoke("Quit",new Variant[0]); word.invoke("Quit",new Variant[0]);
}
/**
* 根据模板、数据生成 word 文件
*
* @param inputPath
*
String 模板文件(包含路径)
* @param outPath
*
String 输出文件(包含路径)
* @param data
*
HashMap 数据包(包含要填充的字段、对应的数据)
*/
public void toWord(String inputPath, String outPath, HashMap data)
* @param newText
*
String 替换为文本
*/
public void replace(Dispatch selection, String newText)
{
//设置替换文本
Dispatch.put(selection, "Text", newText);
}
/**
* 全局替换
*
* @param selection
{
//移动到文件开头
moveStart(selection);
String newText = (String) replaceObj; while (find(selection, oldText)) {
replace(selection, newText); Dispatch.call(selection, "MoveRight"); } }
this.table=Dispatch.call(tables,"Add" , range,new Variant(rowCount), new
Variant(colCount),new Variant(width)).toDispatch();
}
return this.table;
}
/**
* 设置当前表格指定行的背景色
Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
Dispatch range = Dispatch.get(select(), "Range" ).toDispatch();
if(rowCount >0 && colCount >0){
Dispatch.put(find, "Text", toFindText);
//向前查找
Dispatch.put(find, "Forward", "True");
//设置格式
Dispatch.put(find, "Format", "True");
//大小写匹配
Dispatch.put(find, "MatchCase", "True");
public class JavaToword {
private boolean saveOnExit;
/** * word 文档 */
Dispatch doc = null;
/** * word 运行程序对象 */
private ActiveXComponent word;
/** * 所有 word 文档 */
{
String oldText;
Object newValue;
try
{
doc = open(inputPath);
Dispatch selection = select();
Iterator keys = data.keySet().iterator(); while (keys.hasNext()) {
*
* @param rowIndex
* @param color
*
取值范围 0 < color < 17 默认:16 浅灰色 1:黑色 2:蓝色 3:浅
蓝 ...............
*/
public void setRowBgColor( int rowIndex, int color) {
this .getRow(rowIndex);
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();
replaceAll(selection, oldText, newValue); } } /** * 保存文件路径 * @param path */ public void savePath(String path){ save(path);
if (doc != null){ close(doc);
word.setProperty("Visible", new Variant(false)); } if(documents==null){ documents = word.getProperty("Documents").toDispatch(); } }
/**
* 打开文件
*
* @param inputDoc
//全字匹配
Dispatch.put(find, "MatchWholeWord", "True");
wk.baidu.com
//查找并选中
return Dispatch.call(find, "Execute").getBoolean();
}
/**
* 把选定内容替换为设定文本
*
* @param selection
*
Dispatch 选定内容
{
Dispatch.call(selection, "HomeKey", new Variant(6));
}
/** * 从选定内容或插入点开始查找文本 * * @param selection
*
Dispatch 选定内容
* @param toFindText
*
String 要查找的文本
* @return boolean true-查找到并选中该文本,false-未查找到文本
return word.getProperty("Selection").toDispatch(); }
/**
* 把插入点移动到文件首位置
*
* @param selection
*
Dispatch 插入点
*/
public void moveStart(Dispatch selection)
*/
public boolean find(Dispatch selection, String toFindText)
{
//从 selection 所在位置开始查询
Dispatch find = word.call(selection, "Find").toDispatch();
//设置要查找的内容
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 com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant;
Dispatch.put((Dispatch) wordObject, "Visible", new Variant(false)); Dispatch documents = objWord.getProperty("Documents").toDispatch(); Dispatch document = Dispatch.call(documents, "Open", outPath).toDispatch();
/**
* 保存文件
*
* @param outputPath
*
String 输出文件(包含路径)
*/
public void save(String outputPath)
{
Dispatch.call((Dispatch) Dispatch.call(word, "WordBasic").getDispatch(),
} ComThread.Release(); }
/**
* 当前表格
*/
private Dispatch table;
// 选定的范围或插入点
// private static Dispatch selection = null ;
/**
* 创建新表格
*/
public Dispatch createNewTable(int rowCount,int colCount,int width){
}
/////////////////////////////////////////////以下部分为动态导出增加部分 /**
* 打开一个 word */ public void openWord(String inPath){
doc = open(inPath); } /**
* 替换模版中对应的字符串 * @param data */ public void replaceTem(Map data){
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){
*
Dispatch 选定内容或起始插入点
* @param oldText
*
String 要替换的文本
* @param newText
*
String 替换为文本
*/
public void replaceAll(Dispatch selection, String oldText, Object replaceObj)
*
String 要打开的文件,全路径
* @return Dispatch 打开的文件
*/
public Dispatch open(String inputDoc)
{
return Dispatch.call(documents, "Open", inputDoc).toDispatch();
}
/** * 选定内容 * * @return Dispatch 选定的范围或插入点 */
"FileSaveAs", outputPath);
}
/**
* 关闭文件
*
* @param document
*
Dispatch 要关闭的文件
*/
public void close(Dispatch doc)
{
Dispatch.call(doc, "Close", new Variant(saveOnExit));
private Dispatch documents;
private Dispatch selection;
/** * 构造函数 */
public JavaToword() {
saveOnExit = true; if(word==null){ word = new ActiveXComponent("Word.Application");
String oldText; Object newValue; Dispatch selection = select();
Iterator keys = data.keySet().iterator(); while (keys.hasNext()) {
oldText = (String) keys.next(); newValue = data.get(oldText);
Dispatch.call(document, "PrintOut");
Dispatch.call(document, "Close", new Variant(false)); objWord.invoke("Quit",new Variant[0]); word.invoke("Quit",new Variant[0]);
}
/**
* 根据模板、数据生成 word 文件
*
* @param inputPath
*
String 模板文件(包含路径)
* @param outPath
*
String 输出文件(包含路径)
* @param data
*
HashMap 数据包(包含要填充的字段、对应的数据)
*/
public void toWord(String inputPath, String outPath, HashMap data)
* @param newText
*
String 替换为文本
*/
public void replace(Dispatch selection, String newText)
{
//设置替换文本
Dispatch.put(selection, "Text", newText);
}
/**
* 全局替换
*
* @param selection
{
//移动到文件开头
moveStart(selection);
String newText = (String) replaceObj; while (find(selection, oldText)) {
replace(selection, newText); Dispatch.call(selection, "MoveRight"); } }
this.table=Dispatch.call(tables,"Add" , range,new Variant(rowCount), new
Variant(colCount),new Variant(width)).toDispatch();
}
return this.table;
}
/**
* 设置当前表格指定行的背景色
Dispatch tables = Dispatch.get(doc, "Tables" ).toDispatch();
Dispatch range = Dispatch.get(select(), "Range" ).toDispatch();
if(rowCount >0 && colCount >0){
Dispatch.put(find, "Text", toFindText);
//向前查找
Dispatch.put(find, "Forward", "True");
//设置格式
Dispatch.put(find, "Format", "True");
//大小写匹配
Dispatch.put(find, "MatchCase", "True");
public class JavaToword {
private boolean saveOnExit;
/** * word 文档 */
Dispatch doc = null;
/** * word 运行程序对象 */
private ActiveXComponent word;
/** * 所有 word 文档 */
{
String oldText;
Object newValue;
try
{
doc = open(inputPath);
Dispatch selection = select();
Iterator keys = data.keySet().iterator(); while (keys.hasNext()) {
*
* @param rowIndex
* @param color
*
取值范围 0 < color < 17 默认:16 浅灰色 1:黑色 2:蓝色 3:浅
蓝 ...............
*/
public void setRowBgColor( int rowIndex, int color) {
this .getRow(rowIndex);
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();
replaceAll(selection, oldText, newValue); } } /** * 保存文件路径 * @param path */ public void savePath(String path){ save(path);
if (doc != null){ close(doc);
word.setProperty("Visible", new Variant(false)); } if(documents==null){ documents = word.getProperty("Documents").toDispatch(); } }
/**
* 打开文件
*
* @param inputDoc
//全字匹配
Dispatch.put(find, "MatchWholeWord", "True");
wk.baidu.com
//查找并选中
return Dispatch.call(find, "Execute").getBoolean();
}
/**
* 把选定内容替换为设定文本
*
* @param selection
*
Dispatch 选定内容
{
Dispatch.call(selection, "HomeKey", new Variant(6));
}
/** * 从选定内容或插入点开始查找文本 * * @param selection
*
Dispatch 选定内容
* @param toFindText
*
String 要查找的文本
* @return boolean true-查找到并选中该文本,false-未查找到文本