jacob将docx转换成xml文件出现乱码
核心代码(仅方法):
/**
* doc转换成xml格式
*
* @param filePath doc,docx文件路径
* @param xmlFilePath 转换出的xml文件路径
* @author ServerZhang
* @date 2017年9月28日
*/
public static Boolean doc2Xml(String filePath, String xmlFilePath) {
if (StringUtil.isEmpty(filePath,xmlFilePath)) {
return false;
}
File inputFile = new File(filePath);
if (!inputFile.exists()) {
return false;
}
// 初始化
ComThread.InitSTA();
ActiveXComponent app = new ActiveXComponent("Word.Application"); // 启动word
try {
app.setProperty("Visible", new Variant(false)); // 为false时设置word不可见,为true时是可见要不然看不到Word打打开文件的过程
Dispatch docs = app.getProperty("Documents").toDispatch();
// 打开编辑器
Dispatch doc = Dispatch.invoke(docs, "Open", Dispatch.Method,
new Object[] { filePath, new Variant(false), new Variant(true) }, new int[1]).toDispatch(); // 打开word文档
Dispatch.call(doc, "SaveAs", xmlFilePath, 11);// xml文件格式宏11
Dispatch.call(doc, "Close", false);
app.invoke("Quit", 0);
System.out.println("---------word转换完成--------");
} catch (Exception e) {
e.printStackTrace();
} finally {
app.invoke("Quit", 0);
ComThread.Release();
}
return true;
}
说明:
1.运行条件,配置jacob环境及下载jacob.jar包
2.将doc文件转换xml没有问题,但docx文件转换出的xml会乱码
感谢大神们指点