简体   繁体   English

如何过滤 Pivot 表使用 getFilter() 方法使用 Apache POI

[英]How to filters on Pivot Table using getFilter() Method using Apache POI

Is there any filters to apply on pivot table other than reportFilter.除了 reportFilter 之外,是否还有其他过滤器可应用于 pivot 表。

             pivotTable.getCTPivotTableDefinition().setFilters(filters);

How to use above setFilters Method如何使用上面的 setFilters 方法

For using the CTPivotTableDefinition.setFilters method you need at first creating a filters object of type org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFilters .要使用CTPivotTableDefinition.setFilters方法,您首先需要创建一个filters object 类型为org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFilters

Let's have a complete example which shows that.让我们举一个完整的例子来说明这一点。

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 java.util.GregorianCalendar;

class CreatePivotTableFilter {

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

  try (Workbook workbook = new XSSFWorkbook(); 
       FileOutputStream fileout = new FileOutputStream("Excel.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", "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]);
     }
    }
   }

   XSSFPivotTable pivotTable = ((XSSFSheet)sheet).createPivotTable(
    new AreaReference("A1:D9", 
    SpreadsheetVersion.EXCEL2007), 
    new CellReference("F4"));

   pivotTable.addRowLabel(0);
   pivotTable.addRowLabel(1);

   pivotTable.addColLabel(2);

   pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 3);
   pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 3);

   //create CTPivotFilters having filter for field 1 caption begins with "B"
   org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFilters filters =
    org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFilters.Factory.newInstance();
   org.openxmlformats.schemas.spreadsheetml.x2006.main.CTPivotFilter filter = filters.addNewFilter();
   filter.setId(0);
   filter.setFld(1);
   filter.setType(org.openxmlformats.schemas.spreadsheetml.x2006.main.STPivotFilterType.CAPTION_BEGINS_WITH);
   filter.setStringValue1("B");
   filter.addNewAutoFilter().addNewFilterColumn().addNewCustomFilters().addNewCustomFilter().setVal("B*");
   filter.getAutoFilter().setRef("A1");
   filter.getAutoFilter().getFilterColumnArray(0).setColId(0);

   //set filters to pivot table definition
   pivotTable.getCTPivotTableDefinition().setFilters(filters);

   workbook.write(fileout);

  }

 }
}

Unfortunately there is not any documentation about the ooxml schemas, the low level basic objects of apache poi , public available.不幸的是,没有任何关于 ooxml 模式的文档, apache poi的低级基本对象,公开可用。 So we need downloading the sources of ooxml-schemas and then doing javadoc form those to get an API documentation which describes the classes and methods.因此,我们需要下载ooxml-schemas的源代码,然后从这些源代码中生成javadoc以获得描述类和方法的API文档。

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

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