代码网上找的,但是网上只提供了代码,没有JAR包,而这个功能需要的JAR包很多,收集
起来不是太方便,我这里收集好了,点击下载。
代码如下,亲测可用:
package com.wei.liu.word;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.InputStream;
import java.io.OutputStream;
import org.apache.poi.xwpf.converter.core.FileImageExtractor;
import org.apache.poi.xwpf.converter.core.FileURIResolver;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
public class Word2007 {
public static void main(String[] args) {
try {
File f = new File("e:\\test\\33.docx");
if (!f.exists()) {
System.out.println("Sorry File does not Exists!");
} else {
if (f.getName().endsWith(".docx")
|| f.getName().endsWith(".DOCX")) {
// 1) Load DOCX into XWPFDocument
InputStream in = new FileInputStream(f);
XWPFDocument document = new XWPFDocument(in);
// 2) Prepare XHTML options (here we set the IURIResolver to
// load images from a "word/media" folder)
File imageFolderFile = new File("e://test");
XHTMLOptions options = XHTMLOptions.create().URIResolver(
new FileURIResolver(imageFolderFile));
options.setExtractor(new FileImageExtractor(imageFolderFile));
options.setIgnoreStylesIfUnused(false);
options.setFragment(true);
// 3) Convert XWPFDocument to XHTML
File file = new File("e:\\test\\33.html");
OutputStream out = new FileOutputStream(file);
XHTMLConverter.getInstance()
.convert(document, out, options);
BufferedReader in1 = new BufferedReader(new FileReader(
"e:\\test\\33.html"));
String str;
while ((str = in1.readLine()) != null) {
System.out.println(str);
}
in1.close();
// file.delete();
} else {
System.out.println("Enter only MS Office 2007+ files");
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}