【问题标题】:Converting a .docx to html using Apache POI and getting no text使用 Apache POI 将 .docx 转换为 html 并且没有文本
【发布时间】:2013-12-16 10:02:29
【问题描述】:

我目前有一些将 .doc 文档转换为 html 的代码,但不幸的是,我用于将 .docx 转换为文本的代码没有获取文本并将其转换。下面是我的代码。

private void convertWordDocXtoHTML(File file) throws ParserConfigurationException, TransformerConfigurationException, TransformerException, IOException {
    XWPFDocument wordDocument = null;
    try {
        wordDocument = new XWPFDocument(new FileInputStream(file));
    } catch (IOException ex) {
        Exceptions.printStackTrace(ex);
    }

    WordToHtmlConverter wordToHtmlConverter = new WordToHtmlConverter(DocumentBuilderFactory.newInstance().newDocumentBuilder().newDocument());
    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());
    acDocTextArea.setText(newDocText);
    String htmlText = result;

}

任何关于为什么这不起作用的想法将不胜感激。 ByteArrayOutput 应该返回整个 html,但它是空的并且没有文本。

【问题讨论】:

    标签: java ms-word apache-poi


    【解决方案1】:

    Mark,您正在使用仅支持.doc 格式的HWPF 包,请参阅this description。该文档还提到尝试通过 XWPF 包.docx 文件提供接口。然而,他们似乎缺乏人力资源,鼓励用户提交扩展。不过应该提供有限的功能,提取文本必须是其中之一。

    您还应该看到这个问题:How to Extract docx (word 2007 above) using apache POI

    【讨论】:

    • 我正在使用 XWPFDocument 从 .docx 中获取文本,效果很好,但我还需要将原始 .docx 文件转换为 html 文件。我可以获得的文件的文本。但我无法得到的是文件的 html 版本。当我为此使用单词提取器时,我从 .docx 中获取文本。由于某种原因,我只是无法将文件转换为 html,并且没有给出错误。
    • 您也可以在这里查看我的示例stackoverflow.com/questions/24652953/…
    【解决方案2】:

    此时我也被打动了。
    现在我知道有一个 3rd 方 API 可以将 docx 转换为 html
    工作正常
    https://code.google.com/p/xdocreport/wiki/XWPFConverterXHTML

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-14
    • 1970-01-01
    • 2020-12-13
    • 1970-01-01
    • 2017-09-29
    • 2019-05-20
    • 2012-11-28
    • 2019-12-20
    相关资源
    最近更新 更多