[]
        
(Showing Draft Content)

过滤数据透视表

GcExcel 能够让你在数据透视表中过滤出特定的值。你可以通过 IPivotItem 接口的 setVisible 方法来隐藏行字段或是页面,也可以利用 IPivotField 接口的 getPivotFilters 方法,这可以让你向数据透视表的字段中增加标签、数值以及日期类型的过滤器,具体是通过向 IPivotFilters 接口的 add 方法传递一个 PivotFilterType 枚举参数实现的。

更进一步地,GcExcel 还提供了 IPivotTable 接口下的 setAllowMultipleFilters 方法,使得在同一 PivotField 上同时应用标签和数值过滤成为可能。该方法的默认设定值为 true,意味着允许多重过滤。但若你将该方法的值设为 false,则意味着后设置的过滤器会覆盖先前的设置。

type=warning

注意:setAllowMultipleFilters 的值由 true 更改为 false 时,这一操作将会影响所有的 IPivotField。在这种情况下,如果某个 IPivotField 同时设置了标签过滤器和数值过滤器,GcExcel 会将这两种过滤器都清除掉。

GcExcel 还提供了几种清除过滤器的方法:ClearLabelFilter 专门用来移除标签过滤器;clearValueFilter 则负责清除数值过滤器;而 clearAllFilters 如其名所示,能够一次性清除字段上设置的所有过滤器。

type=warning

注意: 如果你尝试通过 add 方法同时添加两个同类型的标签过滤器或数值过滤器,后添加的那个过滤器将会自动替换掉先添加的那个,不会存在两个相同类型的过滤器共存的情况。

type=warning

注意: IPivotFilter 接口里的多数方法都是仅供读取的,唯有 WholeDayFilter 方法例外,它允许读写操作。然而,直接修改 WholeDayFilter 值并不会自动促使当前的数据透视表进行刷新。所以,为了确保过滤结果的更新或刷新,你应当手动调用 IPivotTable 接口所提供的 update 或者 refresh 方法。

应用过滤器

请参考以下示例代码来使用 Visible 属性隐藏行字段或页面:

// Create a new workbook.
Workbook workbook = new Workbook();

// Add data for the pivot table.
Object sourceData = new Object[][]{
        {"Order ID", "Product", "Category", "Amount", "Date", "Country"},
        {1, "Bose 785593-0050", "Consumer Electronics", 4270, new GregorianCalendar(2018, 0, 6), "United States"},
        {2, "Canon EOS 1500D", "Consumer Electronics", 8239, new GregorianCalendar(2018, 0, 7), "United Kingdom"},
        {3, "Haier 394L 4Star", "Consumer Electronics", 617, new GregorianCalendar(2018, 0, 8), "United States"},
        {4, "IFB 6.5 Kg FullyAuto", "Consumer Electronics", 8384, new GregorianCalendar(2018, 0, 10), "Canada"},
        {5, "Mi LED 40inch", "Consumer Electronics", 2626, new GregorianCalendar(2018, 0, 10), "Germany"},
        {6, "Sennheiser HD 4.40-BT", "Consumer Electronics", 3610, new GregorianCalendar(2018, 0, 11), "United States"},
        {7, "Iphone XR", "Mobile", 9062, new GregorianCalendar(2018, 0, 11), "Australia"},
        {8, "OnePlus 7Pro", "Mobile", 6906, new GregorianCalendar(2018, 0, 16), "New Zealand"},
        {9, "Redmi 7", "Mobile", 2417, new GregorianCalendar(2018, 0, 16), "France"},
        {10, "Samsung S9", "Mobile", 7431, new GregorianCalendar(2018, 0, 16), "Canada"},
        {11, "OnePlus 7Pro", "Mobile", 8250, new GregorianCalendar(2018, 0, 16), "Germany"},
        {12, "Redmi 7", "Mobile", 7012, new GregorianCalendar(2018, 0, 18), "United States"},
        {13, "Bose 785593-0050", "Consumer Electronics", 1903, new GregorianCalendar(2018, 0, 20), "Germany"},
        {14, "Canon EOS 1500D", "Consumer Electronics", 2824, new GregorianCalendar(2018, 0, 22), "Canada"},
        {15, "Haier 394L 4Star", "Consumer Electronics", 6946, new GregorianCalendar(2018, 0, 24), "France"},
};

// Access first worksheet.
IWorksheet worksheet = workbook.getWorksheets().get(0);
        
// Add values to the range.
worksheet.getRange("G1:L16").setValue(sourceData);
        
// Set column width.
worksheet.getRange("G:L").setColumnWidth(15);
        
// Create pivot cache.
IPivotCache pivotcache = workbook.getPivotCaches().create(worksheet.getRange("G1:L16"));
        
// Add data to the pivot table.
IPivotTable pivottable = worksheet.getPivotTables().add(pivotcache, worksheet.getRange("A1"), "pivottable1");
        
// Set number format.
worksheet.getRange("J1:J16").setNumberFormat("$#,##0.00");

// Configure pivot table's fields.
IPivotField field_Product = pivottable.getPivotFields().get(1);
field_Product.setOrientation(PivotFieldOrientation.RowField);

IPivotField field_Amount = pivottable.getPivotFields().get(3);
field_Amount.setOrientation(PivotFieldOrientation.DataField);
field_Amount.setNumberFormat("$#,##0.00");

IPivotField field_Country = pivottable.getPivotFields().get(5);
field_Country.setOrientation(PivotFieldOrientation.PageField);

// Add row field filter.
field_Product.getPivotItems().get("Bose 785593-0050").setVisible(false);
field_Product.getPivotItems().get("Haier 394L 4Star").setVisible(false);
field_Product.getPivotItems().get("Iphone XR").setVisible(false);

// Add page filter.
field_Country.getPivotItems().get("United States").setVisible(false);
field_Country.getPivotItems().get("Canada").setVisible(false);

worksheet.getRange("A:B").getEntireColumn().autoFit();
    
// Save WorkBook.
workbook.save("FilterPivotTable.xlsx");

应用标签过滤器

请参考以下示例代码,根据项目名称中包含 "mi" 的产品来进行过滤:

// Configure pivot table's fields.
var field_Category = pivottable.getPivotFields().get("Category");
field_Category.setOrientation(PivotFieldOrientation.ColumnField);

var field_Product = pivottable.getPivotFields().get("Product");
field_Product.setOrientation(PivotFieldOrientation.RowField);

var field_Amount = pivottable.getPivotFields().get("Amount");
field_Amount.setOrientation(PivotFieldOrientation.DataField);
field_Amount.setNumberFormat("$#,##0.00");

// Filter for products where the product name contains 'mi'.
field_Product.getPivotFilters().add(PivotFilterType.CaptionContains, "mi");

worksheet.getRange("A:D").getEntireColumn().autoFit();

// Save the workbook.
workbook.save("PivotLabelFilter.xlsx");

应用数值过滤器

请参考示例代码,根据数值来过滤产品:

// Configure pivot table's fields.
var field_Category = pivottable.getPivotFields().get("Category");
field_Category.setOrientation(PivotFieldOrientation.ColumnField);

var field_Product = pivottable.getPivotFields().get("Product");
field_Product.setOrientation(PivotFieldOrientation.RowField);

var field_Amount = pivottable.getPivotFields().get("Amount");
field_Amount.setOrientation(PivotFieldOrientation.DataField);
field_Amount.setNumberFormat("$#,##0.00");

PivotFilterOptions options = new PivotFilterOptions();
options.setAppliedDataField(0);
        
// Filter for products with sales volume greater than 7000.
field_Product.getPivotFilters().add(PivotFilterType.ValueGreaterThan, 7000, null, options);

worksheet.getRange("A:D").getEntireColumn().autoFit();

// Save the workbook.
workbook.save("PivotValueFilter.xlsx");

type=warning

注意:你必须使用 PivotFilterOptions 类的 setAppliedDataField 方法,并结合数值过滤器,来指定依据哪个数据字段进行过滤。

应用日期过滤器

请参考以下示例代码,用于过滤在两个指定日期之间的产品:

// Configure pivot table's fields.
var field_Category = pivottable.getPivotFields().get("Category");
field_Category.setOrientation(PivotFieldOrientation.ColumnField);

var field_Product = pivottable.getPivotFields().get("Product");
field_Product.setOrientation(PivotFieldOrientation.RowField);

var field_Date = pivottable.getPivotFields().get("Date");
field_Date.setOrientation(PivotFieldOrientation.RowField);

var field_Amount = pivottable.getPivotFields().get("Amount");
field_Amount.setOrientation(PivotFieldOrientation.DataField);
field_Amount.setNumberFormat("$#,##0.00");

// Filter for products between two dates.
field_Date.getPivotFilters().add(PivotFilterType.DateBetween, LocalDate.of(2018, 1, 1), LocalDate.of(2018, 1, 15));

worksheet.getRange("A:D").getEntireColumn().autoFit();

// Save the workbook.
workbook.save("PivotDateFilter.xlsx");

应用前10过滤器

请参考示例代码,根据数值来过滤排名前十二的产品:

// Configure pivot table's fields.
var field_Category = pivottable.getPivotFields().get("Category");
field_Category.setOrientation(PivotFieldOrientation.ColumnField);

var field_Product = pivottable.getPivotFields().get("Product");
field_Product.setOrientation(PivotFieldOrientation.RowField);

var field_Amount = pivottable.getPivotFields().get("Amount");
field_Amount.setOrientation(PivotFieldOrientation.DataField);
field_Amount.setNumberFormat("$#,##0.00");

// Filter for top 12 products.
PivotFilterOptions options = new PivotFilterOptions();
options.setIsTop10Type(true);
field_Product.getPivotFilters().add(PivotFilterType.Count, 12, null, options);

worksheet.getRange("A:D").getEntireColumn().autoFit();

// Save the workbook.
workbook.save("PivotTopTenFilter.xlsx");

应用多重过滤器

请参考以下示例代码,使用标签和数值过滤器来共同筛选产品:

// Configure pivot table's fields.
var field_Category = pivottable.getPivotFields().get("Category");
field_Category.setOrientation(PivotFieldOrientation.ColumnField);

var field_Product = pivottable.getPivotFields().get("Product");
field_Product.setOrientation(PivotFieldOrientation.RowField);

var field_Amount = pivottable.getPivotFields().get("Amount");
field_Amount.setOrientation(PivotFieldOrientation.DataField);
field_Amount.setNumberFormat("$#,##0.00");

// Allow adding label filter and value filter to a field at the same time.
pivottable.setAllowMultipleFilters(true);

// Filter for products with sales volume greater than 7000.
PivotFilterOptions options = new PivotFilterOptions();
options.setAppliedDataField(0);
field_Product.getPivotFilters().add(PivotFilterType.ValueGreaterThan, 7000, null, options);

// Filter for products where the product name contains 'mi'.
field_Product.getPivotFilters().add(PivotFilterType.CaptionContains, "mi");

worksheet.getRange("A:D").getEntireColumn().autoFit();

// Save the workbook.
workbook.save("MultipleFilter.xlsx");