[英]Convert Doc or Docx into HTML in Java
如何在Java中將doc或docx轉換為HTML。 使用Apache POI,我能夠將doc轉換為html,但無法將docx轉換為html? 請給我看看示例代碼? 此代碼適用於doc,但不適用於docx。
HWPFDocumentCore wordDocument = WordToHtmlUtils.loadDoc(stream);
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
wordToHtmlConverter.processDocument(wordDocument);
Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StreamResult streamResult = new StreamResult(out);
TransformerFactory tf = TransformerFactory.newInstance();
Transformer serializer = tf.newTransformer();
serializer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
serializer.setOutputProperty(OutputKeys.INDENT, "yes");
serializer.setOutputProperty(OutputKeys.METHOD, "html");
serializer.transform(domSource, streamResult);
out.close();
String result = new String(out.toByteArray());
沒有理由不這樣做。
請查看以下內容:
簡而言之,請確保您使用的是最新版本的POI,並且具有所有必需的庫。
(如果您需要其他幫助,請說明什么不起作用。您遇到編譯時錯誤嗎?運行時錯誤?意外輸出?)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.