odt中的Java Open\LibreOffice插入文件\对象 5 月 Questions & Answers 419 是否有任何方法或示例可以使用Open\LibreOffice API for java在odt中嵌入file\object 或者使用其他API或语言
# 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文件包含以下内容: 从上面看,代码清单将打印以下内容: 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”
# 1 楼答案
下面是一个正在实施的片段:
让我们假设“测试”。上面的ods文件包含以下内容:![enter image description here](https://i.stack.imgur.com/ZBL1m.png)
从上面看,代码清单将打印以下内容:
第二个例子是,我正在阅读OpenOffice文本文档的第一段:
在我的类路径上有“odfdom.jar”和“xerces-2.8.0.jar”