格式可能有一点变化,精准要求的不能用
package com.kk.test;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.TransformerException;
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;
/**
*
* @author Jesse
*
*/
public class WordToHtml {
private final static String tempPath = "D:\\gzl\\testHtml\\image"; //存图片的地方
public static void main(String argv[]) {
try {
word2Html("D:\\gzl\\testHtml\\4.doc", "D:\\gzl\\testHtml\\7.html");
} catch (Exception e) {
e.printStackTrace();
}
}
/**
* docx格式word转换为html
*
* @param fileName
* docx文件路径
* @param outPutFile
* html输出文件路径
* @throws TransformerException
* @throws IOException
* @throws ParserConfigurationException
*/
public static void word2Html(String fileName, String outPutFile) throws TransformerException, IOException, ParserConfigurationException {
String fileOutName = outPutFile;
long startTime = System.currentTimeMillis();
XWPFDocument document = new XWPFDocument(new FileInputStream(fileName));
XHTMLOptions options = XHTMLOptions.create().indent(4);
// 导出图片
File imageFolder = new File(tempPath);
options.setExtractor(new FileImageExtractor(imageFolder));
// URI resolver
options.URIResolver(new FileURIResolver(imageFolder));
File outFile = new File(fileOutName);
outFile.getParentFile().mkdirs();
OutputStream out = new FileOutputStream(outFile);
XHTMLConverter.getInstance().convert(document, out, options);
System.out.println("用时 " + fileOutName + " with " + (System.currentTimeMillis() - startTime) + " ms.");
}
}