[]
IPivotTable 是 IPivotTables 集合的成员,用于汇总和分析工作表上来自 IPivotCache 的数据。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Name", "Value"},
{"A", 100},
{"B", 200},
{"A", 100}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "PivotTable1");
pivotTable.setName("SalesPivot");
addDataField(IPivotField field,
String caption,
ConsolidationFunction function) voidvoidbooleanbooleanintbooleanbooleanbooleanbooleanbooleanbooleanbooleanbooleangetName()intIPivotCache。IPivotAxis 对象。IPivotAxis 对象。booleanbooleanbooleanbooleanbooleanbooleangetStyle()getTag()pivotValueCell(int rowline,
int columnline) IPivotValueCell。booleanrefresh()voidvoidsetAllowMultipleFilters(boolean allowMultipleFilters) voidsetColumnGrand(boolean value) voidsetCompactRowIndent(int value) voidsetDeferLayoutUpdate(boolean value) voidsetDisplayContextTooltips(boolean value) voidsetDisplayErrorString(boolean value) voidsetDisplayFieldCaptions(boolean displayFieldCaptions) voidsetDisplayMemberPropertyTooltips(boolean value) voidsetDisplayNullString(boolean value) voidsetErrorString(String value) voidsetGrandTotalName(String value) voidsetHasAutoFormat(boolean value) voidsetLayoutRowDefault(LayoutRowType value) IPivotField 对象首次添加到 IPivotTable 时的默认布局行类型。voidsetMergeLabels(boolean value) voidvoidsetNullString(String value) voidsetPageFieldOrder(Order value) voidsetPageFieldWrapCount(int value) voidsetRowAxisLayout(LayoutRowType rowLayout) voidsetRowGrand(boolean value) voidsetShowTableStyleColumnHeaders(boolean value) voidsetShowTableStyleColumnStripes(boolean value) voidsetShowTableStyleLastColumn(boolean value) voidsetShowTableStyleRowHeaders(boolean value) voidsetShowTableStyleRowStripes(boolean value) voidsetStyle(ITableStyle style) voidsetTableStyle(String value) voidvoidsubtotalLocation(SubtotalLocationType location) voidupdate()此属性指示是否为数据透视表报表的列区域显示总计。默认值为 true。
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");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.ColumnField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
boolean showColumnGrand = pivotTable.getColumnGrand();
true;否则为 false。此属性指示是否在数据透视表报告的列区域显示总计。默认值为 true。
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");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.ColumnField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
pivotTable.setColumnGrand(true);
value - 数据透视表报告是否显示列的总计。返回的集合为只读,并包含方向设置为列字段的字段。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Category", "Region", "Amount"},
{"Beverages", "East", 120},
{"Beverages", "West", 80},
{"Snacks", "East", 95},
{"Snacks", "West", 110}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.ColumnField);
IPivotFields columnFields = pivotTable.getColumnFields();
返回的区域涵盖当前数据透视表布局中用于列字段的单元格。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Category", "Region", "Amount"},
{"Beverages", "East", 120},
{"Beverages", "West", 80},
{"Snacks", "East", 95},
{"Snacks", "West", 110}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.ColumnField);
pivotTable.addDataField(pivotTable.getPivotFields().get("Amount"), "Sum of Amount", ConsolidationFunction.Sum);
IRange range = pivotTable.getColumnRange();
String address = range.getAddress();
当数据透视表报告使用紧凑行布局时,此值控制每个嵌套数据透视项的缩进量。
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");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
pivotTable.setCompactRowIndent(2);
int indent = pivotTable.getCompactRowIndent();
当数据透视表报告使用紧凑行布局时,此值控制每个嵌套数据透视项的缩进量。
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");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
pivotTable.setCompactRowIndent(2);
value - 启用紧凑行布局时数据透视项的缩进增量。返回的区域表示标题区域和插入行之间的单元格。
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");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
pivotTable.addDataField(pivotTable.getPivotFields().get("Amount"), "Sum of Amount", ConsolidationFunction.Sum);
IRange range = pivotTable.getDataBodyRange();
返回的集合为只读,包含放置在 PivotTable“值”区域中的字段。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 100},
{"Fruit", 80},
{"Vegetables", 60}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.addDataField(pivotTable.getPivotFields().get("Amount"), "Sum of Amount", ConsolidationFunction.Sum);
IPivotFields dataFields = pivotTable.getDataFields();
String caption = dataFields.get(0).getName();
当此属性为 true 时,可以累积布局更改,然后通过调用 update() 应用这些更改。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Category", "Product", "Amount"},
{"Beverages", "Tea", 100},
{"Beverages", "Coffee", 200},
{"Snacks", "Biscuits", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
pivotTable.setDeferLayoutUpdate(true);
boolean deferred = pivotTable.getDeferLayoutUpdate();
true;否则为 false。当此属性为 true 时,可以累积布局更改,然后通过调用 update() 应用这些更改。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Category", "Product", "Amount"},
{"Beverages", "Tea", 100},
{"Beverages", "Coffee", 200},
{"Snacks", "Biscuits", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
pivotTable.setDeferLayoutUpdate(true);
value - 是否推迟 PivotTable 的布局更新。此属性指示是否为数据透视表报表中的单元格显示上下文工具提示。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Name", "Amount"},
{"A", 100},
{"B", 200},
{"A", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
boolean displayContextTooltips = pivotTable.getDisplayContextTooltips();
true;否则为 false。此属性指示是否为 PivotTable 报表中的单元格显示上下文工具提示。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Name", "Amount"},
{"A", 100},
{"B", 200},
{"A", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setDisplayContextTooltips(true);
value - 是否为 PivotTable 单元格显示工具提示。当此属性为 true 时,数据透视表会对包含错误的单元格使用 getErrorString() 指定的字符串。默认值为 false。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"A", 10},
{"B", 20},
{"A", 15}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "PivotTable1");
pivotTable.setDisplayErrorString(true);
boolean displayErrorString = pivotTable.getDisplayErrorString();
true;否则为 false。当此属性为 true 时,数据透视表会对包含错误的单元格使用 getErrorString() 指定的字符串。默认值为 false。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"A", 10},
{"B", 20},
{"A", 15}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "PivotTable1");
pivotTable.setDisplayErrorString(true);
value - 数据透视表报告是否在包含错误的单元格中显示自定义错误字符串。当此属性为 true 时,工具提示可包含数据透视表项的成员属性信息。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"A", 100},
{"B", 200},
{"A", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setDisplayMemberPropertyTooltips(true);
boolean displayMemberPropertyTooltips = pivotTable.getDisplayMemberPropertyTooltips();
true;否则为 false。当此属性为 true 时,工具提示可包含数据透视表项的成员属性信息。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"A", 100},
{"B", 200},
{"A", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setDisplayMemberPropertyTooltips(true);
value - 是否在数据透视表工具提示中显示成员属性。当此属性为 true 时,数据透视表对包含 null 值的单元格使用由 getNullString() 指定的字符串。默认值为 true。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Name", "Value"},
{"A", 100},
{"B", null},
{"A", 200}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "PivotTable1");
boolean displayNullString = pivotTable.getDisplayNullString();
true;否则为 false。当此属性为 true 时,数据透视表会对包含空值的单元格使用由 getNullString() 指定的字符串。默认值为 true。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Name", "Value"},
{"A", 100},
{"B", null},
{"A", 200}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "PivotTable1");
pivotTable.setDisplayNullString(true);
value - 数据透视表报表是否在包含空值的单元格中显示自定义字符串。默认值为空字符串("")。使用 setErrorString(String) 可以指定数据透视表为错误单元格显示的自定义错误文本。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"A", 10},
{"B", 20},
{"A", 15}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "PivotTable1");
pivotTable.setDisplayErrorString(true);
pivotTable.setErrorString("Invalid");
String errorString = pivotTable.getErrorString();
默认值为空字符串("")。使用 setErrorString(String) 指定数据透视表为错误单元格显示的自定义错误文本。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"A", 10},
{"B", 20},
{"A", 15}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "PivotTable1");
pivotTable.setDisplayErrorString(true);
pivotTable.setErrorString("Invalid");
value - 启用自定义错误字符串显示时,在包含错误的单元格中显示的字符串。默认标签为 "Grand Total"。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Region", "Sales"},
{"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");
String grandTotalName = pivotTable.getGrandTotalName();
默认标签为 "Grand Total"。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Region", "Sales"},
{"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");
pivotTable.setGrandTotalName("Sample");
value - 数据透视表报告的总计行标题或总计列标题中显示的文本标签。此设置确定新添加 PivotFields 使用的初始行布局,例如紧凑、大纲或表格布局。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 120},
{"West", "Apple", 80},
{"East", "Orange", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.setLayoutRowDefault(LayoutRowType.TabularRow);
LayoutRowType layoutType = pivotTable.getLayoutRowDefault();
IPivotField 对象首次添加到 IPivotTable 时的默认布局行类型。此设置适用于新添加的 PivotFields。若要同时更改现有 PivotFields 的布局,请使用 setRowAxisLayout(LayoutRowType)。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"East", "Pear", 80},
{"West", "Apple", 120},
{"West", "Pear", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.setLayoutRowDefault(LayoutRowType.TabularRow);
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.RowField);
value - PivotField 首次添加到 PivotTable 时要应用的默认布局行类型。使用此属性可以确定 数据透视表布局中的重复标签是否通过合并相邻单元格显示。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"East", "Pear", 200},
{"West", "Apple", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
pivotTable.setMergeLabels(true);
boolean mergeLabels = pivotTable.getMergeLabels();
true;否则为 false。使用此属性可设置数据透视表布局中的重复标签是否通过合并相邻单元格来显示。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"East", "Pear", 200},
{"West", "Apple", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
pivotTable.setMergeLabels(true);
value - 指定的数据透视表报表是否对其外部行项目、列项目、小计和总计标签使用合并单元格。该名称用于在工作表中标识数据透视表,并可用于以编程方式引用数据透视表。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 120},
{"West", "Apple", 80},
{"East", "Orange", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
String name = pivotTable.getName();
该名称用于标识工作表上的数据透视表,并可用于以编程方式引用该数据透视表。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 120},
{"West", "Apple", 80},
{"East", "Orange", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.setName("Sample");
value - 数据透视表名称。默认值为空字符串("")。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Name", "Value"},
{"A", 100},
{"B", null},
{"A", 200}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setDisplayNullString(true);
pivotTable.setNullString("(blank)");
String nullString = pivotTable.getNullString();
默认值为空字符串("")。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Name", "Value"},
{"A", 100},
{"B", null},
{"A", 200}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setDisplayNullString(true);
pivotTable.setNullString("(blank)");
value - 启用自定义空值字符串显示时,在包含 null 值的数据透视表单元格中显示的字符串。返回值是 Order 常量之一:Order.DownThenOver 或 Order.OverThenDown。默认值为 Order.DownThenOver。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"East", "Pear", 80},
{"West", "Apple", 90},
{"West", "Pear", 70}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.PageField);
pivotTable.setPageFieldOrder(Order.OverThenDown);
Order order = pivotTable.getPageFieldOrder();
使用 Order.DownThenOver 可以在移至下一列之前沿每列向下放置页字段,或使用 Order.OverThenDown 可以在移至下一行之前沿每行横向放置页字段。默认值为 Order.DownThenOver。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"East", "Pear", 80},
{"West", "Apple", 90},
{"West", "Pear", 70}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.PageField);
pivotTable.setPageFieldOrder(Order.OverThenDown);
value - 页字段布局顺序。使用 Order.DownThenOver 或 Order.OverThenDown;如果指定 null,则将其视为 Order.DownThenOver。返回的集合为只读,包含方向设置为 PivotTable 报告页面区域的字段。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"East", "Pear", 80},
{"West", "Apple", 90},
{"West", "Pear", 70}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.PageField);
IPivotFields pageFields = pivotTable.getPageFields();
当数据透视表包含多个页字段时,此值确定页字段在报告筛选区域中的换行方式。使用 setPageFieldWrapCount(int) 可以更改布局。
worksheet.getRange("A1:D5").setValue(new Object[][] {
{"Region", "Product", "Quarter", "Amount"},
{"East", "Apple", "Q1", 120},
{"East", "Pear", "Q2", 95},
{"West", "Apple", "Q1", 80},
{"West", "Pear", "Q2", 110}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:D5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("F1"), "SalesPivot");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.PageField);
pivotTable.getPivotFields().get("Quarter").setOrientation(PivotFieldOrientation.PageField);
pivotTable.setPageFieldWrapCount(1);
int wrapCount = pivotTable.getPageFieldWrapCount();
当数据透视表包含多个页字段时,此值决定这些页字段在报表筛选区域中的换行方式。使用 setPageFieldWrapCount(int) 更改布局。
worksheet.getRange("A1:D5").setValue(new Object[][] {
{"Region", "Product", "Quarter", "Amount"},
{"East", "Apple", "Q1", 120},
{"East", "Pear", "Q2", 95},
{"West", "Apple", "Q1", 80},
{"West", "Pear", "Q2", 110}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:D5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("F1"), "SalesPivot");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.PageField);
pivotTable.getPivotFields().get("Quarter").setOrientation(PivotFieldOrientation.PageField);
pivotTable.setPageFieldWrapCount(1);
value - 数据透视表报告中每列或每行显示的页字段数量。页面区域是报表筛选区域,用于显示方向设置为页字段的字段。如果 PivotTable 当前没有页面区域,此方法返回 null。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 100},
{"East", "Pear", 80},
{"West", "Apple", 90},
{"West", "Pear", 70}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.PageField);
IRange pageRange = pivotTable.getPageRange();
IPivotAxis 对象。使用此轴可以处理数据透视表的列侧结构,例如报告顶部显示的数据透视线。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Category", "Region", "Amount"},
{"Beverages", "East", 120},
{"Beverages", "West", 80},
{"Snacks", "East", 95},
{"Snacks", "West", 110}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.ColumnField);
IPivotAxis columnAxis = pivotTable.getPivotColumnAxis();
IPivotLines lines = columnAxis.getPivotLines();
IPivotAxis 对象。IPivotAxis 对象。使用此轴可以处理数据透视表的行侧结构,例如报告左侧向下显示的数据透视线。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Category", "Region", "Amount"},
{"Beverages", "East", 120},
{"Beverages", "West", 80},
{"Snacks", "East", 95},
{"Snacks", "West", 110}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
IPivotAxis rowAxis = pivotTable.getPivotRowAxis();
IPivotLines lines = rowAxis.getPivotLines();
IPivotAxis 对象。返回的 IPivotFields 集合为只读,包含方向设置为行字段的字段。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 120},
{"East", "Pear", 80},
{"West", "Apple", 95},
{"West", "Pear", 110}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
IPivotFields rowFields = pivotTable.getRowFields();
String firstFieldName = rowFields.get(0).getName();
IPivotFields。返回的 IPivotFields 集合为只读,包含方向设置为行字段的字段。使用此方法可以确定数据透视表是否为行字段显示总计行。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Region", "Amount"},
{"East", 120},
{"West", 150},
{"East", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
boolean showRowGrand = pivotTable.getRowGrand();
true;否则为 false。使用此方法可设置 PivotTable 是否为行字段显示总计行。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Region", "Amount"},
{"East", 120},
{"West", 150},
{"East", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
pivotTable.setRowGrand(value);
value - PivotTable 报表是否显示行总计。返回的区域表示当前数据透视表布局中行字段和行项使用的单元格。
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");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
IRange rowRange = pivotTable.getRowRange();
此属性指示数据透视表是否使用当前表样式设置显示其列标题区域。
worksheet.getRange("A1:D5").setValue(new Object[][] {
{"Category", "Product", "Amount", "Region"},
{"Beverages", "Tea", 100, "East"},
{"Beverages", "Coffee", 200, "West"},
{"Snacks", "Chips", 150, "East"},
{"Snacks", "Nuts", 120, "West"}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:D5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("F1"), "SalesPivot");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.ColumnField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
boolean showColumnHeaders = pivotTable.getShowTableStyleColumnHeaders();
true;否则为 false。此属性表示 PivotTable 是否使用当前表样式设置显示其列标题区域。
worksheet.getRange("A1:D5").setValue(new Object[][] {
{"Category", "Product", "Amount", "Region"},
{"Beverages", "Tea", 100, "East"},
{"Beverages", "Coffee", 200, "West"},
{"Snacks", "Chips", 150, "East"},
{"Snacks", "Nuts", 120, "West"}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:D5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("F1"), "SalesPivot");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.ColumnField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
pivotTable.setShowTableStyleColumnHeaders(value);
value - 是否在 PivotTable 中显示列标题。当此属性为 true 时,偶数列与奇数列使用不同格式,使 PivotTable 更易于阅读。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 100},
{"Drink", 200},
{"Fruit", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setShowTableStyleColumnStripes(true);
boolean showColumnStripes = pivotTable.getShowTableStyleColumnStripes();
true;否则为 false。当此属性为 true 时,偶数列的格式与奇数列不同,以使 PivotTable 更易于阅读。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 100},
{"Drink", 200},
{"Fruit", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setShowTableStyleColumnStripes(true);
value - 是否在 PivotTable 样式中显示带状列。此属性指示 PivotTable 样式是否应用于 PivotTable 报告的最后一列。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 100},
{"Drink", 200},
{"Fruit", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
pivotTable.setShowTableStyleLastColumn(true);
boolean showLastColumnStyle = pivotTable.getShowTableStyleLastColumn();
true;否则为 false。此属性指示是否将 PivotTable 样式应用于 PivotTable 报表的最后一列。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 100},
{"Drink", 200},
{"Fruit", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
pivotTable.setShowTableStyleLastColumn(true);
value - 是否在 PivotTable 中显示最后一列样式。使用此方法可以确定当前 PivotTable 样式是否显示行标题格式。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 100},
{"Drink", 200},
{"Fruit", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setShowTableStyleRowHeaders(true);
boolean showRowHeaders = pivotTable.getShowTableStyleRowHeaders();
true;否则为 false。使用此方法可设置 PivotTable 样式是否显示行标题格式。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 100},
{"Drink", 200},
{"Fruit", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setShowTableStyleRowHeaders(true);
value - 是否在 PivotTable 样式中显示行标题。当此属性为 true 时,数据透视表样式会应用交替行格式以提高可读性。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Beverages", 120},
{"Snacks", 95},
{"Beverages", 80}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
boolean showRowStripes = pivotTable.getShowTableStyleRowStripes();
true;否则为 false。当此属性为 true 时,PivotTable 样式会应用交替的行格式以提高可读性。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Beverages", 120},
{"Snacks", 95},
{"Beverages", 80}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
pivotTable.setShowTableStyleRowStripes(value);
value - 是否在 PivotTable 中显示带状行,以便偶数行与奇数行采用不同的格式。使用此方法可以访问呈现的 PivotTable 区域,而不包括与主报告分开显示的任何页字段区域。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Fruit", 100},
{"Fruit", 200},
{"Vegetable", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
IRange range = pivotTable.getTableRange1();
String address = range.toString();
使用此方法可以检索完整显示的数据透视表区域(包括筛选区域),而不只是主体报告区域。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Beverages", 120},
{"Beverages", 80},
{"Snacks", 50}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D2"), "SalesPivot");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
IRange range = pivotTable.getTableRange2();
默认值为空字符串,表示未应用任何样式。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Beverages", 120},
{"Beverages", 80},
{"Snacks", 95}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setTableStyle("PivotStyleMedium9");
String styleName = pivotTable.getTableStyle();
默认值为空字符串,表示不应用任何样式。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Beverages", 120},
{"Beverages", 80},
{"Snacks", 95}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setTableStyle("PivotStyleMedium9");
value - 在 PivotTable 报告正文中使用的样式。使用此属性可以检索与数据透视表报告关联的自定义文本元数据。要存储值,请使用 setTag(String)。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Region", "Amount"},
{"East", 120},
{"West", 80},
{"East", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setTag("Monthly sales summary");
String tag = pivotTable.getTag();
该值随数据透视表报表一起保存。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Region", "Amount"},
{"East", 120},
{"West", 80},
{"East", 90}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setTag("Monthly sales summary");
value - 要与数据透视表报表关联的自定义文本元数据。使用此方法可以将源字段添加到值区域,并指定该数据字段使用的汇总函数。
Object[][] sourceData = {
{"Region", "Product", "Amount"},
{"East", "Apple", 120},
{"West", "Apple", 80},
{"East", "Orange", 90}
};
worksheet.getRange("A1:C4").setValue(sourceData);
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.addDataField(pivotTable.getPivotFields().get("Amount"), "Sum of Amount", ConsolidationFunction.Sum);
field - 服务器上的唯一字段。如果源数据为 OLAP,则此字段是多维数据集字段;否则,它是数据透视表字段。caption - 数据透视表报告中用于标识所添加数据字段的标签。function - 在添加的数据字段中执行的函数。使用返回的 ICalculatedFields 集合可以访问或添加当前 PivotTable 的计算字段。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Product", "Amount"},
{"Apple", 120},
{"Orange", 90},
{"Apple", 80}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
ICalculatedFields calculatedFields = pivotTable.getCalculatedFields();
calculatedFields.add("DoubleAmount", "=Amount*2");
ICalculatedFields。使用返回的 ICalculatedFields 集合可以访问或添加当前 PivotTable 的计算字段。此方法会移除数据透视表筛选器集合中的筛选器,清除手动筛选,并将报告筛选区域中的所有数据透视字段重置为其默认项。
Object[][] sourceData = {
{"Product", "Country", "Amount"},
{"Apple", "Canada", 120},
{"Apple", "France", 150},
{"Banana", "Canada", 90}
};
worksheet.getRange("A1:C4").setValue(sourceData);
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
IPivotField fieldProduct = pivotTable.getPivotFields().get("Product");
fieldProduct.setOrientation(PivotFieldOrientation.RowField);
IPivotField fieldAmount = pivotTable.getPivotFields().get("Amount");
fieldAmount.setOrientation(PivotFieldOrientation.DataField);
fieldProduct.getPivotFilters().add(PivotFilterType.CaptionContains, "pp");
pivotTable.clearAllFilters();
清除 PivotTable 会移除所有字段,并删除应用于报告的所有筛选和排序。此方法会将 PivotTable 重置为刚创建后、添加任何字段之前的状态。
Object[][] sourceData = {
{"Product", "Region", "Amount"},
{"Apple", "East", 120},
{"Pear", "West", 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"), "PivotTable1");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.RowField);
pivotTable.addDataField(pivotTable.getPivotFields().get("Amount"), "Sum of Amount", ConsolidationFunction.Sum);
pivotTable.clearTable();
IPivotCache。返回的缓存表示与数据透视表关联的源数据缓存。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Product", "Sales"},
{"Apple", 100},
{"Orange", 200},
{"Pear", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
IPivotCache cache = pivotTable.getPivotCache();
返回的 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");
IPivotFields pivotFields = pivotTable.getPivotFields();
pivotFields.get("Category").setOrientation(PivotFieldOrientation.RowField);
IPivotFields。返回的 IPivotFields 集合包含数据透视表数据源中的可见字段和隐藏字段。IPivotValueCell。使用此方法可以访问 PivotTable 报告中的计算值单元格,然后检查其值或相关的 IPivotCell 信息。
worksheet.getRange("A1:B3").setValue(new Object[][] {
{"Region", "Amount"},
{"East", 100},
{"West", 200}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B3"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"));
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
IPivotValueCell valueCell = pivotTable.pivotValueCell(0, 0);
Object value = valueCell.getValue();
rowline - 数据区域中的行位置。columnline - 数据区域中的列位置。当源数据已更改并且需要 PivotTable 反映最新数据时,请使用此方法。
此方法执行以下操作:
IPivotCache。IPivotFields 与已刷新的缓存同步。如果仅 PivotTable 布局或显示设置已更改,而源数据未更改,请改用 update()。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Product", "Amount"},
{"Apple", 100},
{"Apple", 200},
{"Orange", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "PivotTable1");
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
worksheet.getRange("B2").setValue(120);
boolean refreshed = pivotTable.refresh();
true。IllegalStateException - 如果找不到 PivotTable 的源工作表。此设置会应用于数据透视表中的所有现有数据透视字段。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Category", "Product", "Amount"},
{"Beverages", "Tea", 100},
{"Beverages", "Coffee", 200},
{"Snacks", "Chips", 150},
{"Snacks", "Nuts", 120}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
pivotTable.repeatAllLabels(PivotFieldRepeatLabels.RepeatLabels);
repeat - 指定是否为数据透视表中的所有数据透视字段重复项目标签。使用此方法可以一次性将单个 LayoutRowType 应用于当前行字段。要为以后添加的 PivotFields 设置默认布局,请使用 setLayoutRowDefault(LayoutRowType)。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Region", "Product", "Amount"},
{"East", "Apple", 120},
{"West", "Apple", 80},
{"East", "Orange", 90},
{"West", "Orange", 110}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Region").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
pivotTable.setRowAxisLayout(LayoutRowType.TabularRow);
rowLayout - 要应用于所有现有 PivotFields 的行轴布局类型。更改小计位置只会立即影响大纲形式中的字段视觉效果,但此设置也会应用于表格形式中的字段。
如果行轴布局为 LayoutRowType.TabularRow,则 location 必须为 SubtotalLocationType.Bottom。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Category", "Amount"},
{"Beverages", 100},
{"Snacks", 200},
{"Beverages", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"));
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
pivotTable.subtotalLocation(SubtotalLocationType.Bottom);
location - 要应用于所有现有数据透视字段的小计位置。IllegalArgumentException - 如果行轴布局为 LayoutRowType.TabularRow,且 location 不是 SubtotalLocationType.Bottom。常见用法是启用 setDeferLayoutUpdate(boolean),进行多个布局更改后,再调用此方法一次性应用所有的更改。
此方法不会从源数据刷新 IPivotCache。如果源数据已更改且必须先刷新缓存,请改为调用 refresh()。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Category", "Product", "Amount"},
{"Beverages", "Tea", 100},
{"Beverages", "Coffee", 200},
{"Snacks", "Biscuits", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "PivotTable1");
pivotTable.setDeferLayoutUpdate(true);
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
pivotTable.update();
此方法返回与数据透视表关联的当前 ITableStyle。如果未应用任何样式,则此方法返回 null。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Name", "Amount"},
{"A", 100},
{"B", 200},
{"A", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setStyle(workbook.getTableStyles().get("PivotStyleMedium3"));
ITableStyle style = pivotTable.getStyle();
String styleName = style.getName();
此方法设置与数据透视表关联的当前 ITableStyle。如果尚未应用任何样式,此方法将设置 null。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Name", "Amount"},
{"A", 100},
{"B", 200},
{"A", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("D1"), "SalesPivot");
pivotTable.setStyle(workbook.getTableStyles().get("PivotStyleMedium3"));
String styleName = style.getName();
style - 应用于数据透视表的样式。使用此方法可以访问 IPivotFormulas 集合,该集合包含为 PivotTable 定义的自定义公式,例如由计算项创建的公式。
worksheet.getRange("A1:C4").setValue(new Object[][] {
{"Product", "Category", "Amount"},
{"Apple", "Fruit", 120},
{"Banana", "Fruit", 80},
{"Bran", "Snack", 60}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.getPivotFields().get("Product").setOrientation(PivotFieldOrientation.RowField);
pivotTable.getPivotFields().get("Amount").setOrientation(PivotFieldOrientation.DataField);
pivotTable.getPivotFields().get("Category").setOrientation(PivotFieldOrientation.ColumnField);
pivotTable.getPivotFields().get("Product").getCalculatedItems().add("TotalFruit", "=Product[Apple] + Product[Banana]");
IPivotFormulas formulas = pivotTable.getPivotFormulas();
IPivotFormula formula = formulas.get(0);
使用此属性可以确定数据透视表是否允许单个数据透视字段同时保留多个筛选条件。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Category", "Product", "Amount"},
{"Fruit", "Apple", 120},
{"Fruit", "Banana", 90},
{"Drink", "Tea", 60},
{"Drink", "Coffee", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.setAllowMultipleFilters(true);
boolean allowMultipleFilters = pivotTable.getAllowMultipleFilters();
true;否则为 false。使用此属性可设置数据透视表是否允许单个数据透视字段同时保留多个筛选条件。
worksheet.getRange("A1:C5").setValue(new Object[][] {
{"Category", "Product", "Amount"},
{"Fruit", "Apple", 120},
{"Fruit", "Banana", 90},
{"Drink", "Tea", 60},
{"Drink", "Coffee", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:C5"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.setAllowMultipleFilters(true);
allowMultipleFilters - 数据透视表中的数据透视字段是否可以同时应用多个筛选器。默认值为 true。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Product", "Amount"},
{"Apple", 100},
{"Orange", 200},
{"Apple", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
boolean hasAutoFormat = pivotTable.getHasAutoFormat();
true;否则为 false。默认值为 true。
worksheet.getRange("A1:B4").setValue(new Object[][] {
{"Product", "Amount"},
{"Apple", 100},
{"Orange", 200},
{"Apple", 150}
});
IPivotCache pivotCache = workbook.getPivotCaches().create(worksheet.getRange("A1:B4"));
IPivotTable pivotTable = worksheet.getPivotTables().add(pivotCache, worksheet.getRange("E1"), "SalesPivot");
pivotTable.setHasAutoFormat(true);
value - PivotTable 报表在刷新时是否自动调整列宽。
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.setDisplayFieldCaptions(false);
boolean displayFieldCaptions = pivotTable.getDisplayFieldCaptions();
true;否则为 false。
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.setDisplayFieldCaptions(false);
displayFieldCaptions - true 表示显示筛选按钮和字段标题;否则为 false。