简体   繁体   English

使用UNO将新行插入LibreOffice writer表中

[英]Insert new row into LibreOffice writer table using UNO

I have recently tried to code a small java file which will insert a row into an already existing table in a .odt document. 我最近尝试编码一个小的Java文件,它将在.odt文档中的现有表中插入一行。 The table itself has 4 rows and 3 column, but I would like to implement a check which will expand that table if the content to be inserted is larger than 4. However, every time I try to get the table's rows, it returns a null pointer. 该表本身具有4行和3列,但是我想实现一个检查,如果要插入的内容大于4,它将对该表进行扩展。但是,每次我尝试获取表的行时,它都会返回null指针。 I am not that familiar with UNO api, but as far as i read through the documentation, the class XColumnsAndRowRange should be used in this situation. 我对UNO api不太熟悉,但是就我通读文档而言,在这种情况下应使用XColumnsAndRowRange类。 My code is as follows: 我的代码如下:

XTextTablesSupplier xTablesSupplier = (XTextTablesSupplier) UnoRuntime.queryInterface(XTextTablesSupplier.class, xTextDocument);

    XNameAccess xNamedTables = xTablesSupplier.getTextTables();
    try {
        Object table = xNamedTables.getByName(tblName);
        XTextTable xTable = (XTextTable) UnoRuntime.queryInterface(XTextTable.class, table);
        XCellRange xCellRange = (XCellRange) UnoRuntime.queryInterface(XCellRange.class, table);
        if(flag){
            XColumnRowRange xCollumnAndRowRange =(XColumnRowRange) 
                    UnoRuntime.queryInterface(XColumnRowRange.class, xCellRange);
                XTableRows rows = xCollumnAndRowRange.getRows();

                 System.out.println("Testing if this works");
                rows.insertByIndex(4, size-4);
        }

I am not sure if I am missing something here or if I should be using a different function. 我不确定在这里是否缺少某些内容,或者是否应该使用其他功能。

As Lyrl suggested, this works: 正如Lyrl所建议的,这可以工作:

XTableRows rows = xTable.getRows();

Apparently XColumnRowRange is only used for spreadsheets. 显然XColumnRowRange仅用于电子表格。

Note: With Basic or Python you would not have this problem, because those languages do not need queryInterface . 注意:使用Basic或Python,您将不会遇到此问题,因为这些语言不需要queryInterface The code would simply be: 该代码将是:

table = tables.getByName(tblName)
rows = table.getRows()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 LibreOffice UNO Writer获取单元名称 - LibreOffice UNO Writer get cell name 如何在libreoffice writer文档中插入数据url /二进制文件以替换图像? - How to insert a data url/binary to replace an image in a libreoffice writer document? 如何使用Hibernate在表中插入新行? - How can I insert new row in a table using Hibernate? 如何在LibreOffice中调试Java UNO? - How to debug Java UNO in LibreOffice? LibreOffice UNO:设置样式(可以使用Java,VB,Python,C ++或使用UNO API的任何语言来提供) - LibreOffice UNO: Setting Styles (anwer can be provided in Java, VB, Python, C++, any language using UNO API) 如何使用自定义表模型将新行插入JTable? - How do I insert new row into a JTable using a custom table model? 使用 Java Spring 和 CrudRepository,是否可以在不为每一列填充值的情况下向表中插入新行? - Using Java Spring and CrudRepository, is it possible to insert a new row into a table without filling every column with a value? 在 java 中使用 JpaRepository 插入一个新行 - Insert a new row using JpaRepository in java 使用 Open/LibreOffice 开始使用 UNO 和 Java - Getting started with UNO and Java with Open/LibreOffice 我在Java中使用Writer类进行UTF8编码输出。 编写时如何插入新行? - I am using Writer class in Java for UTF8 encoding output. How do I insert new line when writing?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM