简体   繁体   English

具有水平列的Apache POI数据透视表

[英]Apache POI pivot table with horizontal columns

I hava data for excel like this 我喜欢这样的excel数据 在此处输入图片说明 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. 顾名思义, addRowLabel添加了行标签。 But you wants column C (2) of your data sheet to be a column label. 但是您希望data表的列C (2)成为列标签。 Up to apache poi version 3.17, the only way to achieve this is using the underlaying low level objects. apache poi版本3.17之前,实现此目的的唯一方法是使用底层低层对象。 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. 研究内部结构的最佳方法是使用Excel的GUI创建一个具有数据透视表的*.xlsx文件。 This *.xlsx file is nothing else than a ZIP archive. *.xlsx文件不过是一个ZIP存档。 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 . 在那里,我们将在/xl/worksheets找到工作表的XML文件,在/xl/pivotTables数据透视表定义,在/xl/pivotCache找到数据透视表缓存。 According to this XML we then can programming using the low level objects of package org.openxmlformats.schemas.spreadsheetml.x2006.main.* . 然后,根据此XML我们可以使用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 ). API - XSSFPivotTable中所示,还有进一步的开发( addColLabel )。 But the current (May 2018) latest stable version apache poi version 3.17 does not providing this. 但是当前(2018年5月)最新的稳定版本apache poi版本3.17没有提供此功能。

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 ). 5替换为所需的数字(我假设2 )。 This code turns the axis of the column 该代码旋转列的轴

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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