Jacob方式將html靜態頁面導出生成word文檔
摘要 這種方式的好處是生成的word文檔 用office打開時默認是頁面視圖,而不會是web視圖。
/** JACOB方式
* notes:需要將jacob.dll拷貝到windows/system32和classpath路徑下
* @param html html靜態頁面路徑
* @param wordFile 要生成的word文檔路徑
*/
public static void htmlToWord(String html, String wordFile) {
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 啟動word
try {
app.setProperty("Visible", new Variant(false));
Dispatch wordDoc = app.getProperty("Documents").toDispatch();
wordDoc = Dispatch.invoke(wordDoc, "Add", Dispatch.Method, new Object[0], new int[1]).toDispatch();
Dispatch.invoke(app.getProperty("Selection").toDispatch(), "InsertFile", Dispatch.Method, new Object[] { html, "", new Variant(false), new Variant(false), new Variant(false) }, new int[3]);
Dispatch.invoke(wordDoc, "SaveAs", Dispatch.Method, new Object[] {wordFile, new Variant(1)}, new int[1]);
Dispatch.call(wordDoc, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
}
}</pre>