切片器未过滤数据透视表数据。
我正在使用Java的Aspose单元库创建数据透视表和切片器。 但是,当我为二维数据透视表添加切片器时,可以在excel工作表中看到它,但无法正常工作。当我在数据透视表中的“筛选器连接”->“筛选器连接”中手动打勾时,它可以工作。
如何从代码中获得这项工作
try {
Workbook wbook = new Workbook("invoice.xlsx");
WorksheetCollection worksheets = wbook.getWorksheets();
Worksheet workSheet = worksheets.get("Aging_Details");
PivotTableCollection pivotTables =
workSheet.getPivotTables();
String firstCell = workSheet.getCells().getFirstCell().getName();
String lastCell = workSheet.getCells().getLastCell().getName();
int ptableIndex = workSheet.getCells().getLastCell().getRow() +5;
String pivotIndex= "J"+ptableIndex;
String source = "="+"A1"+":"+"DC8472";
System.out.println(source);
int index = pivotTables.add(source, pivotIndex, "PivotTable20");
PivotTable pivotTable = pivotTables.get(index);
pivotTable.setRowGrand(false);
pivotTable.setColumnGrand(true);
pivotTable.addFieldToArea(PivotFieldType.ROW, 8);
pivotTable.addFieldToArea(PivotFieldType.COLUMN, 76);
pivotTable.addFieldToArea(PivotFieldType.DATA, 41);
PivotField pf= pivotTable.getDataFields().get(0);
pf.setFunction(ConsolidationFunction.SUM);
//Set refresh data flag true
pivotTable.setRefreshDataFlag(true);
//Refresh and calculate pivot table
pivotTable.calculateData();
pivotTable.getPivotFilters().get(ind);
pivotTable.refreshData();
pivotTable.setRefreshDataFlag(false);
int idx = workSheet.getSlicers().add(pivotTable, "C"+
(ptableIndex), pivotTable.getBaseFields().get(76));
Slicer slicer = workSheet.getSlicers().get(idx);
SlicerCacheItemCollection scItems =
slicer.getSlicerCache().getSlicerCacheItems();
slicer.setNumberOfColumns(2);
slicer.setStyleType(SlicerStyleType.SLICER_STYLE_LIGHT_6);
slicer.getParent().setSelected(true);
Style style1 = wbook.createStyle();
com.aspose.cells.Font font1 = style1.getFont();
font1.setColor(Color.getGreen());
Style style2 = wbook.createStyle();
com.aspose.cells.Font font2 = style2.getFont();
font2.setColor(Color.getBlue());
int i =
wbook.getWorksheets().getTableStyles().addPivotTableStyle("tt");
// Get and Set the table style for different categories
TableStyle ts = wbook.getWorksheets().getTableStyles().get(i);
int index2 =
ts.getTableStyleElements().
add(TableStyleElementType.FIRST_COLUMN);
TableStyleElement e = ts.getTableStyleElements().get(index2);
e.setElementStyle(style1);
index2 =
ts.getTableStyleElements().add(
TableStyleElementType.GRAND_TOTAL_ROW);
e = ts.getTableStyleElements().get(index2);
e.setElementStyle(style2);
pivotTable.setPivotTableStyleName("tt");
System.out.println(filepath);
wbook.save(filepath);
}catch (Exception e) {
e.printStackTrace();
}