import org.apache.poi.hwpf.HWPFDocument;
import org.apache.poi.hwpf.converter.WordToHtmlConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLConverter;
import org.apache.poi.xwpf.converter.xhtml.XHTMLOptions;
import org.apache.poi.xwpf.usermodel.XWPFDocument;
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import java.io.*;
import java.net.MalformedURLException;
import java.net.URL;
public class Word2HtmlUtil {
/**
* 将word2003转换为html文件
*
* @param wordPath
* word文件路径
* @param wordName
* word文件名称无后缀
* @param suffix
* word文件后缀
* @return
*/
public static String Word2003ToHtml(String wordPath, String wordName,
String suffix){
// 原word文档
final String file = wordPath + wordName + suffix;
URL url = null;
InputStream input = null;
HWPFDocument wordDocument = null;
WordToHtmlConverter wordToHtmlConverter = null;
String content = null;
try {
url = new URL(file);
input = new BufferedInputStream(url.openStream());
wordDocument = new HWPFDocument(input);
wordToHtmlConverter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument());
// 解析word文档
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
// 使用字符数组流获取解析的内容
try(ByteArrayOutputStream baos = new ByteArrayOutputStream();
OutputStream outStream = new BufferedOutputStream(baos);) {
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(outStream);
TransformerFactory factory = TransformerFactory.newInstance();
Transformer serializer = factory.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
// 使用字符数组流获取解析的内容
content = baos.toString();
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
return content;
}
/**
* 2007版本word转换成html
*
* @param wordPath
* word文件路径
* @param wordName
* word文件名称无后缀
* @param suffix
* word文件后缀
* @return
*/
public static String Word2007ToHtml(String wordPath, String wordName, String suffix)
{
// word文件
String file = wordPath + wordName + suffix;
// 加载word文档生成 XWPFDocument对象
URL url = null;
InputStream in = null;
XWPFDocument document = null;
String content = null;
try {
url = new URL(file);
in = new BufferedInputStream(url.openStream());
document = new XWPFDocument(in);
XHTMLOptions options = XHTMLOptions.create();
options.setIgnoreStylesIfUnused(false);
options.setFragment(true);
// 使用字符数组流获取解析的内容
try(ByteArrayOutputStream baos = new ByteArrayOutputStream();) {
XHTMLConverter.getInstance().convert(document, baos, options);
content = baos.toString();
} catch (IOException e) {
e.printStackTrace();
}
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return content;
}
public static void main(String[] args) throws Exception {
// System.out.println(Word2003ToHtml("http://10.103.7.195/doc/", "help", ".doc"));
System.out.println(Word2007ToHtml("http://10.103.7.195/doc/", "help", ".docx"));
}
}
【utils】Word2HtmlUtil
最新推荐文章于 2021-06-03 07:27:31 发布