-
Notifications
You must be signed in to change notification settings - Fork 2
/
PivotCalculatedFieldActions.cs
66 lines (60 loc) · 3.28 KB
/
PivotCalculatedFieldActions.cs
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
using DevExpress.Spreadsheet;
namespace SpreadsheetDocServerPivotAPI
{
class PivotCalculatedFieldActions
{
static void AddCalculatedField(IWorkbook workbook)
{
#region #AddCalculatedField
Worksheet worksheet = workbook.Worksheets["Report1"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Access the pivot table by its name in the collection.
PivotTable pivotTable = worksheet.PivotTables["PivotTable1"];
// Create a calculated field based on data in the "Sales" field.
PivotField field = pivotTable.CalculatedFields.Add("=Sales*10%", "Sales Tax");
// Add the calculated field to the data area and specify the custom field name.
PivotDataField dataField = pivotTable.DataFields.Add(field, "Total Tax");
// Specify the number format for the data field.
dataField.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* (#,##0.00);_([$$-409]* "" - ""??_);_(@_)";
#endregion #AddCalculatedField
}
static void RemoveCalculatedField(IWorkbook workbook)
{
#region #RemoveCalculatedField
Worksheet worksheet = workbook.Worksheets["Report1"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Access the pivot table by its name in the collection.
PivotTable pivotTable = worksheet.PivotTables["PivotTable1"];
// Create a calculated field based on data in the "Sales" field.
pivotTable.CalculatedFields.Add("=Sales*10%", "Sales Tax");
// Access the calculated field by its name in the collection.
PivotField field = pivotTable.CalculatedFields["Sales Tax"];
// Add the calculated field to the data area.
PivotDataField dataField = pivotTable.DataFields.Add(field);
//Remove the calculated field.
pivotTable.CalculatedFields.RemoveAt(0);
#endregion #RemoveCalculatedField
}
static void ModifyCalculatedField(IWorkbook workbook)
{
#region #ModifyCalculatedField
Worksheet worksheet = workbook.Worksheets["Report1"];
workbook.Worksheets.ActiveWorksheet = worksheet;
// Access the pivot table by its name in the collection.
PivotTable pivotTable = worksheet.PivotTables["PivotTable1"];
// Create a calculated field based on data in the "Sales" field.
pivotTable.CalculatedFields.Add("=Sales*10%", "Sales Tax Rate 10");
// Access the calculated field by its name in the collection.
PivotField field = pivotTable.CalculatedFields["Sales Tax Rate 10"];
//Change the formula for the calculated field.
field.Formula = "=Sales*15%";
//Change the calculated field name.
field.Name = "Sales Tax Rate 15";
//Add the calculated field to the data area and specify the custom field name.
PivotDataField dataField = pivotTable.DataFields.Add(field, "Total Tax");
// Specify the number format for the data field.
dataField.NumberFormat = @"_([$$-409]* #,##0.00_);_([$$-409]* (#,##0.00);_([$$-409]* "" - ""??_);_(@_)";
#endregion #ModifyCalculatedField
}
}
}