简体   繁体   中英

Apache POI pivot table with horizontal columns

I hava data for excel like this 在此处输入图片说明 When I want to generate pivot table with code below

XSSFSheet pivot = wb.createSheet("pivot");
AreaReference areaReference = new AreaReference("A2:D" + i, SpreadsheetVersion.EXCEL2007);
CellReference cellReference = new CellReference("A1");

XSSFPivotTable pivotTable = pivot.createPivotTable(areaReference, cellReference, sheet);

pivotTable.addRowLabel(1);
pivotTable.addRowLabel(2);

pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 3);

I got next result 在此处输入图片说明 But want something like this 在此处输入图片说明

What I do wrong?

As it's name tells addRowLabel adds a row label. But you wants column C (2) of your data sheet to be a column label. Up to apache poi version 3.17, the only way to achieve this is using the underlaying low level objects. But this needs knowledge about the internals of pivot tables.

Best way to investigate the internals is creating a *.xlsx file having a pivot table using Excel 's GUI. This *.xlsx file is nothing else than a ZIP archive. So we can unzip it and taking a look into. There we will find XML files for the worksheets in /xl/worksheets , the pivot table definitions in /xl/pivotTables and the pivot caches in /xl/pivotCache . According to this XML we then can programming using the low level objects of package org.openxmlformats.schemas.spreadsheetml.x2006.main.* .

Example:

在此处输入图片说明

Code:

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

import java.io.FileInputStream;
import java.io.FileOutputStream;

class PivotTableCreating {

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

  XSSFWorkbook wb = (XSSFWorkbook)WorkbookFactory.create(new FileInputStream("PivotExample.xlsx"));

  XSSFSheet pivotSheet = wb.createSheet("pivot");

  XSSFSheet dataSheet = wb.getSheet("data");
  int lastRowNum = dataSheet.getLastRowNum();

  XSSFPivotTable pivotTable = pivotSheet.createPivotTable(
   new AreaReference(new CellReference("data!A1"), new CellReference("data!D" + (lastRowNum+1)), 
   SpreadsheetVersion.EXCEL2007),
   new CellReference("A5"));

  pivotTable.addRowLabel(1);

  pivotTable.addRowLabel(2); //column C as a row label = RowField and AXIS_ROW

  //do changing column C as a col label
  pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(2)
   .setAxis(org.openxmlformats.schemas.spreadsheetml.x2006.main.STAxis.AXIS_COL); //AXIS_COL

  //remove column C from RowFields
  pivotTable.getCTPivotTableDefinition().getRowFields().removeField(1); 
  pivotTable.getCTPivotTableDefinition().getRowFields().setCount(1);

  //create ColFields for column C
  pivotTable.getCTPivotTableDefinition().addNewColFields().addNewField().setX(2); 
  pivotTable.getCTPivotTableDefinition().getColFields().setCount(1);

  pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 3);

  wb.write(new FileOutputStream("PivotExampleNew.xlsx"));
  wb.close();

 }
}

Result:

在此处输入图片说明

As shown in the API - XSSFPivotTable there is further development ( addColLabel ). But the current (May 2018) latest stable version apache poi version 3.17 does not providing this.

Try to perfrom following code on your date column:

pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(5).setAxis(STAxis.AXIS_COL)
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(5).addNewItems();
pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(5).getItems().addNewItem()
            .setT(STItemType.DEFAULT);

Replace the 5 with the number you need (I assume 2 ). This code turns the axis of the column

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM