【问题标题】:How to collapse all fields in pivot table from java?如何从java折叠数据透视表中的所有字段?
【发布时间】:2018-08-28 13:06:54
【问题描述】:

我在 JAVA 中使用 apache poi 创建一个数据透视表,它会生成一个如下所示的数据透视表,所有行默认展开
我如何生成数据透视表将所有行从java代码折叠如下。

提前谢谢你。
用于生成数据透视表的代码

            AreaReference a=new AreaReference("A1:G5667", null);
            CellReference b=new CellReference("A1");
            XSSFSheet pivot_sheet=workbook.createSheet("Pivot_table");
            XSSFPivotTable pivotTable = pivot_sheet.createPivotTable(a,b,spreadsheet);
            pivotTable.addRowLabel(6);
            pivotTable.addRowLabel(0);
            pivotTable.addRowLabel(2);
            pivotTable.addRowLabel(3);
            pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 4,"Sum");

【问题讨论】:

标签: java excel apache-poi pivot-table


【解决方案1】:

您提供的代码不会导致第一个显示的结果,因为它没有设置不同的列标签。它仅将列E 添加为数据合并列。

但我还是会尝试回答。所以我假设列G 应该是第一行标签。列A 应为第二行标签,应折叠,列C 应为第三行标签。但是D列是包含“APR 2018”、“MAY 2018”、“JUN 2018”的列,应该是列标签。

问题是apache poi 在创建数据透视表时没有分析内容。因此,它只需为每个数据透视字段添加与数据范围内的行一样多的“默认”数据透视字段项。而且它只创建了一个非常基本的枢轴缓存。只要我们只使用自 Excel 以来的默认值,然后在渲染数据透视表时纠正此问题,它就可以工作。但是如果我们需要其他默认值,例如折叠行标签,那么这将失败。

因此,我们需要在 A 列中具有唯一的内容,以便计算所需的数据透视项并正确创建数据透视缓存。然后,我们需要将尽可能多的枢轴字段项从“默认”更改为真正的枢轴字段项,因为不同的内容在列A 中。这些数据透视字段项必须具有指向数据透视缓存的属性x。并且必须正确创建枢轴缓存,在列A 中具有单个唯一内容。另外sd属性必须设置为false,表示隐藏该项目的详细信息。

完整示例:

Excel:

代码:

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.FileOutputStream;
import java.io.FileInputStream;

import java.util.List;
import java.util.Set;
import java.util.HashSet;

class ExcelPivotTableTest {

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

  XSSFWorkbook workbook = (XSSFWorkbook)WorkbookFactory.create(new FileInputStream("PivotExample.xlsx"));
  XSSFSheet dataSheet = workbook.getSheet("Data");

  XSSFSheet pivotSheet = workbook.createSheet("Pivot");

  AreaReference a = new AreaReference("A1:G" + (dataSheet.getLastRowNum() + 1), SpreadsheetVersion.EXCEL2007);
  CellReference b = new CellReference("A1");

  XSSFPivotTable pivotTable = pivotSheet.createPivotTable(a, b, dataSheet);

  pivotTable.addRowLabel(6); //column G as first row label

  pivotTable.addRowLabel(0); //column A as second row label - shall be collapsed

  //we need unique contents in column A for creating the pivot cache
  Set<String> colAValues = new HashSet<String>();
  for (int r = 1; r < dataSheet.getLastRowNum() + 1; r++) {
   Row row = dataSheet.getRow(r);
   if (row != null) {
    Cell cell = row.getCell(0);
    if (cell != null) {
     colAValues.add(cell.toString());
    }
   }
  }

  //now go through all pivot items of first pivot field 
  List<org.openxmlformats.schemas.spreadsheetml.x2006.main.CTItem> itemList = 
   pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(0).getItems().getItemList();
  int i = 0; 
  org.openxmlformats.schemas.spreadsheetml.x2006.main.CTItem item = null;
  for (String value : colAValues) { //as long as there are different column A values
   item = itemList.get(i);
   item.unsetT(); //unset the type "default"
   item.setX(i++); //set x
   pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields()
    .getCacheFieldArray(0).getSharedItems().addNewS().setV(value); //create pivot cache entry
   item.setSd(false); //set sd false = indicates that the details are hidden for this item
  }
  while (i < itemList.size()) {
   item = itemList.get(i++);
   item.setSd(false);
  }


  pivotTable.addRowLabel(2); //column C as third row label

  pivotTable.addRowLabel(3); //column D as row label - shall be column label instead
  //do changing column D to a col label
  pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(3)
   .setAxis(org.openxmlformats.schemas.spreadsheetml.x2006.main.STAxis.AXIS_COL); //AXIS_COL
  //remove column D from RowFields
  pivotTable.getCTPivotTableDefinition().getRowFields().removeField(3); 
  pivotTable.getCTPivotTableDefinition().getRowFields().setCount(3);
  //create ColFields for column D
  pivotTable.getCTPivotTableDefinition().addNewColFields().addNewField().setX(3); 
  pivotTable.getCTPivotTableDefinition().getColFields().setCount(1);

  pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 4, "Sum");

  workbook.write(new FileOutputStream("PivotExample_New.xlsx"));
  workbook.close();

 }
}

此代码需要ooxml-schemas-1.3.jar,如apache poi FAQ 中所述。

结果:

【讨论】:

  • 非常感谢,它完全符合我的要求。太棒了:)
猜你喜欢
  • 2016-11-09
  • 2018-03-18
  • 2012-11-05
  • 2015-10-07
  • 1970-01-01
  • 1970-01-01
  • 2011-09-07
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多