Java 如何使用ApachePOI在excel透视表中生成行标签上的sum、average等聚合,而不是列标签?

Java 如何使用ApachePOI在excel透视表中生成行标签上的sum、average等聚合,而不是列标签?,java,excel,apache-poi,pivot-table,Java,Excel,Apache Poi,Pivot Table,我正在尝试使用ApachePOI库生成excel透视表 当我尝试添加多个聚合时,excel将其分组为值(您可以在行标签的第二幅图像右侧看到“值”属性),并将值放在列中。我想在默认情况下生成一个列标签中包含值的excel 但当我添加sum和average等多个聚合(如下图所示)时,值将显示在列标签中值可以拖动到excel中的行标签,但默认情况下我需要行中的值 这些聚合的代码 XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet

我正在尝试使用ApachePOI库生成excel透视表

当我尝试添加多个聚合时,excel将其分组为(您可以在行标签的第二幅图像右侧看到“值”属性),并将放在列中。我想在默认情况下生成一个列标签中包含值的excel

但当我添加sum和average等多个聚合(如下图所示)时,值将显示在列标签中可以拖动到excel中的行标签,但默认情况下我需要行中的值

这些聚合的代码

XSSFWorkbook wb = new XSSFWorkbook();
            XSSFSheet sheet = wb.createSheet();

            //Create some data to build the pivot table on
            setCellData(sheet);
            XSSFPivotTable pivotTable = null;
            try {
                pivotTable = sheet.createPivotTable(new AreaReference("A1:I8", SpreadsheetVersion.EXCEL2007), new CellReference("M10"));
            } catch (Exception ex) {
                System.out.println("In sheet: " + ex);
            }
            //Configure the pivot table
            //Use first column as row label
            pivotTable.addRowLabel(0);
            pivotTable.addRowLabel(1);

            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).setAxis(STAxis.AXIS_COL);
            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).addNewItems();
            pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).getItems().addNewItem()
                    .setT(STItemType.DEFAULT);
            pivotTable.getCTPivotTableDefinition().addNewColFields().addNewField().setX(1);
           //this.addCalculatedColumnToPivotTable(pivotTable, "field1", "average of count ab", "'count'");
            //Sum up the second column
            pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 3);
            //Set the third column as filter
            pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 3);

但是pivot表中没有函数可以在ApachePOI库的行标签中添加这些聚合。

使用最新的ApachePOI版本
4.1.0
添加列标签不再需要使用低级别的基础bean,因为现在已经有了

但是直到现在还没有任何
addRowLabel(DataConsolidateFunction,int columnIndex)
addColumnLabel(DataConsolidateFunction,int columnIndex)
将列和数据字段上的数据添加为列字段。因此,如果需要将数据放在行上,并将数据字段作为行字段,那么我们需要改变这一点

完整示例:

import java.io.FileOutputStream;

import org.apache.poi.ss.*;
import org.apache.poi.ss.usermodel.*;
import org.apache.poi.ss.util.*;
import org.apache.poi.xssf.usermodel.*;

import java.util.GregorianCalendar;

class CreatePivotTable {

 public static void main(String[] args) throws Exception {

  try (Workbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("Excel.xlsx") ) {

   DataFormat format = workbook.createDataFormat();
   CellStyle dateStyle = workbook.createCellStyle();
   dateStyle.setDataFormat(format.getFormat("M\\/d\\/yy"));

   Sheet sheet = workbook.createSheet();

   String[] headers = new String[]{"Column1", "Column2", "Date", "Count"};
   Row row = sheet.createRow(0);
   Cell cell;
   for (int c = 0; c < headers.length; c++) {
    cell = row.createCell(c); cell.setCellValue(headers[c]);
   }

   Object[][] data = new Object[][]{
    new Object[]{"A", "B", new GregorianCalendar(2019, 0, 1), 2d},
    new Object[]{"A", "B", new GregorianCalendar(2019, 0, 1), 4d},
    new Object[]{"A", "B", new GregorianCalendar(2019, 0, 2), 1d},
    new Object[]{"A", "B", new GregorianCalendar(2019, 0, 2), 7d},
    new Object[]{"A", "C", new GregorianCalendar(2019, 0, 1), 5d},
    new Object[]{"A", "C", new GregorianCalendar(2019, 0, 1), 5d},
    new Object[]{"A", "C", new GregorianCalendar(2019, 0, 2), 2d},
    new Object[]{"A", "C", new GregorianCalendar(2019, 0, 2), 8d}
   };
   for (int r = 0; r < data.length; r++) {
    row = sheet.createRow(r+1);
    Object[] rowData = data[r];
    for (int c = 0; c < rowData.length; c++) {
     cell = row.createCell(c);
     if (rowData[c] instanceof String) {
      cell.setCellValue((String)rowData[c]);
     } else if (rowData[c] instanceof GregorianCalendar) {
      cell.setCellValue((GregorianCalendar)rowData[c]);
      cell.setCellStyle(dateStyle);
     } else if (rowData[c] instanceof Double) {
      cell.setCellValue((Double)rowData[c]);
     }
    }
   }

   XSSFPivotTable pivotTable = ((XSSFSheet)sheet).createPivotTable(new AreaReference("A1:D9", SpreadsheetVersion.EXCEL2007), new CellReference("M10"));

   pivotTable.addRowLabel(0);
   pivotTable.addRowLabel(1);

   pivotTable.addColLabel(2);

   // the default sets data on columns and data fields as col fields
   pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 3);
   pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 3);

   // now changing from the default
   // set dataOnRows
   pivotTable.getCTPivotTableDefinition().setDataOnRows(true);

   // add a new row field for data fields 
   pivotTable.getCTPivotTableDefinition().getRowFields().addNewField().setX(-2);
   pivotTable.getCTPivotTableDefinition().getRowFields().setCount(3);

   // remove data fields from col fields
   pivotTable.getCTPivotTableDefinition().getColFields().removeField(1);
   pivotTable.getCTPivotTableDefinition().getColFields().setCount(1);

   workbook.write(fileout);

  }

 }
}
import java.io.FileOutputStream;
导入org.apache.poi.ss.*;
导入org.apache.poi.ss.usermodel.*;
导入org.apache.poi.ss.util.*;
导入org.apache.poi.xssf.usermodel.*;
导入java.util.GregorianCalendar;
类创建数据透视表{
公共静态void main(字符串[]args)引发异常{
尝试(工作簿=新XSSFWORKWORK();
FileOutputStream fileout=新的FileOutputStream(“Excel.xlsx”)){
DataFormat=workbook.createDataFormat();
CellStyle dateStyle=workbook.createCellStyle();
dateStyle.setDataFormat(format.getFormat(“M\\/d\\/yy”);
工作表=工作簿.createSheet();
字符串[]头=新字符串[]{“Column1”、“Column2”、“Date”、“Count”};
Row Row=sheet.createRow(0);
细胞;
for(int c=0;c
rowFields
colFields
中的
field
元素中的
x
属性通常指定
pivotField
项值的索引。但是
数据字段
可能与
数据透视字段
s没有直接关系

如果只有一个
数据字段
,则只有一种可能显示该字段。因此,不需要
字段
元素

但如果有多个
数据字段
s,则在透视表的
GUI
视图中还有一个名为
Values
的附加字段。在透视表
XML
中,使用
-2
对附加字段进行索引


因此,
pivotTableDefinition
中的
dataOnRows
确定是在行(
dataOnRows=“true”
)上还是在列(默认)上显示
数据字段。而
rowFields
colFields
中的
field
元素中的
x
属性
-2
指定附加
字段在字段列表中的排列位置

使用最新的
apachepoi
version
4.1.0
添加列标签不再需要使用底层bean,因为现在已经有了

但是直到现在还没有任何
addRowLabel(DataConsolidateFunction,int columnIndex)
addColumnLabel(DataConsolidateFunction,int columnIndex)
将列和数据字段上的数据添加为列字段。所以我们需要改变,如果