加几个除word外的干扰项
ddd文件夹 依赖代码 主方法com.jacob jacob1.19 system ${basedir}/src/main/resources/lib/jacob.jar org.apache.poi poi3.10-FINAL org.apache.poi poi3.10.1 org.apache.poi poi-ooxml-schemas3.10.1 org.apache.poi poi-ooxml3.10.1 org.apache.poi poi-scratchpad3.9 fr.opensagres.xdocreport xdocreport2.0.1 fr.opensagres.xdocreport fr.opensagres.xdocreport.document2.0.1 fr.opensagres.xdocreport org.apache.poi.xwpf.converter.core1.0.6 fr.opensagres.xdocreport org.apache.poi.xwpf.converter.pdf1.0.6 fr.opensagres.xdocreport org.apache.poi.xwpf.converter.xhtml1.0.6
这里需要说明的是,同一文件夹中可能会存在A.doc和A.docx这样的情况,当转换为A.txt时后转换的会覆盖掉先前转换的,所以定义了两个路径输出到不同文件夹中来解决(当然也可以在文件名后加随机数等方法),不过这里就先不考虑这个问题,暂时写一个路径,可以根据自己需求修改
public static void main(String[] args) throws Exception {
try {
// 定义word文件所在路径
String path="F:\bbb";
// 定义输出txt文件所在路径
String outdocPath = "F:\zzz";
String outdocxPath = "F:\zzz";
path = URLDecoder.decode(path, "UTF-8");
// 调用方法,遍历文件夹
linkedList files = EveryFile.GetDirectory(path);
for (int i = 0; i < files.size(); i++) {
// word文件所在路径
String filesName = String.valueOf(files.get(i));
// word文件名
String fileName=files.get(i).getName();
// 调用方法,进行转换
WordToTxt.word2txt(filesName,fileName,outdocPath,outdocxPath);
}
System.out.println("转换完毕");
} catch (UnsupportedEncodingException e) {
e.printStackTrace();
}
}
遍历文件夹代码
public static linkedList GetDirectory(String path) {
File file = new File(path);
linkedList Dirlist = new linkedList(); // 保存待遍历文件夹的列表
linkedList fileList = new linkedList();
GetoneDir(file, Dirlist, fileList);// 调用遍历文件夹根目录文件的方法
File tmp;
while (!Dirlist.isEmpty()) {
tmp = (File) Dirlist.removeFirst();// 从文件夹列表中删除第一个文件夹,并返回该文件夹赋给tmp变量
// 遍历这个文件夹下的所有文件,并把
GetoneDir(tmp, Dirlist, fileList);
}
return fileList;
}
// 遍历指定文件夹根目录下的文件
private static void GetoneDir(File file, linkedList Dirlist,
linkedList fileList) {
// 每个文件夹遍历都会调用该方法
File[] files = file.listFiles();
if (files == null || files.length == 0) {
return;
}
for (File f : files) {
if (f.isDirectory()) {
Dirlist.add(f);
} else {
// 这里列出当前文件夹根目录下的所有文件,并添加到fileList列表中
fileList.add(f);
// System.out.println("file==>" + f);
}
}
}
word转txt代码
public static void word2txt(String filesName, String fileName, String outdocPath, String outdocxPath) throws Exception {
String fileType = new String("");
fileType = filesName.substring(filesName.length() - 4, filesName.length());
if (fileType.equals("docx")) {
// 要转换的文档全路径
String docxPath = filesName;
// 转换后的文档全路径
String docxtotxtPath = outdocxPath + "/" + fileName.substring(0, fileName.length() - 5) + ".txt";
//得到.docx文件提取器
XWPFWordExtractor docx = new XWPFWordExtractor(POIXMLdocument.openPackage(docxPath));
//提取.docx正文文本
String text = docx.getText();
FileWriter writer = new FileWriter(docxtotxtPath);
writer.write(text);
writer.close();
} else if (fileType.equals(".doc")) {
// 要转换的文档全路径
String docPath = filesName;
// 转换后的文档全路径
String doctotxtPath = outdocPath + "/" + fileName.substring(0, fileName.length() - 4) + ".txt";
InputStream is = new FileInputStream(docPath);
HWPFdocument worddocument = new HWPFdocument(is);
WordToTextConverter converter = new WordToTextConverter(documentBuilderFactory.newInstance().newdocumentBuilder().newdocument());
//对HWPFdocument进行转换
converter.processdocument(worddocument);
Writer writer = new FileWriter(new File(doctotxtPath));
Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
//是否添加空格
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.METHOD, "text");
transformer.transform(
new DOMSource(converter.getdocument()),
new StreamResult(writer));
}
}
运行结果
后记
word文件中的图片转换到txt后将不会保留。
代码一定存在可以优化的地方,一是水品有限,二是手头还有其他的事要忙,目前这些代码满足需要,所以暂时没有修改。各位大佬使用的时候根据自己需要修改,不足之处欢迎批评指正。



