[]
        
(Showing Draft Content)

展开或折叠数据透视表字段

GcExcel在IPivotItem接口中提供setShowDetail 方法,该方法允许展开或折叠透视表字段的大纲,方法默认值为True, 显示数据透视表字段的展开状态,设置为False时显示折叠状态。

请参阅以下示例代码以设置两个透视表字段的折叠状态

worksheet.getRange("F1:K16").setValue(sourceData);
worksheet.getRange("F:K").setColumnWidth(15);
IPivotCache pivotcache = workbook.getPivotCaches().create(worksheet.getRange("F1:K16"));
IPivotTable pivottable = worksheet.getPivotTables().add(pivotcache, worksheet.getRange("A1"), "pivottable1");
        
// Config pivot table's fields
IPivotField field_Date = pivottable.getPivotFields().get("Date");
field_Date.setOrientation(PivotFieldOrientation.PageField);

IPivotField field_Category = pivottable.getPivotFields().get("Category");
field_Category.setOrientation(PivotFieldOrientation.ColumnField);

IPivotField field_Country = pivottable.getPivotFields().get("Country");
field_Country.setOrientation(PivotFieldOrientation.RowField);

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

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

// Set not to show Canandian and German details.
field_Country.getPivotItems().get("Canada").setShowDetail(false);
field_Country.getPivotItems().get("Germany").setShowDetail(false);

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

//save to an excel file
workbook.save("SetShowDetail.xlsx");