如何在Apache POI数据透视表报表filter中设置默认值

我有一个工作表中的数据,我试图创build一个数据透视表与报告filter。 我想将默认值设置为报表filter。

pivotTable.addReportFilter(13); 

列包含0和1,我想在报告filter中设置0作为我的默认值。

起初这个问题在现在被问到的这个大概情况下是不能回答的。 使用apache poi创build数据透视表直到现在都处于beta状态。 所以我们不仅需要高级的apache poi API,还需要底层的低级对象。 我们需要准确地知道数据透视表中的数据types。 为了一般能够从所有types的数据创build数据透视表,如Excel可以做的那样,需要更多的努力。 数十年来,微软已经通过大型程序员团队进行了编程。 从这个apache poi很远。

到目前为止,如果在数据范围内存在行,apache poi会添加types为“default”( <item t="default"/> )的枢轴字段项目。 这是因为他们不想查看数据,所以他们假设的值与数据中的行数不同。

这很好,因为Excel将在打开时重build其透视caching。 但是,如果我们要预先select项目,那么这是不好的。 那么我们必须知道哪些项目可以预选。

因此,我们至less需要尽可能多的项目,因为我们要预选,作为编号项目: <item x="0"/><item x="1"/><item x="2"/>

我们需要构build一个具有这些项目共享元素的caching定义。

例:

 import org.apache.poi.xssf.usermodel.*; import org.apache.poi.ss.usermodel.*; import org.apache.poi.ss.util.*; import java.util.Random; import java.io.*; class PivotTableTest4 { private static void setCellData(Sheet sheet) { Row row = sheet.createRow(0); Cell cell = row.createCell(0); cell.setCellValue("Name"); cell = row.createCell(1); cell.setCellValue("Value1"); cell = row.createCell(2); cell.setCellValue("Value2"); cell = row.createCell(3); cell.setCellValue("City"); for (int r = 1; r < 15; r++) { row = sheet.createRow(r); cell = row.createCell(0); cell.setCellValue("Name " + ((r-1) % 4 + 1)); cell = row.createCell(1); cell.setCellValue(r * new java.util.Random().nextDouble()); cell = row.createCell(2); cell.setCellValue(r * new java.util.Random().nextDouble()); cell = row.createCell(3); cell.setCellValue("City " + ((r-1) % 3 + 1)); } } public static void main(String[] args) { try { XSSFWorkbook wb = new XSSFWorkbook(); XSSFSheet sheet = wb.createSheet(); //Create some data to build the pivot table on setCellData(sheet); XSSFPivotTable pivotTable = sheet.createPivotTable( new AreaReference(new CellReference("A1"), new CellReference("D15")), new CellReference("H5")); //Configure the pivot table //Use first column as row label pivotTable.addRowLabel(0); //Sum up the second column pivotTable.addColumnLabel(DataConsolidateFunction.SUM, 1); //Avarage the third column pivotTable.addColumnLabel(DataConsolidateFunction.AVERAGE, 2); //Add fourth column as page filter pivotTable.addReportFilter(3); /* Apache poi adds 15 pivot field items of type "default" (<item t="default"/>) here. This is because there are 15 rows (A1:D15) and, because they don't have a look at the data, they are assuming max 15 different values. This is fine because Excel will rebuild its pivot cache while opening. But if we want preselect items, then this is not fine. Then we must know what items there are that can be preselected. So we need at least as much items as we want preselecting as numbered items: <item x="0"/><item x="1"/><item x="2"/>... And we must build a cache definition which has shared elements for those items. */ for (int i = 0; i < 3; i++) { //take the first 3 items as numbered items: <item x="0"/><item x="1"/><item x="2"/> pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(3).getItems().getItemArray(i).unsetT(); pivotTable.getCTPivotTableDefinition().getPivotFields().getPivotFieldArray(3).getItems().getItemArray(i).setX((long)i); //build a cache definition which has shared elements for those items //<sharedItems><sv="City 1"/><sv="City 2"/><sv="City 3"/></sharedItems> pivotTable.getPivotCacheDefinition().getCTPivotCacheDefinition().getCacheFields().getCacheFieldArray(3).getSharedItems().addNewS().setV("City " + (i+1)); } //Now we can predefinite a page filter. Second item, which is "City 2", in this case. pivotTable.getCTPivotTableDefinition().getPageFields().getPageFieldArray(0).setItem(1); FileOutputStream fileOut = new FileOutputStream("PivotTableTest4.xlsx"); wb.write(fileOut); fileOut.close(); wb.close(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } } 

这需要在FAQ中提到的所有模式的完整jar, ooxml-schemas-1.3.jar