最近要利用java来实现word转换为html。
网上google后发现一种可行的方法:
package com.lgf.jacob.word; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.ComThread; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class WordTOHTML { public static void main(String[] args) { String wordExistPath = "E:\\xxx.doc";//word文件路径 String htmlSaveASPath = "E:\\ooo.htm";//html文件路径 wordToHtml(wordExistPath, htmlSaveASPath); } /** * 工具类。实现word转html * @param wordExistPath 要转换的word路径 * @param htmlSaveASPath 生成后html保存的路径 * @return */ private static Boolean wordToHtml(String wordExistPath, String htmlSaveASPath) { String wordPath = wordExistPath; String htmlPath = htmlSaveASPath; ComThread.InitSTA(); ActiveXComponent app = new ActiveXComponent("Word.Application");//打开word // word 是否可见 boolean flag = false; try { // 设置word不可见 app.setProperty("Visible", new Variant(flag)); Dispatch document = app.getProperty("Documents").toDispatch(); Dispatch doc2 = Dispatch.invoke(document,"Open", Dispatch.Method, new Object[]{wordPath,new Variant(false), new Variant(true)}, new int[1]).toDispatch();//打开word文件 /* * new Variant(10)筛选过的网页 * new Variant(9) 单个文件网页 * new Variant(8) 另存为网页 * new Variant(7) 另存为txt格式 * new Variant(6) 另存为rtf格式 * new Variant(0) 另存为doc格式 */ Dispatch.invoke(doc2,"SaveAs", Dispatch.Method, new Object[]{htmlPath,new Variant(8)}, new int[1]); Dispatch.call(doc2,"Close",new Variant(false)); //关闭 } catch(Exception e) { e.printStackTrace(); } finally { app.invoke("Quit",new Variant[]{}); ComThread.Release(); } return true; } }
下面带上jar包和 上面的源码,需要的可以下载