Jacob html轉word基於MS Office/WPS Office; 支援Android WPS mobile view;
方法1:html office:word 宣告方式;
PrintWriter out = getResponse().getWriter();
response.setCharacterEncoding("UTF-8");
response.setCharacterEncoding("UTF-8");
response.setHeader("Content-disposition", "attachment; filename=" + new String(title.getBytes("UTF-8"), "iso-8859-1") + ".doc");
out.write(“<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:w="urn:schemas-microsoft-com:office:word" xmlns="http://www.w3.org/TR/REC-html40">”);
out.write(“<head><meta http-equiv=Content-Type content="text/html; charset=gb2312" >");
out.write(“<title>測試</title>");
out.write("</head>");
out.write(“<body style=\"tab-interval:21pt;text-justify-trim:punctuation;\" >");
out.write(“<!--StartFragment-->");
out.write(“<div>---自定義html內容--</div>");
out.write(“<!--EndFragment-->");
out.write(“</body>");
out.flush();
out.close();
return null;
*此方法當body中無html標籤時支援Android WPS mobile view.
方法2.1:Jacob方式;支援 Android WPS mobile view.
jacob.jar 放到專案lib下, jacob-xx.dll 放到C:\Windows\System32
/**
* HTML 轉 word.
* @param docfile word檔案全路徑
* @param htmlfile 轉換後的html存放路徑
*/
public static void htmlToWord(String htmlfile, String docfile) {
System.out.println(htmlfile + "-->" + docfile);
long start = System.currentTimeMillis();
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Word.Application");
try {
app.setProperty("Visible", new Variant(false));
Dispatch docs = app.getProperty("Documents").toDispatch();
Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method, new Object[] { docfile, new Variant(false), new Variant(true) }, new int[1]).toDispatch();
Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { htmlfile, new Variant(0) }, new int[1]);
Dispatch.call(doc, "Close", new Variant(false));
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", new Variant[] {});
ComThread.Release();
System.out.println("wordToHTML用時:" + (System.currentTimeMillis() - start) + "毫秒");
}
}
方法2.2:Jacob方式;支援 Android WPS mobile view.
private static final int HTML_TO_WORD = 0; // html轉word.
/**
* HTML 轉 word.(部署在Tomcat(服務執行)異常)* @param htmlPath 轉換前的html路徑(支援URL)
* @param wordPath word檔案轉換後路徑
* @exception com.jacob.com.ComFailException Tomcat以服務啟動時 Can't get object clsid from progid KWPS.Application
*/
@Deprecated
System.out.println(htmlPath + "-->" + wordPath);
long start = System.currentTimeMillis();
ComThread.InitSTA();
ActiveXComponent wps = null;
try {
wps = new ActiveXComponent("KWPS.Application"); // 改為 ”Word.Application“ Tomcat 以服務執行OK
wps.setProperty("Visible", false);
ActiveXComponent doc = wps.invokeGetComponent("Documents").invokeGetComponent("Open", new Variant(htmlPath));
doc.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { wordPath, 0}, new int[1]);
doc.invoke("Close");
doc.safeRelease();
} catch (Exception e) {
e.printStackTrace();
} finally {
if (wps != null) {
wps.invoke("Quit");
wps.safeRelease();
}
ComThread.Release();
}
System.out.println("wordToHTML用時:" + (System.currentTimeMillis() - start) + "毫秒");
}
*此方發適合批量轉換,當用戶請求時直接返回已轉換的內容; (*WPS Write程序有時會出現轉換後進程不結束的現象)
*new ActiveXComponent("KWPS.Application") 改為單例且不關閉資源時轉換速度會提高一倍左右;