//create a new workbook var workbook = new GrapeCity.Documents.Excel.Workbook(); object[,] sourceData = new object[,] { { "Order ID", "Product", "Category", "Amount", "Date", "Country" }, { 1, "Bose 785593-0050", "Consumer Electronics", 4270, new DateTime(2018, 1, 6), "United States" }, { 2, "Canon EOS 1500D", "Consumer Electronics", 8239, new DateTime(2018, 1, 7), "United Kingdom" }, { 3, "Haier 394L 4Star", "Consumer Electronics", 617, new DateTime(2018, 1, 8), "United States" }, { 4, "IFB 6.5 Kg FullyAuto", "Consumer Electronics", 8384, new DateTime(2018, 1, 10), "Canada" }, { 5, "Mi LED 40inch", "Consumer Electronics", 2626, new DateTime(2018, 1, 10), "Germany" }, { 6, "Sennheiser HD 4.40-BT", "Consumer Electronics", 3610, new DateTime(2018, 1, 11), "United States" }, { 7, "Iphone XR", "Mobile", 9062, new DateTime(2018, 1, 11), "Australia" }, { 8, "OnePlus 7Pro", "Mobile", 6906, new DateTime(2018, 1, 16), "New Zealand" }, { 9, "Redmi 7", "Mobile", 2417, new DateTime(2018, 1, 16), "France" }, { 10, "Samsung S9", "Mobile", 7431, new DateTime(2018, 1, 16), "Canada" }, { 11, "OnePlus 7Pro", "Mobile", 8250, new DateTime(2018, 1, 16), "Germany" }, { 12, "Redmi 7", "Mobile", 7012, new DateTime(2018, 1, 18), "United States" }, { 13, "Bose 785593-0050", "Consumer Electronics", 1903, new DateTime(2018, 1, 20), "Germany" }, { 14, "Canon EOS 1500D", "Consumer Electronics", 2824, new DateTime(2018, 1, 22), "Canada" }, { 15, "Haier 394L 4Star", "Consumer Electronics", 6946, new DateTime(2018, 1, 24), "France" }, }; IWorksheet worksheet = workbook.Worksheets[0]; worksheet.Range["J1:O16"].Value = sourceData; worksheet.Range["J:O"].ColumnWidth = 15; var pivotcache = workbook.PivotCaches.Create(worksheet.Range["J1:O16"]); var pivottable = worksheet.PivotTables.Add(pivotcache, worksheet.Range["A1"], "pivottable1"); worksheet.Range["L1:L16"].NumberFormat = "$#,##0.00"; //config pivot table's fields var field_Date = pivottable.PivotFields["Date"]; field_Date.Orientation = PivotFieldOrientation.PageField; var field_Category = pivottable.PivotFields["Category"]; field_Category.Orientation = PivotFieldOrientation.RowField; var field_Product = pivottable.PivotFields["Product"]; field_Product.Orientation = PivotFieldOrientation.RowField; var field_Amount = pivottable.PivotFields["Amount"]; field_Amount.Orientation = PivotFieldOrientation.DataField; field_Amount.NumberFormat = "$#,##0.00"; var field_Country = pivottable.PivotFields["Country"]; field_Country.Orientation = PivotFieldOrientation.ColumnField; // Create pivot style "test" ITableStyle pivotStyle = workbook.TableStyles.Add("test"); // Set table style as pivot table style pivotStyle.ShowAsAvailablePivotStyle = true; pivotStyle.TableStyleElements[TableStyleElementType.PageFieldLabels].Interior.Color = System.Drawing.Color.LightBlue; pivotStyle.TableStyleElements[TableStyleElementType.PageFieldValues].Interior.Color = System.Drawing.Color.LightBlue; pivotStyle.TableStyleElements[TableStyleElementType.GrandTotalColumn].Interior.Color = System.Drawing.Color.LightGreen; pivotStyle.TableStyleElements[TableStyleElementType.GrandTotalRow].Interior.Color = System.Drawing.Color.LightGreen; pivotStyle.TableStyleElements[TableStyleElementType.HeaderRow].Interior.Color = System.Drawing.Color.Cyan; pivotStyle.TableStyleElements[TableStyleElementType.FirstColumn].Interior.Color = System.Drawing.Color.Tomato; pivotStyle.TableStyleElements[TableStyleElementType.FirstRowStripe].Interior.Color = System.Drawing.Color.Yellow; pivotStyle.TableStyleElements[TableStyleElementType.SecondRowStripe].Interior.Color = System.Drawing.Color.LightYellow; // Set ShowTableStyleRowStripes as true pivottable.ShowTableStyleRowStripes = true; // Set pivot table style pivottable.Style = pivotStyle; worksheet.Range["A:I"].EntireColumn.AutoFit(); // Save to an excel file workbook.Save("SetCustomPivotStyle.xlsx");
' Create a new Workbook Dim workbook As New Workbook Dim sourceData(,) As Object = { {"Order ID", "Product", "Category", "Amount", "Date", "Country"}, {1, "Bose 785593-0050", "Consumer Electronics", 4270, #2018-1-6#, "United States"}, {2, "Canon EOS 1500D", "Consumer Electronics", 8239, #2018-1-7#, "United Kingdom"}, {3, "Haier 394L 4Star", "Consumer Electronics", 617, #2018-1-8#, "United States"}, {4, "IFB 6.5 Kg FullyAuto", "Consumer Electronics", 8384, #2018-1-10#, "Canada"}, {5, "Mi LED 40inch", "Consumer Electronics", 2626, #2018-1-10#, "Germany"}, {6, "Sennheiser HD 4.40-BT", "Consumer Electronics", 3610, #2018-1-11#, "United States"}, {7, "Iphone XR", "Mobile", 9062, #2018-1-11#, "Australia"}, {8, "OnePlus 7Pro", "Mobile", 6906, #2018-1-16#, "New Zealand"}, {9, "Redmi 7", "Mobile", 2417, #2018-1-16#, "France"}, {10, "Samsung S9", "Mobile", 7431, #2018-1-16#, "Canada"}, {11, "OnePlus 7Pro", "Mobile", 8250, #2018-1-16#, "Germany"}, {12, "Redmi 7", "Mobile", 7012, #2018-1-18#, "United States"}, {13, "Bose 785593-0050", "Consumer Electronics", 1903, #2018-1-20#, "Germany"}, {14, "Canon EOS 1500D", "Consumer Electronics", 2824, #2018-1-22#, "Canada"}, {15, "Haier 394L 4Star", "Consumer Electronics", 6946, #2018-1-24#, "France"} } Dim worksheet As IWorksheet = workbook.Worksheets(0) worksheet.Range("J1:O16").Value = sourceData worksheet.Range("J:O").ColumnWidth = 15 Dim pivotcache = workbook.PivotCaches.Create(worksheet.Range("J1:O16")) Dim pivottable = worksheet.PivotTables.Add(pivotcache, worksheet.Range!A1, "pivottable1") worksheet.Range("L1:L16").NumberFormat = "$#,##0.00" 'config pivot table's fields Dim field_Date = pivottable.PivotFields!Date field_Date.Orientation = PivotFieldOrientation.PageField Dim field_Category = pivottable.PivotFields!Category field_Category.Orientation = PivotFieldOrientation.RowField Dim field_Product = pivottable.PivotFields!Product field_Product.Orientation = PivotFieldOrientation.RowField Dim field_Amount = pivottable.PivotFields!Amount field_Amount.Orientation = PivotFieldOrientation.DataField field_Amount.NumberFormat = "$#,##0.00" Dim field_Country = pivottable.PivotFields!Country field_Country.Orientation = PivotFieldOrientation.ColumnField ' Create pivot style "test" Dim pivotStyle As ITableStyle = workbook.TableStyles.Add("test") ' Set table style as pivot table style pivotStyle.ShowAsAvailablePivotStyle = True pivotStyle.TableStyleElements(TableStyleElementType.PageFieldLabels).Interior.Color = System.Drawing.Color.LightBlue pivotStyle.TableStyleElements(TableStyleElementType.PageFieldValues).Interior.Color = System.Drawing.Color.LightBlue pivotStyle.TableStyleElements(TableStyleElementType.GrandTotalColumn).Interior.Color = System.Drawing.Color.LightGreen pivotStyle.TableStyleElements(TableStyleElementType.GrandTotalRow).Interior.Color = System.Drawing.Color.LightGreen pivotStyle.TableStyleElements(TableStyleElementType.HeaderRow).Interior.Color = System.Drawing.Color.Cyan pivotStyle.TableStyleElements(TableStyleElementType.FirstColumn).Interior.Color = System.Drawing.Color.Tomato pivotStyle.TableStyleElements(TableStyleElementType.FirstRowStripe).Interior.Color = System.Drawing.Color.Yellow pivotStyle.TableStyleElements(TableStyleElementType.SecondRowStripe).Interior.Color = System.Drawing.Color.LightYellow ' Set ShowTableStyleRowStripes as true pivottable.ShowTableStyleRowStripes = True ' Set pivot table style pivottable.Style = pivotStyle worksheet.Range("A:I").EntireColumn.AutoFit() ' save to an excel file workbook.Save("SetCustomPivotStyle.xlsx")