Apache POI数据透视表-如何在Java中使用“中间”值过滤器

时间:2020-09-16 13:26:05

标签: java excel apache-poi

我正在尝试使用Apache POI设计数据透视表,而我想要做的是首先打印出大于一定数量且小于一定数量的总和值。但是,当我尝试执行此操作时,过滤器会相互抵消,这意味着将打印不应该存在的总和值。例如,如果我希望总和在2到5之间,则总和值为1以及大于5的数字都将被打印出来。这是解释我的问题的代码:

package com.tutorialspoint.spring;
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 org.openxmlformats.schemas.spreadsheetml.x2006.main.CTAutoFilter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCustomFilter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTCustomFilters;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataField;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTDataFields;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFilter;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFilterColumn;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTFilters;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotField;
import org.openxmlformats.schemas.spreadsheetml.x2006.main.STFilterOperator;
import org.springframework.boot.SpringApplication;

import java.util.GregorianCalendar;
import java.util.HashSet;
import java.util.TreeSet;

public class ExcelAutoPivotPracticeApplication {

 public static void main(String[] args) throws Exception {
     SpringApplication.run(ExcelAutoPivotPracticeApplication.class, args);

  try (Workbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("MyExcel5.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", "IntVal", "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", "B1", new GregorianCalendar(2019, 0, 1),  2d},
    new Object[]{"A", "B2", new GregorianCalendar(2019, 0, 1),  4d},
        new Object[]{"B", "B1", new GregorianCalendar(2019, 0, 2),  1d},
    new Object[]{"B", "B2", new GregorianCalendar(2019, 0, 2),  7d},
    new Object[]{"A", "C1", new GregorianCalendar(2019, 0, 1),  5d},
    new Object[]{"A", "C2", new GregorianCalendar(2019, 0, 1),  5d},
    new Object[]{"B", "C1", new GregorianCalendar(2019, 0, 2), 2d},
    new Object[]{"B", "C2", 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]);
     }
     else if (rowData[c] instanceof Integer)
         cell.setCellValue((Integer) rowData[c]);
    }
   }
AreaReference a = new AreaReference("A1:D9", SpreadsheetVersion.EXCEL2007);
   XSSFPivotTable pivotTable = ((XSSFSheet)sheet).createPivotTable(
    a, 
    new CellReference("E4"));
   pivotTable.addRowLabel(0);
   pivotTable.addRowLabel(1);
   pivotTable.addColLabel(2);
   pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 3);
    pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 3);
   pivotTable.getCTPivotTableDefinition().setCompact(false);
   pivotTable.getCTPivotTableDefinition().setCompactData(false);
   pivotTable.getCTPivotTableDefinition().setOutline(true);
   pivotTable.getCTPivotTableDefinition().setOutlineData(true);
   for (CTPivotField pf: pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldList())
   {
    System.out.println("FOO");
    pf.setCompact(false);
    pf.setOutline(true);
    pf.setDefaultSubtotal(true);
   }
   org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFilters filters =
    org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFilters.Factory.newInstance();
   org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFilter field = filters.addNewFilter();
   field.setId(0);
   field.setFld(1);
   field.setType(org.openxmlformats.schemas.spreadsheetml.x2006.main.STPivotFilterType.VALUE_BETWEEN);
   field.setIMeasureFld(0);
    CTFilterColumn myCol = field.addNewAutoFilter().addNewFilterColumn();
   CTCustomFilters myFilter2= myCol.addNewCustomFilters();
   CTCustomFilter custFilt = myFilter2.addNewCustomFilter();
   CTCustomFilter custFilt2 = myFilter2.addNewCustomFilter();
   custFilt.setOperator(STFilterOperator.GREATER_THAN_OR_EQUAL);
   custFilt.setVal("2");
   custFilt2.setOperator(STFilterOperator.LESS_THAN_OR_EQUAL);
   custFilt2.setVal("5");
   field.getAutoFilter().setRef("A1");
   field.getAutoFilter().getFilterColumnArray(0).setColId(0);
    pivotTable.getCTPivotTableDefinition().setFilters(filters);
   workbook.write(fileout);
  }
 }
}

这是运行此代码后将输出的数据透视表: enter image description here

如果您对如何帮助我有任何想法,请告诉我!

1 个答案:

答案 0 :(得分:1)

那么,如何获取*.xlsx文件需要设置的内容? *.xlsx只是ZIP存档。因此,您可以解压缩*.xlsx并查看其中的内容。

使用Excel的{​​{1}}执行所需操作,保存GUI,然后解压缩并查看*.xlsx。在那里,您将找到以下过滤器设置:

/xl/pivotTables/pivotTable1.xml

因此,<filters count="1"> <filter fld="1" type="valueBetween" evalOrder="-1" id="1" iMeasureFld="0"> <autoFilter ref="A1"> <filterColumn colId="0"> <customFilters and="1"> <customFilter operator="greaterThanOrEqual" val="2"/> <customFilter operator="lessThanOrEqual" val="5"/> </customFilters> </filterColumn> </autoFilter> </filter> </filters> 需要设置为customFilters链接了以下过滤器。这是您的代码尚未设置的内容。

完整示例:

AND