-
Notifications
You must be signed in to change notification settings - Fork 891
Pivot Table example
k3davis edited this page Mar 2, 2021
·
5 revisions
NOTE: Pivot table support is still very experimental.
In this example, we'll create a pivot table of monthly pastry sales. First, we'll need our Pastry class:
public class Pastry
{
public Pastry(string name, int amount, string month)
{
Month = month;
Name = name;
NumberOfOrders = amount;
}
public string Name { get; set; }
public int NumberOfOrders { get; set; }
public string Month { get; set; }
}
Next, we'll mock up some data:
var pastries = new List<Pastry>
{
new Pastry("Croissant", 150, "Apr"),
new Pastry("Croissant", 250, "May"),
new Pastry("Croissant", 134, "June"),
new Pastry("Doughnut", 250, "Apr"),
new Pastry("Doughnut", 225, "May"),
new Pastry("Doughnut", 210, "June"),
new Pastry("Bearclaw", 134, "Apr"),
new Pastry("Bearclaw", 184, "May"),
new Pastry("Bearclaw", 124, "June"),
new Pastry("Danish", 394, "Apr"),
new Pastry("Danish", 190, "May"),
new Pastry("Danish", 221, "June"),
new Pastry("Scone", 135, "Apr"),
new Pastry("Scone", 122, "May"),
new Pastry("Scone", 243, "June")
};
And then we'll create a worksheet with this data in a table:
var workbook = new XLWorkbook();
var sheet = workbook.Worksheets.Add("PastrySalesData");
// Insert our list of pastry data into the "PastrySalesData" sheet at cell 1,1
var table = sheet.Cell(1, 1).InsertTable(pastries, "PastrySalesData", true);
Finally, we'll use that table as the source for our pivot table:
// Add a new sheet for our pivot table
var ptSheet = workbook.Worksheets.Add("PivotTable");
// Create the pivot table, using the data from the "PastrySalesData" table
var pt = ptSheet.PivotTables.Add("PivotTable", ptSheet.Cell(1, 1), table.AsRange());
// The rows in our pivot table will be the names of the pastries
pt.RowLabels.Add("Name");
// The columns will be the months
pt.ColumnLabels.Add("Month");
// The values in our table will come from the "NumberOfOrders" field
// The default calculation setting is a total of each row/column
pt.Values.Add("NumberOfOrders");
This will create a pivot table with a row for each pastry, a column for each month, and sales numbers in the cells. Each column and row will be totaled.
- How do I deliver an Excel file in ASP.NET?
- Does it support Excel 2003 and prior formats (.xls)?
- How can I insert an image?
- Text with numbers are getting converted to numbers, what's up with that?
- How do I get the result of a formula?
- Data Types
- Creating Multiple Worksheets
- Organizing Sheets
- Loading and Modifying Files
- Using Lambda Expressions
- Cell Values
- Workbook Properties
- Using Formulas
- Evaluating Formulas
- Creating Rows And Columns Outlines
- Hide Unhide Rows And Columns
- Freeze Panes
- Copying Worksheets
- Using Hyperlinks
- Data Validation
- Hide Worksheets
- Sheet Protection
- Tab Colors
- Conditional Formatting
- Pivot Table example
- Sparklines
- Copying IEnumerable Collections
- Inserting Data
- Inserting Tables
- Adding DataTable as Worksheet
- Adding DataSet
- Styles - Alignment
- Styles - Border
- Styles - Fill
- Styles - Font
- Styles - NumberFormat
- NumberFormatId Lookup Table
- Style Worksheet
- Style Rows and Columns
- Using Default Styles
- Using Colors
- ClosedXML Predefined Colors
- Excel Indexed Colors
- Using Rich Text
- Using Phonetics
- Defining Ranges
- Merging Cells
- Clearing Ranges
- Deleting Ranges
- Multiple Ranges
- Shifting Ranges
- Transpose Ranges
- Named Ranges
- Accessing Named Ranges
- Copying Ranges
- Using Tables
- Sorting Data
- Selecting Cells and Ranges
- Row Height and Styles
- Selecting Rows
- Inserting Rows
- Inserting and Deleting Rows
- Adjust Row Height and Column Width to Contents
- Row Cells
- Column Width and Styles
- Selecting Columns
- Inserting Columns
- Inserting and Deleting Columns
- Adjust Row Height and Column Width to Contents
- Column Cells
- Pages Tab
- Paper Size Lookup Table
- Margins Tab
- Headers and Footers Tab
- Sheet Tab
- Print Areas and Page Breaks