【问题标题】:Java Open\LibreOffice insert file\object in odtJava Open\LibreOffice 在 odt 中插入文件\对象
【发布时间】:2018-10-19 12:22:40
【问题描述】:

有什么方法或示例如何使用 Open\LibreOffice API for java 将文件\对象嵌入 odt 中?

或者使用其他一些 API 或语言。

【问题讨论】:

    标签: java libreoffice openoffice-writer odt


    【解决方案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...
        }
    }
    

    假设上面的“test.ods”文件有以下内容:

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

    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”。

    【讨论】:

    • 嗯,谢谢。示例是关于如何插入文本并阅读它。但不能将文件附加到文档中,例如 Word 中的 OLE 对象。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-02-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-10-06
    相关资源
    最近更新 更多