我需要在我的应用中将.doc文件转换为html。
尝试过此代码:
InputStream inputStream = mainactivity.getcontentResolver().
openInputStream(Uri.parse(uri));
HWPFDocumentCore wordDocument = WordToHtmlUtils.loadDoc(inputStream);
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());
并获得此异常:
W / System.err:java.lang.NullPointerException:尝试在null上调用接口方法'org.w3c.dom.Node org.w3c.dom.Node.removeChild(org.w3c.dom.Node)'对象引用
W / System.err:在org.apache.poi.hwpf.converter.AbstractWordUtils.compactChildNodesR(AbstractWordUtils.java:146) 在org.apache.poi.hwpf.converter.WordToHtmlUtils.compactSpans(WordToHtmlUtils.java:238) 在org.apache.poi.hwpf.converter.WordToHtmlConverter.processparagraph(WordToHtmlConverter.java:596) 在org.apache.poi.hwpf.converter.AbstractWordConverter.processparagraphes(AbstractWordConverter.java:1112) 在org.apache.poi.hwpf.converter.WordToHtmlConverter.processSingleSection(WordToHtmlConverter.java:617) 在org.apache.poi.hwpf.converter.AbstractWordConverter.processDocument(AbstractWordConverter.java:722)
此代码在纯Java上有效,但在Android中获取NullPointerException。 我究竟做错了什么? :(