项目中还有一个问题,就是提取数据,如果从xml中提取那就方便一些,但是直接解析成xml的话会导致出现很多的垃圾标签,无意中,把word转txt然后在转xml,垃圾标签就减少了很多。提供源码 package office.syec.lyc; import java.io.File; import com.jacob.activeX.ActiveXComponent; import com.jacob.com.Dispatch; import com.jacob.com.Variant; public class Word2Xml { public static void main(String[] args) { System.out.println(word2Xml("D://Tomcat 5.5//webapps//SomeDome//无线网络优化平台数据需求规范-无线参数部分-诺基亚分册-V1.0.0.doc")); } public static String word2Xml(String filePath) { ActiveXComponent app = new ActiveXComponent("Word.Application");//启动word String inFile = filePath;//要转换的word文件 String tpFile = filePath + ".txt";//临时文件 String otFile = filePath + ".xml";//目标文件 boolean flag = false; try { app.setProperty("Visible", new Variant(false));//设置word不可见 Dispatch docs = app.getProperty("Documents").toDispatch(); Dispatch doc = Dispatch.invoke( docs, "Open", Dispatch.Method, new Object[] { inFile, new Variant(false), new Variant(true) }, new int[1]).toDispatch();//打开word文件 Dispatch.invoke(doc, "SaveAs", Dispatch.Method, new Object[] { tpFile, new Variant(7) }, new int[1]);//作为html格式保存到临时文件 Variant f = new Variant(false); Dispatch.call(doc, "Close", f); flag = true; } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[] {}); } if (flag) { app = new ActiveXComponent("Excel.Application");//启动excel try { app.setProperty("Visible", new Variant(false));//设置excel不可见 Dispatch workbooks = app.getProperty("Workbooks").toDispatch(); Dispatch workbook = Dispatch.invoke( workbooks, "Open", Dispatch.Method, new Object[] { tpFile, new Variant(false), new Variant(false) }, new int[1]).toDispatch();//打开临时文件 System.out.println("open temp html successfully"); //****************************保存为XML时报错************************** Dispatch.invoke(workbook, "SaveAs", Dispatch.Method, new Object[] { otFile, new Variant(46) }, new int[1]);//以xml格式保存到目标文件 //****************************保存为XML时报错************************** System.out.println("save to xml successfully!"); Variant f = new Variant(false); Dispatch.call(workbook, "Close", f); } catch (Exception e) { e.printStackTrace(); } finally { app.invoke("Quit", new Variant[] {}); try { File file = new File(tpFile); file.delete(); } catch (Exception e) { } } } return otFile; } }