PivotTableCollection

PivotTableCollection class

表示指定工作表上所有数据透视表对象的集合。

public class PivotTableCollection : CollectionBase<PivotTable>, IDisposable

特性

姓名描述
Capacity { get; set; }
Count { get; }
Item { get; }按索引获取数据透视表。 (3 indexers)
Item { get; set; }

方法

姓名描述
Add(PivotTable, string, string)将新的数据透视表对象从另一个数据透视表添加到集合中。
Add(string, string, string)将新的数据透视表缓存添加到 PivotCaches 集合中。
Add(PivotTable, int, int, string)将新的数据透视表对象从另一个数据透视表添加到集合中。
Add(string, int, int, string)将新的数据透视表缓存添加到 PivotCaches 集合中。
Add(string, string, string, bool)将新的数据透视表缓存添加到 PivotCaches 集合中。
Add(string, int, int, string, bool)将新的数据透视表缓存添加到 PivotCaches 集合中。
Add(string[], bool, PivotPageFields, string, string)将一个新的数据透视表对象添加到具有多个合并范围作为数据源的集合中。
Add(string[], bool, PivotPageFields, int, int, string)将一个新的数据透视表对象添加到具有多个合并范围作为数据源的集合中。
BinarySearch(PivotTable)
BinarySearch(PivotTable, IComparer<PivotTable>)
BinarySearch(int, int, PivotTable, IComparer<PivotTable>)
Clear()清除所有数据透视表。 (2 methods)
Contains(PivotTable)
CopyTo(PivotTable[])
CopyTo(PivotTable[], int)
CopyTo(int, PivotTable[], int, int)
Dispose()执行与释放、释放或 重置非托管资源相关的应用程序定义任务。
Exists(Predicate<PivotTable>)
Find(Predicate<PivotTable>)
FindAll(Predicate<PivotTable>)
FindIndex(Predicate<PivotTable>)
FindIndex(int, Predicate<PivotTable>)
FindIndex(int, int, Predicate<PivotTable>)
FindLast(Predicate<PivotTable>)
FindLastIndex(Predicate<PivotTable>)
FindLastIndex(int, Predicate<PivotTable>)
FindLastIndex(int, int, Predicate<PivotTable>)
GetEnumerator()
IndexOf(PivotTable)
IndexOf(PivotTable, int)
IndexOf(PivotTable, int, int)
LastIndexOf(PivotTable)
LastIndexOf(PivotTable, int)
LastIndexOf(PivotTable, int, int)
Remove(PivotTable)删除指定数据透视表并删除数据透视表数据
Remove(PivotTable, bool)删除指定的数据透视表
RemoveAt(int)删除指定索引处的数据透视表并删除数据透视表数据 (2 methods)
RemoveAt(int, bool)删除指定索引处的数据透视表

例子


[C#]

Workbook book = new Workbook();
Worksheet sheet = book.Worksheets[0];
Cells cells = sheet.Cells;
cells[0, 0].Value = "fruit";
cells[1, 0].Value = "grape";
cells[2, 0].Value = "blueberry";
cells[3, 0].Value = "kiwi";
cells[4, 0].Value = "cherry";
cells[5, 0].Value = "grape";
cells[6, 0].Value = "blueberry";
cells[7, 0].Value = "kiwi";
cells[8, 0].Value = "cherry";

cells[0, 1].Value = "year";
cells[1, 1].Value = 2020;
cells[2, 1].Value = 2020;
cells[3, 1].Value = 2020;
cells[4, 1].Value = 2020;
cells[5, 1].Value = 2021;
cells[6, 1].Value = 2021;
cells[7, 1].Value = 2021;
cells[8, 1].Value = 2021;

cells[0, 2].Value = "amount";
cells[1, 2].Value = 50;
cells[2, 2].Value = 60;
cells[3, 2].Value = 70;
cells[4, 2].Value = 80;
cells[5, 2].Value = 90;
cells[6, 2].Value = 100;
cells[7, 2].Value = 110;
cells[8, 2].Value = 120;

PivotTableCollection pivots = sheet.PivotTables;

int pivotIndex = pivots.Add("=Sheet1!A1:C9", "A12", "TestPivotTable");
PivotTable pivot = pivots[pivotIndex];
pivot.AddFieldToArea(PivotFieldType.Row, "fruit");
pivot.AddFieldToArea(PivotFieldType.Column, "year");
pivot.AddFieldToArea(PivotFieldType.Data, "amount");

pivot.PivotTableStyleType = PivotTableStyleType.PivotTableStyleMedium10;

//改变PivotField的属性
PivotField rowField = pivot.RowFields[0];
rowField.DisplayName = "custom display name";

//添加透视过滤器
int index = pivot.PivotFilters.Add(0, PivotFilterType.Count);
PivotFilter filter = pivot.PivotFilters[index];
filter.AutoFilter.FilterTop10(0, false, false, 2);

//添加PivotFormatCondition
int formatIndex = pivot.PivotFormatConditions.Add();
PivotFormatCondition pfc = pivot.PivotFormatConditions[formatIndex];
FormatConditionCollection fcc = pfc.FormatConditions;
fcc.AddArea(pivot.DataBodyRange);
int idx = fcc.AddCondition(FormatConditionType.CellValue);
FormatCondition fc = fcc[idx];
fc.Formula1 = "100";
fc.Operator = OperatorType.GreaterOrEqual;
fc.Style.BackgroundColor = Color.Red;

pivot.RefreshData();
pivot.CalculateData();

//做你的事

book.Save("out.xlsx");

[Visual Basic]

Dim book As Workbook = New Workbook()
Dim sheet As Worksheet = book.Worksheets(0)
Dim cells As Cells = sheet.Cells

cells(0, 0).Value = "fruit"
cells(1, 0).Value = "grape"
cells(2, 0).Value = "blueberry"
cells(3, 0).Value = "kiwi"
cells(4, 0).Value = "cherry"
cells(5, 0).Value = "grape"
cells(6, 0).Value = "blueberry"
cells(7, 0).Value = "kiwi"
cells(8, 0).Value = "cherry"

cells(0, 1).Value = "year"
cells(1, 1).Value = 2020
cells(2, 1).Value = 2020
cells(3, 1).Value = 2020
cells(4, 1).Value = 2020
cells(5, 1).Value = 2021
cells(6, 1).Value = 2021
cells(7, 1).Value = 2021
cells(8, 1).Value = 2021

cells(0, 2).Value = "amount"
cells(1, 2).Value = 50
cells(2, 2).Value = 60
cells(3, 2).Value = 70
cells(4, 2).Value = 80
cells(5, 2).Value = 90
cells(6, 2).Value = 100
cells(7, 2).Value = 110
cells(8, 2).Value = 120

Dim pivots As PivotTableCollection = sheet.PivotTables
Dim pivotIndex As Int32 = pivots.Add("=Sheet1!A1:C9", "A12", "TestPivotTable")
Dim pivot As PivotTable = pivots(pivotIndex)
pivot.AddFieldToArea(PivotFieldType.Row, "fruit")
Pivot.AddFieldToArea(PivotFieldType.Column, "year")
Pivot.AddFieldToArea(PivotFieldType.Data, "amount")

pivot.PivotTableStyleType = PivotTableStyleType.PivotTableStyleMedium10

'Change PivotField's attributes
Dim rowField As PivotField = pivot.RowFields(0)
rowField.DisplayName = "custom display name"

'添加透视过滤器
Dim filterIndex As Int32 = pivot.PivotFilters.Add(0, PivotFilterType.Count)
Dim filter As PivotFilter = pivot.PivotFilters(filterIndex)
filter.AutoFilter.FilterTop10(0, False, False, 2)

'添加 PivotFormatCondition
Dim formatIndex As Int32 = pivot.PivotFormatConditions.Add()
Dim pfc As PivotFormatCondition = pivot.PivotFormatConditions(formatIndex)
Dim fcc As FormatConditionCollection = pfc.FormatConditions
fcc.AddArea(pivot.DataBodyRange)
Dim idx As Int32 = fcc.AddCondition(FormatConditionType.CellValue)
Dim fc As FormatCondition = fcc(idx)
fc.Formula1 = "100"
fc.Operator = OperatorType.GreaterOrEqual
fc.Style.BackgroundColor = Color.Red

pivot.RefreshData()
pivot.CalculateData()

book.Save("out_vb.xlsx")

也可以看看