日期:2014-05-20 浏览次数:20683 次
public class WordReader {
public static void extractDoc(String inputFIle, String outputFile) {
boolean flag = false;
// 打开Word 应用程序
ActiveXComponent app = new ActiveXComponent("Word.Application");
try {
// 设置word 不可见
app.setProperty("Visible", new Variant(false));
// 打开word 文件
Dispatch doc1 = app.getProperty("Documents").toDispatch();
Dispatch doc2 = Dispatch.invoke(
doc1,
"Open",
Dispatch.Method,
new Object[] { inputFIle, new Variant(false),
new Variant(true) }, new int[1]).toDispatch();
// 作为txt 格式保存到临时文件
Dispatch.invoke(doc2, "SaveAs", Dispatch.Method, new Object[] {
outputFile, new Variant(7) }, new int[1]);
// 关闭word
Variant f = new Variant(false);
Dispatch.call(doc2, "Close", f);
flag = true;
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
if (flag == true) {
System.out.println("Transformed Successfully");
} else {
System.out.println("Transform Failed");
}
}
public static void main(String[] args) {
WordReader.extractDoc("c:/a.doc", "c:/a.txt");
}
}