如何使用 Docx4J 向现有 table 添加行

How to add rows to an existing table using Docx4J

我有一个 Sheet 的现有工作簿,其中有一个 table('table' 指的是 Excel [= 的插入 Table 功能46=] 与列 headers 和过滤箭头等)。我无法确定使用哪个 类 来编辑 table 的现有行或将新行插入 table.

我已经成功地(在很大程度上要感谢 )将新的单元格内容写入现有单元格以及创建全新的 cells/rows。我宁愿不必先写 header 行和所有数据行,然后使用 API 将它们变成 table,但我想知道是否有人知道如何应该完成。过去,我可以只用我知道需要的行数创建 table,但在这种情况下,行数是动态的。我希望我可以只引用一个 tablePart 然后会有一些方法可以插入到列表 object.

感谢任何指导。

编辑:

举个具体的例子,假设我有一个工作簿和 sheet,现有 table 2 列 2 行(包括 header)从 A1 开始。我可以打开底层的 xl>tables>table1.xml 并看到这个:


    <table xmlns="http://schemas.openxmlformats.org/spreadsheetml/2006/main" xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006" mc:Ignorable="xr xr3" xmlns:xr="http://schemas.microsoft.com/office/spreadsheetml/2014/revision" xmlns:xr3="http://schemas.microsoft.com/office/spreadsheetml/2016/revision3" id="5" xr:uid="{FAABA541-34FC-423B-94F5-DDD8D784132E}" name="SummarySFTP" displayName="SummarySFTP" ref="A1:B2" totalsRowShown="0" headerRowDxfId="46" headerRowBorderDxfId="45" tableBorderDxfId="44">
        <autoFilter ref="A1:B2" xr:uid="{93499C15-75FB-4436-A9B9-0C1FCBD787F4}">
            <filterColumn colId="0" hiddenButton="0"/>
            <filterColumn colId="1" hiddenButton="0"/>
        </autoFilter>
        <tableColumns count="2">
            <tableColumn id="1" xr3:uid="{D4DA50CD-C581-4286-9B64-42B02B6646B6}" name="Status"/>
            <tableColumn id="2" xr3:uid="{9D4F40B3-B530-42E1-92E9-86D5632CA191}" name="Quantity" dataDxfId="43"/>
        </tableColumns>
        <tableStyleInfo showFirstColumn="0" showLastColumn="0" showRowStripes="1" showColumnStripes="0"/>
    </table>

我可以看到我的两列,根标签的 ref 属性以及 autoFilter 块的 ref 属性。我想要做的是添加一个新行,使 table 的面积为 A1:B3.

在 docx4j 网络应用程序中检查包含 table 的 xlsx 文件,它看起来相当简单。

它为 table 部分的内容生成代码,例如:

CTTable table = smlObjectFactory.createCTTable(); 
JAXBElement<org.xlsx4j.sml.CTTable> tableWrapped = smlObjectFactory.createTable(table); 
    // Create object for autoFilter
    CTAutoFilter autofilter = smlObjectFactory.createCTAutoFilter(); 
    table.setAutoFilter(autofilter); 
        autofilter.setRef( "A2:B4"); 
    // Create object for tableColumns
    CTTableColumns tablecolumns = smlObjectFactory.createCTTableColumns(); 
    table.setTableColumns(tablecolumns); 
        tablecolumns.setCount( new Long(2) );
        // Create object for tableColumn
        CTTableColumn tablecolumn = smlObjectFactory.createCTTableColumn(); 
        tablecolumns.getTableColumn().add( tablecolumn); 
            tablecolumn.setTotalsRowFunction(org.xlsx4j.sml.STTotalsRowFunction.NONE);
            tablecolumn.setName( "Column1"); 
            tablecolumn.setId( 1 );
        // Create object for tableColumn
        CTTableColumn tablecolumn2 = smlObjectFactory.createCTTableColumn(); 
        tablecolumns.getTableColumn().add( tablecolumn2); 
            tablecolumn2.setTotalsRowFunction(org.xlsx4j.sml.STTotalsRowFunction.NONE);
            tablecolumn2.setName( "Column2"); 
            tablecolumn2.setId( 2 );
    // Create object for tableStyleInfo
    CTTableStyleInfo tablestyleinfo = smlObjectFactory.createCTTableStyleInfo(); 
    table.setTableStyleInfo(tablestyleinfo); 
        tablestyleinfo.setName( "TableStyleMedium2"); 
    table.setTableType(org.xlsx4j.sml.STTableType.WORKSHEET);
    table.setHeaderRowCount( new Long(1) );
    table.setTotalsRowCount( new Long(0) );
    table.setName( "Table1"); 
    table.setId( 1 );
    table.setRef( "A2:B4"); 
    table.setDisplayName( "Table1"); 

一旦你知道了行数,你可以看到你需要在 2 个地方写它,使用 setRef 和 autofilter.setRef