java 实现word转txt
- http://sourceforge.net/projects/jacob-project/,下载jacob.jar包
下载的文件目录如下所示:
![在这里插入图片描述](https://i-blog.csdnimg.cn/blog_migrate/8ee41c0c5f89bc00d6c2a5e5e0a6f23b.png)
2.将.dll文件拷贝到%JAVA_HOME%/jre/bin。注意:根据jdk是32位或64位选择对应的.dll文件。
3.拷贝jacob.jar到自己的项目中。
4.代码如下所示:
package com.paperCompare.util;
import com.jacob.activeX.ActiveXComponent;
import com.jacob.com.Dispatch;
import com.jacob.com.Variant;
public class Word2TxtUtil {
public static void word2Txt(String wordPath,String txtPath) {
ActiveXComponent app = new ActiveXComponent("Word.Application");
app.setProperty("Visible", new Variant(false));
Dispatch doc1 = app.getProperty("Documents").toDispatch();
Dispatch doc2 = Dispatch.invoke(
doc1,
"Open",
Dispatch.Method,
new Object[]{wordPath, new Variant(false), new Variant(true)},
new int[1]
).toDispatch();
Dispatch.invoke(
doc2,
"SaveAs",
Dispatch.Method,
new Object[]{txtPath,new Variant(7)
},
new int[1]
);
Variant f = new Variant(false);
Dispatch.call(doc2, "Close", f);
}
public static void main(String[] args) {
Word2TxtUtil.word2Txt("G:\\EclipseCode\\MyCode3\\paperCompre-authors\\src\\main\\webapp\\upload\\1590809006292基于BS模式的学生成绩管理系统.docx", "G:\\EclipseCode\\MyCode3\\paperCompre-authors\\src\\main\\webapp\\upload\\1590809006292基于BS模式的学生成绩管理系统.txt");
}
}