Reputation:
could we convert microsoft office documents(doc, docx, ppt, pptx, xls, xlsx, etc.) in to html string in Android. i need to show office documents in my app. i have searched and found docx4j, apache poi and http://angelozerr.wordpress.com/2012/12/06/how-to-convert-docxodt-to-pdfhtml-with-java/ to convert files in html. this approach is working fine in desktop version. but when using in android i am getting "Unable to convert in Dalvik format error 1". which is may be due to using too much jars in my android project. i want to know is there a single way from which i convert office document to html in android. sorry for english.
EDIT
i am now able to convert doc to html using apache poi. here is method
public void showsimpleWord() {
File file = new File("/sdcard/test.doc");
HWPFDocumentCore wordDocument = null;
try {
wordDocument = WordToHtmlUtils.loadDoc(new FileInputStream(file));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
WordToHtmlConverter wordToHtmlConverter = null;
try {
wordToHtmlConverter = new WordToHtmlConverter(
DocumentBuilderFactory.newInstance().newDocumentBuilder()
.newDocument());
wordToHtmlConverter.processDocument(wordDocument);
org.w3c.dom.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());
System.out.println(result);
((WebView) findViewById(R.id.webview)).loadData(result,
"text/html", "utf-8");
} catch (Exception e) {
e.printStackTrace();
}
}
now searching for others.
Upvotes: 6
Views: 3246
Reputation: 97
While it definitely is possible to convert a word document to HTML on the android device itself, as other have mentions, it isn't advisable due to a bunch of issues. (namely the limited processing power, and memory of an android, as well as compatibility issues with the majority of libraries designed to handle MS Office files) As they all mentioned it would be preferable to do the conversion server side. That said, if there's really no way that's possible given your situation, you might want to take a look at this stackoverflow question.
The ask-er seems to have a similar problem to yours. (given that I understand your question correctly) The poster answers their own question partway through the discussion, and provides a link to his source in the comment to his own answer. He uses the Apachi POI library in his solution, but breaks the library up into several smaller DEX files (one for each component of the library) so that it plays nicely with android.
Hope that was helpful!
Upvotes: 2