【问题标题】:how to parse .doc to .html for Android如何将 .doc 解析为 Android 的 .html
【发布时间】:2012-10-21 03:48:42
【问题描述】:

我使用了 apache poi lib,但它使用样式类名进行了解析。我需要没有类的android。 这是我的代码:

    HWPFDocumentCore wordDocument = WordToHtmlUtils.loadDoc(input);

    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.err.println(result);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (TransformerConfigurationException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    } catch (TransformerException e) {
        e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.
    }`

结果:

<html><head>
        <style type="text/css">.b1{white-space-collapsing:preserve;}
    .b2{margin: 0.90555555in 0.66944444in 0.66944444in 0.66944444in;}
    .s1{text-transform:uppercase;color:black;}
    .s2{font-weight:bold;text-transform:uppercase;color:black;}
    .s3{color:black;}
    .s4{font-style:italic;color:black;}
    .s5{font-weight:bold;color:black;}
    .s6{font-weight:bold;font-style:italic;text-transform:uppercase;color:black;}
    .s7{font-weight:bold;color:maroon;}....</style>
</head>
<body>
<p class="p2"></p>
<p class="p2">
<span class="s2">...</span>
</p>
<p class="p2">
<br>
...</body>
</html>

建议在没有类名的情况下进行解析。我需要机器人的标准标签而不是类。

【问题讨论】:

  • 嗨,有更新吗?

标签: android html-parsing apache-poi doc


【解决方案1】:

解析生成的 HTML 并对其进行修改以适合。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-09-10
    • 1970-01-01
    • 2012-12-18
    • 1970-01-01
    相关资源
    最近更新 更多