Jacob方式将html静态页面导出生成word文档_html/css_WEB-ITnose
程序员文章站
2022-04-10 11:18:26
...
摘要 这种方式的好处是生成的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[] {}); } }
来自: http://my.oschina.net/sunboy1116/blog/591493