你的意思就是把word中的东西替换成你现有数据吧!
需要在word中设置识别标记。这是我写的替换:
/**
*替换word中的文字
* @param FilePath 文件路径
* @param oldWord 原来的文字
* @param newWord 生成的文字
*/
public static void replaceWord(String FilePath,String oldWord,String newWord){
String inFile = FilePath.substring(0, 3) + "\\" +
FilePath.substring(3, FilePath.length()); //要替换的word文件
System.out.println("inFile:"+inFile);
ActiveXComponent app = new ActiveXComponent("Word.Application"); //启动word boolean flag = false;
try {
app.setProperty("Visible", new Variant(false)); //设置word不可见
Object docs = app.getProperty("Documents").toDispatch();
Object doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
new Object[] {inFile, new Variant(false), new Variant(false)},
new int[1]).toDispatch(); //打开word文件,注意这里第三个参数要设为false,这个参数表示是否以只读方式打开,因为我们要保存原文件,所以以可写方式打开。
Object content = Dispatch.get(doc, "Content").toDispatch(); //提取word文档内容对象
Object finder = Dispatch.get(content, "Find").toDispatch(); //提取find对象,也就查找替换的那个对象
Variant vt = new Variant(false); boolean rt = true;
while (rt) {
rt = Dispatch.invoke(finder, "Execute", Dispatch.Method,
new Object[] {oldWord, vt , vt , vt , vt , vt , vt , vt , vt , newWord,
new Variant(true)}
, new int[1]).toBoolean();
} Dispatch.call(doc, "Save"); //保存
Dispatch.call(doc, "Close", vt );
flag = true;
System.out.println("is over");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
app.invoke("Quit", new Variant[] {});
} }
需要在word中设置识别标记。这是我写的替换:
/**
*替换word中的文字
* @param FilePath 文件路径
* @param oldWord 原来的文字
* @param newWord 生成的文字
*/
public static void replaceWord(String FilePath,String oldWord,String newWord){
String inFile = FilePath.substring(0, 3) + "\\" +
FilePath.substring(3, FilePath.length()); //要替换的word文件
System.out.println("inFile:"+inFile);
ActiveXComponent app = new ActiveXComponent("Word.Application"); //启动word boolean flag = false;
try {
app.setProperty("Visible", new Variant(false)); //设置word不可见
Object docs = app.getProperty("Documents").toDispatch();
Object doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
new Object[] {inFile, new Variant(false), new Variant(false)},
new int[1]).toDispatch(); //打开word文件,注意这里第三个参数要设为false,这个参数表示是否以只读方式打开,因为我们要保存原文件,所以以可写方式打开。
Object content = Dispatch.get(doc, "Content").toDispatch(); //提取word文档内容对象
Object finder = Dispatch.get(content, "Find").toDispatch(); //提取find对象,也就查找替换的那个对象
Variant vt = new Variant(false); boolean rt = true;
while (rt) {
rt = Dispatch.invoke(finder, "Execute", Dispatch.Method,
new Object[] {oldWord, vt , vt , vt , vt , vt , vt , vt , vt , newWord,
new Variant(true)}
, new int[1]).toBoolean();
} Dispatch.call(doc, "Save"); //保存
Dispatch.call(doc, "Close", vt );
flag = true;
System.out.println("is over");
}
catch (Exception e) {
e.printStackTrace();
}
finally {
app.invoke("Quit", new Variant[] {});
} }
解决方案 »
免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货