Page tree

Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Table of Contents

Table of Contents

Intro

This example takes an existing workbook that contains some data and creates a PivotTable. The workbook used in this example is available for download: Download Example.xlsx.

Before writing any PivotTable code, make sure to open the workbook with ExcelApplication and get references to the data worksheet and a worksheet for the PivotTable.

Code Block
ExcelApplication xla = new ExcelApplication();

//Example.xlsx has a worksheet 'Data' with 9 columns and 244 rows of data
Workbook wb = xla.Open("\\Example.xlsx");

Worksheet data_ws = wb.Worksheets["Data"];
Worksheet pivot_ws = wb.Worksheets.CreateWorksheet("Pivot");
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 on separate worksheets.

Writing the code

Set up the data source

...