-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathPivotTableActions.vb
More file actions
147 lines (115 loc) · 6.5 KB
/
PivotTableActions.vb
File metadata and controls
147 lines (115 loc) · 6.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
Imports DevExpress.Spreadsheet
Namespace SpreadsheetWPFPivotTableExamples
Public NotInheritable Class PivotTableActions
Private Sub New()
End Sub
Private Shared Sub CreatePivotTableFromRange(ByVal workbook As IWorkbook)
' #Region "#CreateFromRange"
Dim sourceWorksheet As Worksheet = workbook.Worksheets("Data1")
Dim worksheet As Worksheet = workbook.Worksheets.Add()
workbook.Worksheets.ActiveWorksheet = worksheet
' Create a pivot table using the cell range "A1:D41" as the data source.
Dim pivotTable As PivotTable = worksheet.PivotTables.Add(sourceWorksheet("A1:D41"), worksheet("B2"))
' Add the "Category" field to the row axis area.
pivotTable.RowFields.Add(pivotTable.Fields("Category"))
' Add the "Product" field to the row axis area.
pivotTable.RowFields.Add(pivotTable.Fields("Product"))
' Add the "Sales" field to the data area.
pivotTable.DataFields.Add(pivotTable.Fields("Sales"))
' Set the default style for the pivot table.
pivotTable.Style = workbook.TableStyles.DefaultPivotStyle
' #End Region ' #CreateFromRange
End Sub
Private Shared Sub CreatePivotTableFromCache(ByVal workbook As IWorkbook)
' #Region "#CreateFromPivotCache"
Dim worksheet As Worksheet = workbook.Worksheets.Add()
workbook.Worksheets.ActiveWorksheet = worksheet
' Create a pivot table based on the specified PivotTable cache.
Dim cache As PivotCache = workbook.Worksheets("Report1").PivotTables("PivotTable1").Cache
Dim pivotTable As PivotTable = worksheet.PivotTables.Add(cache, worksheet("B2"))
' Add the "Category" field to the row axis area.
pivotTable.RowFields.Add(pivotTable.Fields("Category"))
' Add the "Product" field to the row axis area.
pivotTable.RowFields.Add(pivotTable.Fields("Product"))
' Add the "Sales" field to the data area.
pivotTable.DataFields.Add(pivotTable.Fields("Sales"))
' Set the default style for the pivot table.
pivotTable.Style = workbook.TableStyles.DefaultPivotStyle
' #End Region ' #CreateFromPivotCache
End Sub
Private Shared Sub RemovePivotTable(ByVal workbook As IWorkbook)
' #Region "#RemoveTable"
Dim worksheet As Worksheet = workbook.Worksheets("Report1")
workbook.Worksheets.ActiveWorksheet = worksheet
' Access the pivot table by its name in the collection.
Dim pivotTable As PivotTable = worksheet.PivotTables("PivotTable1")
' Remove the pivot table from the collection.
worksheet.PivotTables.Remove(pivotTable)
' #End Region ' #RemoveTable
End Sub
Private Shared Sub ChangePivotTableLocation(ByVal workbook As IWorkbook)
' #Region "#ChangeLocation"
Dim worksheet As Worksheet = workbook.Worksheets("Report1")
workbook.Worksheets.ActiveWorksheet = worksheet
worksheet.Columns("A").WidthInCharacters = 40
' Change the pivot table location.
worksheet.PivotTables("PivotTable1").MoveTo(worksheet("A7"))
' Refresh the pivot table.
worksheet.PivotTables("PivotTable1").Cache.Refresh()
' #End Region ' #ChangeLocation
End Sub
Private Shared Sub MovePivotTableToWorksheet(ByVal workbook As IWorkbook)
' #Region "#MoveToWorksheet"
Dim worksheet As Worksheet = workbook.Worksheets("Report1")
' Create a new worksheet.
Dim targetWorksheet As Worksheet = workbook.Worksheets.Add()
' Access the pivot table by its name in the collection
' and move it to the new worksheet.
worksheet.PivotTables("PivotTable1").MoveTo(targetWorksheet("B2"))
' Refresh the pivot table.
targetWorksheet.PivotTables("PivotTable1").Cache.Refresh()
workbook.Worksheets.ActiveWorksheet = targetWorksheet
' #End Region ' #MoveToWorksheet
End Sub
Private Shared Sub ChangePivotTableDataSource(ByVal workbook As IWorkbook)
' #Region "#ChangeDataSource"
Dim worksheet As Worksheet = workbook.Worksheets("Report1")
workbook.Worksheets.ActiveWorksheet = worksheet
' Access the pivot table by its name in the collection.
Dim pivotTable As PivotTable = worksheet.PivotTables("PivotTable1")
Dim sourceWorksheet As Worksheet = workbook.Worksheets("Data2")
' Change the data source of the pivot table.
pivotTable.ChangeDataSource(sourceWorksheet("A1:H6367"))
' Add the "State" field to the row axis area.
pivotTable.RowFields.Add(pivotTable.Fields("State"))
' Add the "Yearly Earnings" field to the data area.
Dim dataField As PivotDataField = pivotTable.DataFields.Add(pivotTable.Fields("Yearly Earnings"))
' Calculate the average of the "Yearly Earnings" values for each state.
dataField.SummarizeValuesBy = PivotDataConsolidationFunction.Average
' #End Region ' #ChangeDataSource
End Sub
Private Shared Sub ClearPivotTable(ByVal workbook As IWorkbook)
' #Region "#ClearTable"
Dim worksheet As Worksheet = workbook.Worksheets("Report1")
workbook.Worksheets.ActiveWorksheet = worksheet
' Clear the pivot table.
worksheet.PivotTables("PivotTable1").Clear()
' #End Region ' #ClearTable
End Sub
Private Shared Sub ChangeBehaviorOptions(ByVal workbook As IWorkbook)
' #Region "#ChangeBehaviorOptions"
Dim worksheet As Worksheet = workbook.Worksheets("Report1")
workbook.Worksheets.ActiveWorksheet = worksheet
worksheet.Columns("B").WidthInCharacters = 40
' Access the pivot table by its name in the collection.
Dim pivotTable As PivotTable = worksheet.PivotTables("PivotTable1")
' Restrict specific operations for the pivot table.
Dim behaviorOptions As PivotBehaviorOptions = pivotTable.Behavior
behaviorOptions.AutoFitColumns = False
behaviorOptions.EnableFieldList = False
' Refresh the pivot table.
pivotTable.Cache.Refresh()
' #End Region ' #ChangeBehaviorOptions
End Sub
End Class
End Namespace