java - XSSF(Apache POI) - 从数据透视表中的单列值添加多列标签

标签 java excel apache-poi pivot-table

我目前正在使用 Apache POI 3.12 添加数据透视表。这是我的 sample.xlsx 文件:

enter image description here

现在我使用以下代码为上述数据创建数据透视表。

    File excel = new File("sample.xlsx"); 
    FileInputStream fis = new FileInputStream(excel); 
    XSSFWorkbook wb = new XSSFWorkbook(fis); 
    XSSFSheet sheet = wb.getSheetAt(0); 
    XSSFPivotTable pivotTable = sheet.createPivotTable(new AreaReference("A3:C7"), new CellReference("E3"));
    pivotTable.addRowLabel(0);
    pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1);
    pivotTable.addDataColumn(1, true);
    pivotTable.addReportFilter(2);
    FileOutputStream fileOut = new FileOutputStream("output.xlsx");
    wb.write(fileOut);
    fileOut.close();
    wb.close();

我的 output.xlsx 文件有以下数据透视表:

enter image description here

当我要在 Excel 中编辑数据透视表时,它会在页面字段而不是列字段中添加年份列。实际上我需要以下结果:

enter image description here

我无法从单个列值添加多个列标签。请你帮助我好吗?提前致谢

最佳答案

XSSFPivotTable 类处于@Beta 状态。所以这只能使用底层的低级对象。

XSSFWorkbook wb = new XSSFWorkbook(new FileInputStream("sample.xlsx")); 
XSSFSheet sheet = wb.getSheetAt(0);

//the following creates a Pivot Table with 3 PivotFields (0 to 2) (3 Columns A3:C7); all dataField="false" at first 
XSSFPivotTable pivotTable = sheet.createPivotTable(new AreaReference(new CellReference("A3"), new CellReference("C7")), new CellReference("E3"));

//the following makes PivotFields(0) an Axis-Field AXIS_ROW with 5 Items (5 Rows A3:C7). Why one Item for each row? I don't know.
//and it adds a new RowField for this
pivotTable.addRowLabel(0);

//the following makes PivotFields(1) a DataField and creates a DataColumn for this
pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1);
//pivotTable.addDataColumn(2, false); //not neccessary since addColumnLabel already adds a DataColumn

//now PivotFields(2) needs to be an Axis-Field AXIS_COL
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).setAxis(
  org.openxmlformats.schemas.spreadsheetml.x2006.main.STAxis.AXIS_COL);

//PivotFields(2) needs to have at least one Item  
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).addNewItems();
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2).getItems().addNewItem().setT(
  org.openxmlformats.schemas.spreadsheetml.x2006.main.STItemType.DEFAULT);

//new ColField needs to be added
pivotTable.getCTPivotTableDefinition().addNewColFields().addNewField().setX(2);

//pivotTable.addReportFilter(2);
FileOutputStream fileOut = new FileOutputStream("output.xlsx");
wb.write(fileOut);
fileOut.close();
wb.close();

pivotTable.addDataColumn(1, true); 不是必需的,因为 addColumnLabel 已经添加了一个 DataColumn

关于java - XSSF(Apache POI) - 从数据透视表中的单列值添加多列标签,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35943812/

相关文章:

vba - 在 Excel 中通过 VBA 发布文档

excel - CRTL + Tilde 然后将 "exploded"公式复制粘贴到 excel 中

apache-poi - Apache poi-3.10-FINAL-20140208.jar 下载链接

java - 如何在 Eclipse IDE 中将 JSON 关联到字符串

java - 从 jackson 序列化的现有领域派生新领域?

excel - 更改 Excel for Mac 的 VBE 的默认字体大小

java - 如何复制 .xlsx 完整工作簿

java - 在 struts 2 应用程序中没有 Action 映射错误

java - Android 蓝牙服务发现在两个不同的设备上产生不同的结果 - 是 Android 版本吗?

java - 求和的递归函数