JavaApachePOI编写Excel

JavaApachePOI编写Excel,java,excel,apache-poi,pivot-table,Java,Excel,Apache Poi,Pivot Table,我在Java应用程序中工作,以编写excel。我正在使用ApachePOI库。我需要创建透视表。我能够使用下面的代码创建透视表和列总和 CellReference topLeft = new CellReference(0, 0); CellReference bottomRight = new CellReference(10, 3); AreaReference aref = new AreaReference(topLeft, bottomRight); CellReference pos

我在Java应用程序中工作,以编写excel。我正在使用ApachePOI库。我需要创建透视表。我能够使用下面的代码创建透视表和列总和

CellReference topLeft = new CellReference(0, 0);
CellReference bottomRight = new CellReference(10, 3);
AreaReference aref = new AreaReference(topLeft, bottomRight);
CellReference pos = new CellReference(0, 0);
XSSFSheet pivotSheet = workbook.createSheet("PivotSheet");
XSSFPivotTable pivotTable = pivotSheet.createPivotTable(aref,pos,dataSheet)
pivotOrgWiseSheet.setDisplayGridlines(true);
pivotTable.addRowLabel(0);
pivotTable.addRowLabel(1);
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 2, "Sum of column3");
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 3, "Sum of column4");
但是上面的代码生成了类似excel的代码

但我不知道为什么关键字“values”出现在第二列标题中,也不知道是否可以将值“Row Label”更改为自定义文本,如“Category”

我想要下面这样的


我不知道如何删除关键字“Values”,但我想要将标题更改为自定义字符串,我们必须获取值并设置它?

Excel
中,在
显示
组中的
分析
选项
选项卡上有一个设置
字段标题
。这将在显示和隐藏字段标题之间切换

org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotTableDefinition
中的相应设置是
setShowHeaders

所以

应该在透视表中隐藏字段标题

但是您想要的结果的图片看起来更像是数据头是可见的,但是
RowHeaderCaption
被更改,并且包含
DataCaption
的第一行被隐藏。这将是:

...
//pivotTable.getCTPivotTableDefinition().setShowHeaders(false);
pivotTable.getCTPivotTableDefinition().setRowHeaderCaption("Category");
pivotTable.getCTPivotTableDefinition().setDataCaption("Changed Data Caption");
CellUtil.getRow(pos.getRow(), pivotSheet).setZeroHeight(true);
...

Excel
中,在
Show
组的
Analyze
Options
选项卡上有一个设置
fieldheaders
。这将在显示和隐藏字段标题之间切换

org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotTableDefinition
中的相应设置是
setShowHeaders

所以

应该在透视表中隐藏字段标题

但是您想要的结果的图片看起来更像是数据头是可见的,但是
RowHeaderCaption
被更改,并且包含
DataCaption
的第一行被隐藏。这将是:

...
//pivotTable.getCTPivotTableDefinition().setShowHeaders(false);
pivotTable.getCTPivotTableDefinition().setRowHeaderCaption("Category");
pivotTable.getCTPivotTableDefinition().setDataCaption("Changed Data Caption");
CellUtil.getRow(pos.getRow(), pivotSheet).setZeroHeight(true);
...