jacob将docx转换成xml文件出现乱码

wolfshadow.cn 2017-12-25 04:15:20
核心代码(仅方法):
/**
* 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会乱码
感谢大神们指点
...全文
244 回复 打赏 收藏 转发到动态 举报
写回复
用AI写文章
回复
切换为时间正序
请发表友善的回复…
发表回复

50,530

社区成员

发帖
与我相关
我的任务
社区描述
Java相关技术讨论
javaspring bootspring cloud 技术论坛(原bbs)
社区管理员
  • Java相关社区
  • 小虚竹
  • 谙忆
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧