如何使用Java中的ApachePOI库对数据透视表数据进行排序
是否可以在Java中使用ApachePOI对XSSF数据透视表数据进行排序?目前,我能够创建透视表、分配标签和添加聚合函数,但在数据排序方面运气不佳。目前我的Java代码如下:如何使用Java中的ApachePOI库对数据透视表数据进行排序,java,excel,sorting,apache-poi,pivot-table,Java,Excel,Sorting,Apache Poi,Pivot Table,是否可以在Java中使用ApachePOI对XSSF数据透视表数据进行排序?目前,我能够创建透视表、分配标签和添加聚合函数,但在数据排序方面运气不佳。目前我的Java代码如下: public XSSFSheet createPivotTable (XSSFSheet datasht, XSSFSheet destinationsheet) { XSSFPivotTable pivotTable = destinationsheet.createPivotTable(new AreaRef
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;
}
有没有人知道如何访问XSSFPivotTable的排序功能,或者知道这是否可行
我研究过的另一个选项是,使用一个模板excel文件,其中包含一个预先存在的透视表,该透视表按照我的要求进行配置(包括排序),并链接到excel中的命名范围。然后,只要在每次执行Java代码时用新数据更新模板文件中的命名范围即可
然而,excel数据透视表确实是一个奇怪的野兽,当您将“模板”excel文档保存为新文件时,数据透视表区域引用始终更新为绝对值,即=OLDFILENAME.xlsx!pivotdatarange与=NAMEFILENAME.xlsx相反!数据透视范围
因此,我对该选项的问题是,是否可以在excel中修改现有透视表的区域引用
干杯
Josh尽管您必须使用CT类,并且可能需要包含另一个依赖项,但是可以对数据透视表中的数据进行排序 要按第一列升序排序,请执行以下操作:
int indexOfSortColumn=0;
数据透视表。getCTPivotTableDefinition()
.getPivotFields()
.getPivotFieldArray(indexOfSortColumn)
.setSortType(STFieldSortType.ASCENDING);
indexOfSortColumn
是输入区域中的索引
如果编译器找不到STFieldSortType,则需要将对
org.apache.poi:poi ooxml模式的依赖替换为对org.apache.poi:ooxml模式的依赖ooxml模式
和poi ooxml模式
都包含ooxml文档的XML模型,但后者被精简了。我有同样的问题,如何自动对数据透视表的数据排序?您已经有了解决方案吗?