有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!


共 (1) 个答案

  1. # 1 楼答案

    下面是一个正在实施的片段:

        public static void main(String[] args) {
        try {
            OdfDocument odfDoc = OdfDocument.loadDocument(new File("/home/geertjan/test.ods"));
            OdfFileDom odfContent = odfDoc.getContentDom();
            XPath xpath = odfDoc.getXPath();
            DTMNodeList nodeList = (DTMNodeList) xpath.evaluate("//table:table-row/table:table-cell[1]", odfContent, XPathConstants.NODESET);
            for (int i = 0; i < nodeList.getLength(); i++) {
                Node cell = nodeList.item(i);
                if (!cell.getTextContent().isEmpty()) {
                    System.out.println(cell.getTextContent());
                }
            }
        } catch (Exception ex) {
            //Handle...
        }
    }
    

    让我们假设“测试”。上面的ods文件包含以下内容: enter image description here

    从上面看,代码清单将打印以下内容:

    Cuthbert
    Algernon
    Wilbert
    

    第二个例子是,我正在阅读OpenOffice文本文档的第一段:

    public static void main(String[] args) {
        try {
            OdfDocument odfDoc = OdfDocument.loadDocument(new File("/home/geertjan/chapter2.odt"));
            OdfFileDom odfContent = odfDoc.getContentDom();
            XPath xpath = odfDoc.getXPath();
            OdfParagraphElement para = (OdfParagraphElement) xpath.evaluate("//text:p[1]", odfContent, XPathConstants.NODE);
            System.out.println(para.getFirstChild().getNodeValue());
        } catch (Exception ex) {
            //Handle...
        }
    }
    

    在我的类路径上有“odfdom.jar”和“xerces-2.8.0.jar”