我需要將.docx文件內容轉換爲HTML文本才能在web ui中顯示。無法使用Java將docx轉換爲html
我使用Apache的POI的XWPFDocument類,但一直沒能得到任何結果,但; 獲得空字符串。我的代碼基於this sample。
這裏也是我的代碼:
public JSONObject uploadDocxFile(MultipartFile multipartFile) throws Exception {
InputStream inputStream = multipartFile.getInputStream();
XWPFDocument wordDocument = new XWPFDocument(inputStream);
WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
org.w3c.dom.Document htmlDocument = wordToHtmlConverter.getDocument();
ByteArrayOutputStream out = new ByteArrayOutputStream();
DOMSource domSource = new DOMSource(htmlDocument);
StringWriter stringWriter = new StringWriter();
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, new StreamResult(stringWriter));
out.close();
String result = new String(out.toByteArray());
String htmlText = result;
JSONObject jsonObject = new JSONObject();
jsonObject.put("content", htmlText);
jsonObject.put("success", true);
return jsonObject;
}
可能的重複[使用Apache POI將.docx轉換爲html並獲取文本](http://stackoverflow.com/questions/13103421/converting-a-docx-to-html-using-apache- poi-and-getting-no-text) –
有沒有適當的答案在那裏..問題的所有者以同樣的理由與我打開這個問題;但他補充說,他在獲取文本時沒有問題。 – talha06