代码之家  ›  专栏  ›  技术社区  ›  Vito Karleone

odt中的Java Open\LibreOffice insert file\object

  •  0
  • Vito Karleone  · 技术社区  · 7 年前

    有没有什么方法或例子可以使用Open\LibreOffice API for java在odt中嵌入file\object?

    或者使用其他API或语言。

    1 回复  |  直到 7 年前
        1
  •  1
  •   Kamlesh Samrit    7 年前

    下面是它的一个片段:

        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...
        }
    }
    

    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...
        }
    }