excel如何使用Java中的Apache POI库对数据透视表数据进行排序
可以在Java中使用Apache POI对XSSF数据透视表数据进行排序吗?目前,我能够创建透视表、分配标签和添加聚合函数,但在数据排序方面运气不佳。目前我的Java代码如下:
public XSSFSheet createPivotTable (XSSFSheet datasht, XSSFSheet destinationsheet) {
XSSFPivotTable pivotTable = destinationsheet.createPivotTable(new AreaReference("$A:$AV"), new CellReference("A1"), datasht);
pivotTable.addRowLabel(0);
pivotTable.addColumnLabel(DataConsolidateFunction.MIN, 25);
pivotTable.addColumnLabel(DataConsolidateFunction.MIN, 24);
pivotTable.addColumnLabel(DataConsolidateFunction.MIN, 46);
pivotTable.addColumnLabel(DataConsolidateFunction.MIN, 47);
/*
Insert code to sort pivot table data in ascending order here
*/
return destinationsheet;
}
有没有人知道如何访问XSSF数据透视表的排序功能,或者知道这是否可行
我研究过的另一个选项是,使用一个模板excel文件,其中包含一个预先存在的数据透视表,配置了我想要的方式(包括排序),该数据透视表链接到excel中的一个命名范围。然后,每次执行Java代码时,只需用新数据更新模板文件中的命名范围
然而,excel透视表确实是一个奇怪的野兽,当您将“模板”excel文档保存为新文件时,透视表区域引用始终更新为绝对值,即=OLDFILENAME。xlsx!pivotdatarange,而不是=NAMEFILENAME。xlsx!数据透视范围
因此,我对该选项的问题是,是否可以在excel中修改现有透视表的区域引用
干杯
乔希
# 1 楼答案
可以在pivot表中对数据进行排序,不过您必须使用CT类,并且可能需要包含另一个依赖项
要按第一列升序排序,请执行以下操作:
indexOfSortColumn
是输入区域中的索引如果编译器找不到STFieldSortType,则需要将对
org.apache.poi:poi-ooxml-schemas
的依赖项替换为对org.apache.poi:ooxml-schemas
的依赖项ooxml-schemas
和poi-ooxml-schemas
都包含OOXML文档的XML模型,但后者被精简了