[]
IPivotField 是 IPivotFields 集合的一个成员,用于描述源字段在数据透视表报表中的使用方式,包括隐藏字段。使用此接口可以访问字段元数据、更改字段方向、配置排序和筛选,以及处理行、列、页或数据字段。
worksheet.getRange("A1:B5").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 120},
{"Fruit", 80},
{"Vegetables", 95},
{"Vegetables", 60}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Category");
field.setOrientation(PivotFieldOrientation.RowField);
voidvoidvoidvoidvoidvoidbooleanIPivotField 的计算项集合。PivotFieldCalculation 值,该值表示指定字段执行的计算类型。intbooleanbooleanbooleanbooleanbooleanbooleanbooleanbooleangetName()IPivotField 的数据透视筛选器集合。intbooleanIPivotField 的项目标签是否重复显示。booleanvoidgroup(PivotFieldCustomGroupOptions options) voidgroup(PivotFieldDateGroupOptions options) voidgroup(PivotFieldNumberGroupOptions options) voidsetBaseField(String value) voidsetBaseItem(String value) voidPivotFieldCalculation 值,该值指定在 PivotTable 中如何计算数据字段。voidsetCurrentPage(int value) voidsetDragToColumn(boolean value) voidsetDragToData(boolean value) voidsetDragToHide(boolean value) voidsetDragToPage(boolean value) voidsetDragToRow(boolean value) voidsetEnableMultiplePageItems(boolean value) voidsetFormula(String value) voidsetFunction(ConsolidationFunction value) voidsetLayoutBlankLine(boolean value) voidsetLayoutCompactRow(boolean value) voidsetLayoutForm(LayoutFormType value) voidvoidvoidsetNumberFormat(String value) voidvoidsetPosition(int value) voidsetRepeatLabels(boolean value) IPivotField 重复项标签。voidsetShowAllItems(boolean value) voidsetSubtotals(EnumSet<SubtotalType> value) voidungroup()当通过隐藏一个或多个项应用了手动筛选时,返回 false。使用 clearManualFilter() 可恢复所有项的可见性。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Product", "Amount"},
{"Example Speaker", 4270},
{"Example Camera", 8239},
{"Example Appliance", 617}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "PivotTable1");
IPivotField productField = pivotTable.getPivotFields().get("Product");
productField.setOrientation(PivotFieldOrientation.RowField);
productField.getPivotItems().get("Example Camera").setVisible(false);
boolean allItemsVisible = productField.getAllItemsVisible();
true;否则为 false。如果通过 autoSort(SortOrder,String) 配置了自动排序,则此方法返回为该规则指定的排序键字段名称。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Product", "Region", "Amount"},
{"Apple", "East", 120},
{"Orange", "West", 80},
{"Apple", "West", 150},
{"Orange", "East", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
IPivotField regionField = pivotTable.getPivotFields().get("Region");
regionField.setOrientation(PivotFieldOrientation.RowField);
IPivotField amountField = pivotTable.getPivotFields().get("Amount");
pivotTable.addDataField(amountField, "Sum of Amount", ConsolidationFunction.Sum);
regionField.autoSort(SortOrder.Descending, amountField.getSourceName());
String autoSortField = regionField.getAutoSortField();
如果通过 autoSort(SortOrder) 或 autoSort(SortOrder,String) 配置了自动排序,则此方法返回应用于该字段的 SortOrder。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Product", "Region", "Amount"},
{"Apple", "East", 120},
{"Orange", "West", 80},
{"Apple", "West", 150},
{"Orange", "East", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
IPivotField regionField = pivotTable.getPivotFields().get("Region");
regionField.setOrientation(PivotFieldOrientation.RowField);
regionField.autoSort(SortOrder.Descending);
SortOrder autoSortOrder = regionField.getAutoSortOrder();
此属性仅对数据字段有效。当数据字段配置为通过自定义计算(例如 PivotFieldCalculation.PercentOf)显示值时,可将此属性与 getCalculation() 一起使用。
Object[][] sourceData = {
{"Product", "Country", "Amount"},
{"Bike", "Australia", 100},
{"Bike", "Canada", 80}
};
worksheet.getRange("A1:C3").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C3"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"));
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Country").setOrientation(PivotFieldOrientation.ColumnField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
IPivotField dataField = pivotTable.getDataFields().get("Sum of Amount");
dataField.setCalculation(PivotFieldCalculation.PercentOf);
dataField.setBaseField("Country");
String baseField = dataField.getBaseField();
此属性仅对数据字段有效。当数据字段配置为按自定义计算(例如 PivotFieldCalculation.PercentOf)显示值时,请将此属性与 getCalculation() 一起使用。
Object[][] sourceData = {
{"Product", "Country", "Amount"},
{"Bike", "Australia", 100},
{"Bike", "Canada", 80}
};
worksheet.getRange("A1:C3").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C3"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"));
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Country").setOrientation(PivotFieldOrientation.ColumnField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
IPivotField dataField = pivotTable.getDataFields().get("Sum of Amount");
dataField.setCalculation(PivotFieldCalculation.PercentOf);
dataField.setBaseField("Country");
value - 用于自定义计算的基础字段名称。此属性仅对数据字段有效。可将此属性与 getBaseField() 和 getCalculation() 一起使用,以确定自定义计算(例如 PivotFieldCalculation.PercentOf)所使用的基字段中的项。
Object[][] sourceData = {
{"Product", "Country", "Amount"},
{"Bike", "Australia", 100},
{"Bike", "Canada", 80}
};
worksheet.getRange("A1:C3").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C3"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"));
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Country").setOrientation(PivotFieldOrientation.ColumnField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
IPivotField dataField = pivotTable.getDataFields().get("Sum of Amount");
dataField.setCalculation(PivotFieldCalculation.PercentOf);
dataField.setBaseField("Country");
dataField.setBaseItem("Australia");
String baseItem = dataField.getBaseItem();
此属性仅对数据字段有效。将此属性与 getBaseField() 和 getCalculation() 一起使用,以确定基础字段中哪个项目被用于 PivotFieldCalculation.PercentOf 等自定义计算。
Object[][] sourceData = {
{"Product", "Country", "Amount"},
{"Bike", "Australia", 100},
{"Bike", "Canada", 80}
};
worksheet.getRange("A1:C3").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C3"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"));
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Country").setOrientation(PivotFieldOrientation.ColumnField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
IPivotField dataField = pivotTable.getDataFields().get("Sum of Amount");
dataField.setCalculation(PivotFieldCalculation.PercentOf);
dataField.setBaseField("Country");
dataField.setBaseItem("Australia");
value - 基础字段中用于自定义计算的项目名称。PivotFieldCalculation 值,该值表示指定字段执行的计算类型。此属性仅对数据字段有效。
Object[][] sourceData = {
{"Product", "Country", "Amount"},
{"Bike", "Australia", 100},
{"Bike", "Canada", 80}
};
worksheet.getRange("A1:C3").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C3"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"));
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Country").setOrientation(PivotFieldOrientation.ColumnField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
IPivotField dataField = pivotTable.getDataFields().get("Sum of Amount");
dataField.setCalculation(PivotFieldCalculation.PercentOf);
PivotFieldCalculation calculation = dataField.getCalculation();
PivotFieldCalculation 值,指示该字段执行的计算类型。PivotFieldCalculation 值,该值指定在 PivotTable 中如何计算数据字段。此属性仅对数据字段有效。使用它可将数据字段从默认计算更改为自定义计算,例如 PivotFieldCalculation.PercentOf。
Object[][] sourceData = {
{"Product", "Country", "Amount"},
{"Bike", "Australia", 100},
{"Bike", "Canada", 80}
};
worksheet.getRange("A1:C3").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C3"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"));
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Country").setOrientation(PivotFieldOrientation.ColumnField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
IPivotField dataField = pivotTable.getDataFields().get("Sum of Amount");
dataField.setCalculation(PivotFieldCalculation.PercentOf);
value - 指定数据字段计算类型的 PivotFieldCalculation 值。此方法仅对页面字段有效。返回的索引从零开始。值 -1 表示页面字段显示的是 All 或多个项目。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Region", "Amount"},
{"East", 100},
{"West", 200},
{"East", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField pageField = pivotTable.getPivotFields().get("Region");
pageField.setOrientation(PivotFieldOrientation.PageField);
int currentPage = pageField.getCurrentPage();
All 或多个项目,则为 -1。此方法仅对页字段有效。返回的索引从零开始。值 -1 表示页字段正在显示 All 或多个项。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Region", "Amount"},
{"East", 100},
{"West", 200},
{"East", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField pageField = pivotTable.getPivotFields().get("Region");
pageField.setOrientation(PivotFieldOrientation.PageField);
pageField.setCurrentPage(0);
value - 页字段当前页项的从零开始的索引;如果页字段正在显示 All 或多个项,则为 -1。此属性指示是否可以将该字段移动到数据透视表报表的列区域。默认值为 true。使用 setDragToColumn(boolean) 可更改此设置。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"West", "Pear", 200},
{"East", "Pear", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Product");
field.setDragToColumn(false);
boolean canDragToColumn = field.getDragToColumn();
true;否则为 false。此属性指示是否可以将该字段移动到数据透视表报表的列区域。默认值为 true。使用此方法可更改此设置。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"West", "Pear", 200},
{"East", "Pear", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Product");
field.setDragToColumn(false);
value - 如果该字段可以拖动到列位置,则为 true;否则为 false。此属性指示该字段是否可以移动到 PivotTable 报告的数据区域中。默认值为 true。使用 setDragToData(boolean) 可更改此设置。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"West", "Pear", 200},
{"East", "Pear", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Amount");
field.setDragToData(false);
boolean canDragToData = field.getDragToData();
true;否则为 false。此属性指示该字段是否可以移动到 PivotTable 报告的数据区域中。默认值为 true。使用此方法可更改此设置。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"West", "Pear", 200},
{"East", "Pear", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Amount");
field.setDragToData(false);
value - 如果字段可以拖动到数据位置,则为 true;否则为 false。此属性指示用户是否可以通过将该字段拖走来将其从 PivotTable 报表中移除。默认值为 true。使用 setDragToHide(boolean) 可更改此设置。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"West", "Pear", 200},
{"East", "Pear", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Product");
field.setDragToHide(false);
boolean canDragToHide = field.getDragToHide();
true;否则为 false。此属性指示用户是否可以通过将该字段拖走来将其从 PivotTable 报表中移除。默认值为 true。使用此方法可更改此设置。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"West", "Pear", 200},
{"East", "Pear", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Product");
field.setDragToHide(false);
value - 如果字段可通过拖出 PivotTable 报告来隐藏,则为 true;否则为 false。默认值为 true。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"West", "Pear", 200},
{"East", "Pear", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Region");
field.setDragToPage(false);
boolean canDragToPage = field.getDragToPage();
true;否则为 false。默认值为 true。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"West", "Pear", 200},
{"East", "Pear", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Region");
field.setDragToPage(false);
value - 如果该字段可以拖动到页面位置,则为 true;否则为 false。此属性确定是否允许将字段移动到 PivotTable 中的行区域。默认值为 true。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"West", "Pear", 200},
{"East", "Pear", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Region");
boolean canDragToRow = field.getDragToRow();
true;否则为 false。此属性确定是否允许将字段移动到 PivotTable 中的行区域。默认值为 true。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"West", "Pear", 200},
{"East", "Pear", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Region");
field.setDragToRow(true);
value - 如果字段可以拖动到行位置,则为 true;否则为 false。此属性适用于放置在页面区域中的 PivotTable 字段。默认值为 false。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Date", "Amount"},
{"2024-01", 120},
{"2024-02", 150},
{"2024-03", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField pageField = pivotTable.getPivotFields().get("Date");
pageField.setOrientation(PivotFieldOrientation.PageField);
boolean allowMultipleItems = pageField.getEnableMultiplePageItems();
true;否则为 false。此属性适用于放置在页区域中的 PivotTable 字段。默认值为 false。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Date", "Amount"},
{"2024-01", 120},
{"2024-02", 150},
{"2024-03", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField pageField = pivotTable.getPivotFields().get("Date");
pageField.setOrientation(PivotFieldOrientation.PageField);
pageField.setEnableMultiplePageItems(true);
value - 如果字段可以在页字段中选择多个项,则为 true;否则为 false。此属性返回与字段关联的公式文本。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Product", "Amount"},
{"Apple", 100},
{"Banana", 200},
{"Orange", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField field = pivotTable.getCalculatedFields().add("TotalAmount", "=Amount*2");
String formula = field.getFormula();
null。此属性设置与该字段关联的公式文本。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Product", "Amount"},
{"Apple", 100},
{"Banana", 200},
{"Orange", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField field = pivotTable.getCalculatedFields().add("TotalAmount", "=Amount*2");
field.setFormula("=Amount*3");
value - 采用 A1 样式表示法并使用宏语言的公式字符串。如果没有与该字段关联的公式,则设置为 null。此属性仅适用于数据字段。使用它可确定字段是否通过求和、计数或平均值等函数汇总源值。若要更改汇总函数,请参见 setFunction(ConsolidationFunction)。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 10},
{"Fruit", 20},
{"Drink", 5}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField dataField = pivotTable.addDataField(pivotTable.getPivotFields().get("Amount"), "Sum of Amount", ConsolidationFunction.Sum);
ConsolidationFunction function = dataField.getFunction();
ConsolidationFunction。此方法仅适用于数据字段。使用 ConsolidationFunction 值(例如 ConsolidationFunction.Sum、 ConsolidationFunction.Count 或 ConsolidationFunction.Average)可控制字段值的聚合方式。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 120},
{"Fruit", 80},
{"Drink", 50}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
pivotTable.getDataFields().get("Sum of Amount").setFunction(ConsolidationFunction.Average);
value - 用于汇总 PivotTable 数据字段的汇总函数。此属性适用于放置在数据透视表行区域中的 PivotFields。默认值为 false。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Product", "Amount"},
{"Apple", 100},
{"Apple", 200},
{"Banana", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"));
IPivotField field = pivotTable.getPivotFields().get("Product");
field.setOrientation(PivotFieldOrientation.RowField);
field.setLayoutBlankLine(true);
boolean blankLine = field.getLayoutBlankLine();
true;否则为 false。此属性适用于放置在数据透视表行区域中的 PivotFields。默认值为 false。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Product", "Amount"},
{"Apple", 100},
{"Apple", 200},
{"Banana", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"));
IPivotField field = pivotTable.getPivotFields().get("Product");
field.setOrientation(PivotFieldOrientation.RowField);
field.setLayoutBlankLine(true);
value - 如果在数据透视表报告中的此行字段后插入空行,则为 true;否则为 false。当此属性为 true 时,多个行 PivotField 中的项目可以显示在单个列中。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Category", "Product", "Amount"},
{"Fruit", "Apple", 100},
{"Fruit", "Orange", 80},
{"Vegetables", "Carrot", 60},
{"Vegetables", "Celery", 40}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"));
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
IPivotField field = pivotTable.getPivotFields().get("Product");
field.setOrientation(PivotFieldOrientation.RowField);
field.setLayoutCompactRow(true);
boolean compactRow = field.getLayoutCompactRow();
true;否则为 false。当此属性为 true 时,来自多个行 PivotField 的项可以显示在单列中。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Category", "Product", "Amount"},
{"Fruit", "Apple", 100},
{"Fruit", "Orange", 80},
{"Vegetables", "Carrot", 60},
{"Vegetables", "Celery", 40}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"));
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
IPivotField field = pivotTable.getPivotFields().get("Product");
field.setOrientation(PivotFieldOrientation.RowField);
field.setLayoutCompactRow(true);
value - 如果选择行时 PivotField 以紧凑行布局显示,则为 true;否则为 false。使用此属性可确定该字段是以表格格式显示还是以大纲格式显示。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Beverages", 100},
{"Beverages", 200},
{"Snacks", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Category");
field.setOrientation(PivotFieldOrientation.RowField);
field.setLayoutForm(LayoutFormType.Outline);
LayoutFormType layoutForm = field.getLayoutForm();
LayoutFormType.Tabularor LayoutFormType.Outline。使用 LayoutFormType.Tabular 以表格形式显示该字段,或使用LayoutFormType.Outline以大纲形式显示。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 120},
{"Fruit", 80},
{"Vegetables", 95}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Category");
field.setOrientation(PivotFieldOrientation.RowField);
field.setLayoutForm(LayoutFormType.Outline);
value - 要应用于数据透视表字段的布局形式,例如LayoutFormType.Tabular或 LayoutFormType.Outline。使用此方法可确定该字段的小计是显示在字段项的上方还是下方。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Beverages", 100},
{"Beverages", 200},
{"Snacks", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Category");
field.setOrientation(PivotFieldOrientation.RowField);
field.setLayoutSubtotalLocation(SubtotalLocationType.Bottom);
SubtotalLocationType location = field.getLayoutSubtotalLocation();
SubtotalLocationType 值。使用 SubtotalLocationType.Top 可在字段项上方显示小计,或使用 SubtotalLocationType.Bottom 在字段项下方显示小计。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Beverages", 100},
{"Beverages", 200},
{"Snacks", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Category");
field.setOrientation(PivotFieldOrientation.RowField);
field.setLayoutSubtotalLocation(SubtotalLocationType.Bottom);
value - 字段的小计位置。使用此方法可获取 PivotTable 报表中字段的当前名称。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 120},
{"Drink", 50},
{"Fruit", 80}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Category");
String name = field.getName();
使用此方法可设置 PivotTable 报告中字段的名称。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 120},
{"Drink", 50},
{"Fruit", 80}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Category");
field.setName("Product Category");
value - 对象的名称。返回的字符串对应于 setNumberFormat(String) 分配的格式代码,并用于在 PivotTable 报告中显示字段值。
Object[][] sourceData = new Object[][] {
{"Category", "Amount"},
{"Fruit", 1200},
{"Fruit", 800},
{"Vegetables", 600}
};
worksheet.getRange("A1:B4").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField amountField = pivotTable.getPivotFields().get("Amount");
amountField.setOrientation(PivotFieldOrientation.DataField);
amountField.setNumberFormat("$#,##0.00");
String numberFormat = amountField.getNumberFormat();
Object[][] sourceData = new Object[][] {
{"Category", "Amount"},
{"Fruit", 1200},
{"Fruit", 800},
{"Vegetables", 600}
};
worksheet.getRange("A1:B4").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField amountField = pivotTable.getPivotFields().get("Amount");
amountField.setOrientation(PivotFieldOrientation.DataField);
amountField.setNumberFormat("$#,##0.00");
value - 数据透视字段的数字格式代码。使用此方法可确定字段当前放置在 PivotTable 的行、列、页、数据或隐藏区域中。
Object[][] sourceData = new Object[][] {
{"Product", "Category", "Amount"},
{"Apple", "Fruit", 100},
{"Pear", "Fruit", 200}
};
worksheet.getRange("A1:C3").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C3"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "pivotTable1");
IPivotField field = pivotTable.getPivotFields().get("Category");
field.setOrientation(PivotFieldOrientation.RowField);
PivotFieldOrientation orientation = field.getOrientation();
PivotFieldOrientation 值。使用此方法可将字段放置在数据透视表的行、列、页或数据区域中。
Object[][] sourceData = {
{"Category", "Product", "Amount"},
{"Fruit", "Apple", 100},
{"Fruit", "Orange", 200}
};
worksheet.getRange("A1:C3").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C3"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
IPivotField pivotField = pivotTable.getPivotFields().get("Category");
pivotField.setOrientation(PivotFieldOrientation.RowField);
value - 指定字段在数据透视表报告中放置位置的 PivotFieldOrientation。该位置确定字段在其方向区域中的顺序,例如行字段、列字段、页字段或数据字段。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Category", "Product", "Amount"},
{"Fruit", "Apple", 100},
{"Fruit", "Orange", 80},
{"Snacks", "Cookie", 60}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Category");
field.setOrientation(PivotFieldOrientation.RowField);
field.setPosition(0);
int position = field.getPosition();
该位置确定字段在其方向区域中的顺序,例如行字段、列字段、页字段或数据字段。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Category", "Product", "Amount"},
{"Fruit", "Apple", 100},
{"Fruit", "Orange", 80},
{"Snacks", "Cookie", 60}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Category");
field.setOrientation(PivotFieldOrientation.RowField);
field.setPosition(0);
value - 字段在其当前方向中的从零开始的位置。IPivotField 的项目标签是否重复显示。使用此属性可确定在数据透视表布局中是否显示该字段的重复项目标签。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Product"},
{"Fruit", "Apple"},
{"Fruit", "Orange"},
{"Drink", "Tea"}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"));
IPivotField field = pivotTable.getPivotFields().get("Category");
field.setOrientation(PivotFieldOrientation.RowField);
field.setRepeatLabels(true);
boolean repeatLabels = field.getRepeatLabels();
true;否则为 false。IPivotField 重复项标签。使用此属性可设置是否在数据透视表布局中显示该字段的重复项标签。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Product"},
{"Fruit", "Apple"},
{"Fruit", "Orange"},
{"Drink", "Tea"}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"));
IPivotField field = pivotTable.getPivotFields().get("Category");
field.setOrientation(PivotFieldOrientation.RowField);
field.setRepeatLabels(true);
value - 如果重复项标签,则为 true;否则为 false。默认值为 false。当此属性为 true 时,对于 PivotField,仍会显示没有汇总值的项。
worksheet.getRange("A1:B5").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 120},
{"Fruit", 80},
{"Vegetables", 95},
{"Vegetables", 60}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Category");
field.setShowAllItems(true);
boolean showAllItems = field.getShowAllItems();
true;否则为 false。默认值为 false。当此属性为 true 时,对于 PivotField,仍会显示没有汇总值的项。
worksheet.getRange("A1:B5").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 120},
{"Fruit", 80},
{"Vegetables", 95},
{"Vegetables", 60}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField field = pivotTable.getPivotFields().get("Category");
field.setShowAllItems(true);
value - 如果显示 PivotTable 报告中的所有项,即使它们不包含汇总数据,则为 true;否则为 false。此值标识 PivotTable 报告的底层源字段。如果字段已使用 setName(String) 重命名,则它可能不同于 getName() 返回的当前显示名称。
Object[][] sourceData = new Object[][] {
{"Product", "Amount"},
{"Apple", 100},
{"Pear", 200}
};
worksheet.getRange("G1:H3").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("G1:H3"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("A1"), "PivotTable1");
IPivotField field = pivotTable.getPivotFields().get("Product");
field.setName("Item");
String sourceName = field.getSourceName();
此方法仅对非数据字段有效。使用它可检查该字段启用了哪些 SubtotalType 值。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Beverages", 100},
{"Beverages", 200},
{"Snacks", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "pivotTable1");
IPivotField categoryField = pivotTable.getPivotFields().get("Category");
categoryField.setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
categoryField.setSubtotals(EnumSet.of(SubtotalType.Sum, SubtotalType.Count));
EnumSet<SubtotalType> subtotals = categoryField.getSubtotals();
此方法仅对非数据字段有效。使用它可检查该字段已启用哪些 SubtotalType 值。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Beverages", 100},
{"Beverages", 200},
{"Snacks", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "pivotTable1");
IPivotField categoryField = pivotTable.getPivotFields().get("Category");
categoryField.setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
categoryField.setSubtotals(EnumSet.of(SubtotalType.Sum, SubtotalType.Count));
value - 当前为该字段显示的小计类型组合。此方法使用指定的排序顺序和指定的排序键字段对当前 PivotField 中的项排序。Field 参数必须是排序键字段的唯一源名称,如 getSourceName() 返回的名称,而不是显示的字段名称。
示例:
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Product", "Region", "Amount"},
{"Apple", "East", 120},
{"Orange", "West", 80},
{"Apple", "West", 150},
{"Orange", "East", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
IPivotField fieldRegion = pivotTable.getPivotFields().get("Region");
fieldRegion.setOrientation(PivotFieldOrientation.RowField);
pivotTable.addDataField(pivotTable.getPivotFields().get("Amount"), "Sum of Amount", ConsolidationFunction.Sum);
fieldRegion.autoSort(SortOrder.Descending, pivotTable.getPivotFields().get("Amount").getSourceName());
Order - 要应用的排序顺序。Field - 排序键字段的唯一源名称,如 getSourceName() 返回的名称;不要使用显示的字段名称。使用此方法可对 PivotTable 报表中的当前字段应用自动升序或降序排序。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Product", "Region", "Amount"},
{"Apple", "East", 120},
{"Orange", "West", 80},
{"Apple", "West", 150},
{"Orange", "East", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
IPivotField fieldRegion = pivotTable.getPivotFields().get("Region");
fieldRegion.setOrientation(PivotFieldOrientation.RowField);
fieldRegion.autoSort(SortOrder.Descending);
Order - 要应用的排序顺序。不得为 null。此方法会从 getPivotFilters() 集合中删除所有筛选器,并清除应用于该字段的任何手动筛选。如果该字段位于报告筛选区域,则所选项会重置为默认项。
Object[][] sourceData = new Object[][] {
{"Product", "Amount"},
{"Example Speaker", 4270},
{"Example TV", 2626},
{"Example Mobile", 9062}
};
worksheet.getRange("G1:H4").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("G1:H4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("A1"), "pivottable1");
IPivotField field = pivotTable.getPivotFields().get("Product");
field.setOrientation(PivotFieldOrientation.RowField);
field.getPivotFilters().add(PivotFilterType.CaptionContains, "Example");
field.clearAllFilters();
使用 PivotFieldDateGroupOptions 可以指定分组范围以及用于创建分组 PivotField 的日期或时间单位。该字段必须是未计算的源字段,且其源项必须为日期值。
Workbook workbook = new Workbook();
workbook.getOptions().getData().setAutomaticGroupDateTimeInPivotTable(false);
IWorksheet worksheet = workbook.getWorksheets().get(0);
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Order Date", "Region", "Amount"},
{java.time.LocalDateTime.of(2024, 1, 5, 0, 0), "East", 10},
{java.time.LocalDateTime.of(2024, 1, 18, 0, 0), "West", 20},
{java.time.LocalDateTime.of(2024, 2, 2, 0, 0), "East", 30},
{java.time.LocalDateTime.of(2024, 2, 25, 0, 0), "West", 40}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
IPivotField orderDateField = pivotTable.getPivotFields().get("Order Date");
orderDateField.setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
PivotFieldDateGroupOptions options = new PivotFieldDateGroupOptions();
options.setGroupBy(Collections.singletonList(PivotFieldDateGroupBy.Months));
options.setAutoStart(true);
options.setAutoEnd(true);
orderDateField.group(options);
String groupedFieldName = pivotTable.getRowFields().get(0).getName();
options - 日期分组设置。不得为 null。IllegalArgumentException - 如果 options 为 null。UnsupportedOperationException - 如果此字段无法按日期分组。使用 PivotFieldNumberGroupOptions 可以指定分组范围以及用于创建每个生成分组的数值间隔。该字段必须是未计算的源字段,且其源项必须为数值。
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.getWorksheets().get(0);
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Order ID", "Region", "Amount"},
{10348, "East", 10},
{10362, "East", 20},
{10375, "West", 30},
{10391, "West", 40}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
IPivotField orderIdField = pivotTable.getPivotFields().get("Order ID");
orderIdField.setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
PivotFieldNumberGroupOptions options = new PivotFieldNumberGroupOptions();
options.setStart(10348d);
options.setEnd(10407d);
options.setAutoStart(false);
options.setAutoEnd(false);
options.setInterval(20);
orderIdField.group(options);
options - 数值分组设置。不得为 null。IllegalArgumentException - 如果 options 为 null。UnsupportedOperationException - 如果此字段无法分组为数值范围。使用 PivotFieldCustomGroupOptions 可以指定源 PivotItem 名称、可选显示名称,以及是否将生成的分组字段自动添加到数据透视表布局。源名称、项名称和标题匹配时不区分大小写。
Workbook workbook = new Workbook();
IWorksheet worksheet = workbook.getWorksheets().get(0);
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Product", "Region", "Amount"},
{"Apple", "East", 10},
{"Banana", "West", 20},
{"Carrot", "East", 30},
{"Date", "West", 40}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
IPivotField productField = pivotTable.getPivotFields().get("Product");
productField.setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
PivotFieldCustomGroupOptions options = new PivotFieldCustomGroupOptions();
options.setName("Fruit Group");
options.setItems(java.util.Arrays.asList("Apple", "Banana"));
productField.group(options);
options - 自定义分组设置。不得为 null。IllegalArgumentException - 如果 options 为 null。UnsupportedOperationException - 如果此字段无法分组。移除分组后,数据透视表字段将使用 PivotCache 中的原始源项。
Workbook workbook = new Workbook();
workbook.getOptions().getData().setAutomaticGroupDateTimeInPivotTable(false);
IWorksheet worksheet = workbook.getWorksheets().get(0);
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Order Date", "Region", "Amount"},
{java.time.LocalDateTime.of(2024, 1, 5, 0, 0), "East", 10},
{java.time.LocalDateTime.of(2024, 1, 18, 0, 0), "West", 20},
{java.time.LocalDateTime.of(2024, 2, 2, 0, 0), "East", 30},
{java.time.LocalDateTime.of(2024, 2, 25, 0, 0), "West", 40}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
IPivotField orderDateField = pivotTable.getPivotFields().get("Order Date");
orderDateField.setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
PivotFieldDateGroupOptions options = new PivotFieldDateGroupOptions();
options.setGroupBy(Collections.singletonList(PivotFieldDateGroupBy.Months));
orderDateField.group(options);
orderDateField.ungroup();
IllegalStateException - 如果此数据透视表字段未分组。此方法会将数据透视表中该字段的所有项目的可见状态设置为可见。对于 OLAP 数据透视表,它还会清空手动筛选使用的隐藏项目列表和可见项目列表。
worksheet.getRange("A1:B5").setValue(new Object[][] {
{"Region", "Amount"},
{"East", 120},
{"West", 80},
{"East", 150},
{"South", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
IPivotField fieldRegion = pivotTable.getPivotFields().get("Region");
fieldRegion.setOrientation(PivotFieldOrientation.RowField);
fieldRegion.getPivotItems().get("West").setVisible(false);
fieldRegion.clearManualFilter();
使用此集合可访问各个字段项,包括当前因手动筛选而隐藏的项目。
Object[][] sourceData = {
{"Region", "Amount"},
{"North", 100},
{"South", 200},
{"East", 150}
};
worksheet.getRange("A1:B4").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "salesPivot");
IPivotField field = pivotTable.getPivotFields().get("Region");
field.setOrientation(PivotFieldOrientation.RowField);
IPivotItems items = field.getPivotItems();
items.get("North").setVisible(false);
IPivotField 的计算项集合。返回的 ICalculatedItems 集合表示为当前 PivotTable 字段定义的所有计算项,可用于访问或添加计算项。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 120},
{"East", "Banana", 80},
{"West", "Apple", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.ColumnField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
ICalculatedItems calculatedItems = pivotTable.getPivotFields().get("Product").getCalculatedItems();
ICalculatedItems 集合。IPivotField 的数据透视筛选器集合。返回的 IPivotFilters 集合包含为当前数据透视字段定义的标签、日期和值筛选器,可用于添加或访问筛选器。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Product", "Amount"},
{"Mi Phone", 100},
{"Camera", 200},
{"Mi Band", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField productField = pivotTable.getPivotFields().get("Product");
productField.setOrientation(PivotFieldOrientation.RowField);
IPivotFilters filters = productField.getPivotFilters();
filters.add(PivotFilterType.CaptionContains, "Mi");
IPivotFilters 集合。此方法会移除存储在数据透视字段的 getPivotFilters() 集合中的基于标签的筛选条件。使用此方法可保持值筛选器等其他筛选类型不变。
Object[][] sourceData = new Object[][] {
{"Product", "Amount"},
{"Mi Phone", 100},
{"Camera", 200},
{"Mi Band", 150}
};
worksheet.getRange("A1:B4").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField productField = pivotTable.getPivotFields().get("Product");
productField.setOrientation(PivotFieldOrientation.RowField);
productField.getPivotFilters().add(PivotFilterType.CaptionContains, "Mi");
productField.clearLabelFilter();
此方法会从数据透视字段的 getPivotFilters() 集合中移除基于值的筛选条件。使用此方法可清除值筛选器,而不移除标签筛选器或手动筛选。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Product", "Amount"},
{"Apple", 100},
{"Banana", 200},
{"Apple", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotField productField = pivotTable.getPivotFields().get("Product");
productField.setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
PivotFilterOptions options = new PivotFilterOptions();
options.setAppliedDataField(0);
productField.getPivotFilters().add(PivotFilterType.ValueGreaterThan, 120, null, options);
productField.clearValueFilter();