...
Note |
---|
Excel does not encourage placing more than one PivotTable on a worksheet because PivotTables are not allowed to overlap. ExcelWriter cannot render PivotTables, so it does not have the ability to calculate which cells the PivotTable occupies and cannot detect when PivotTables might overlap. To avoid this, we encourage you to keep each PivotTable your PivotTables on separate worksheets. |
Writing the code
Step 1. Set up the data source
The data source needs to be a continuous block of cells with a header row with column names. The data source can be defined as an Area
or a NamedRange
.
Here is a snapshot of the data for this tutorial, which can also be found in Example.xlsx:
<IMAGE>
Info |
---|
Download Example.xlsx to see the full sample data set. |
In this case, the data source for the PivotTable will be a dynamically defined Area
on the data worksheet. Note that the row of column names is included in the area.
Code Block |
---|
Area data_area = data_ws.CreateArea(0, 0, 9, 244); |
Step 2. Create the PivotTable
CREATING A PIVOT TABLE
To create a PivotTable
, call CreatePivotTable
on the PivotTables
collection. Specify the 0-indexed row and column values for the PivotTable location:
Code Block |
---|
PivotTable pt = pivot_ws.PivotTables.CreatePivotTable(data_area, 5, 5);
|
After creating the PivotTable, always set RefreshOnOpen
to true
.
...