Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

give some useful idea for poi function in android platform #118

Open
lzxshare opened this issue Jun 4, 2024 · 1 comment
Open

give some useful idea for poi function in android platform #118

lzxshare opened this issue Jun 4, 2024 · 1 comment

Comments

@lzxshare
Copy link

lzxshare commented Jun 4, 2024

hi author,thanks for your greate work to help android developer about poi,so i want to get some good idea about “how to use poi preview office document in andoid”. popular idea is that use poi convert office file to html,then use WebView to open the html. could you perfect the sample project about how to use poi convert the office file to html? thank you so much!

@EliAgne
Copy link
Contributor

EliAgne commented Aug 31, 2024

I was curious about this as well, and looks like the existing Apache POI code works as intended within this project.

Example:

    HWPFDocumentCore wordDocument = WordToHtmlUtils.loadDoc(new FileInputStream("D:\\temp\\seo\\1.doc"));
    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());
    System.out.println(result);

Source: https://stackoverflow.com/a/7901139/12327198

At that point, you would just need load the HTML data into the WebView, like you would any HTML file.

Just be aware that the WordToHtmlUtils seems to only be limited to the HWPFDocument (.doc) and not the XWPFDocument (.docx).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants