# Pivot tables and pivot charts

**EPPlus supports both creating new and updating existing pivot tables in a workbook. Pivot tables are created and altered via the PivotTables collection in the `ExcelWorksheet` class.**

### Add a pivot table

Here is an example of how a pivot table can be added to a workbook:

```csharp
var wsPivot = pck.Workbook.Worksheets.Add("PivotSimple");
var pivotTable1 = wsPivot.PivotTables.Add(wsPivot.Cells["A1"], dataRange, "PerCountry");

pivotTable1.RowFields.Add(pivotTable1.Fields["Country"]);
var dataField = pivotTable1.DataFields.Add(pivotTable1.Fields["OrderValue"]);
dataField.Format="#,##0";
pivotTable1.DataOnRows = true;
```

```vbnet
Dim wsPivot = pck.Workbook.Worksheets.Add("PivotSimple")
Dim pivotTable1 = wsPivot.PivotTables.Add(wsPivot.Cells("A1"), dataRange, "PerCountry")

pivotTable1.RowFields.Add(pivotTable1.Fields("Country"))
Dim dataField = pivotTable1.DataFields.Add(pivotTable1.Fields("OrderValue"))
dataField.Format = "#,##0"
pivotTable1.DataOnRows = True
```

In this example a pivot table will be created starting from cell A1. One row field and one data field is added. You can also add column fields and page fields.

### Add a pivot chart

When you have created a pivot table you can easily create a pivot chart on the pivot table:

```csharp
var chart = wsPivot.Drawings.AddPieChart("PivotChart", ePieChartType.PieExploded3D, pivotTable1);
chart.SetPosition(1, 0, 4, 0);
chart.SetSize(800, 600);
chart.Legend.Remove();
chart.Series[0].DataLabel.ShowCategory = true;
chart.Series[0].DataLabel.Position = eLabelPosition.OutEnd;
chart.StyleManager.SetChartStyle(ePresetChartStyle.Pie3dChartStyle6);
```

```vbnet
Dim chart = wsPivot.Drawings.AddPieChart("PivotChart", ePieChartType.PieExploded3D, pivotTable1)
chart.SetPosition(1, 0, 4, 0)
chart.SetSize(800, 600)
chart.Legend.Remove()
chart.Series(0).DataLabel.ShowCategory = True
chart.Series(0).DataLabel.Position = eLabelPosition.OutEnd
chart.StyleManager.SetChartStyle(ePresetChartStyle.Pie3dChartStyle6)
```

Here we create a 3D exploded pie chart on the data, setting the style to preset style 6. Pivot charts are added as normal charts via the `Add[Chart type]Chart` methods in the `Drawings` Collection as described [here](<https://epplussoftware.com/docs/charts-and-drawing-objects/charts/index.md>). The pivot table is added as argument three of this method.

### Restrictions of pivot tables sources.

EPPlus do not update connections to external workbooks, data models or connections.

### See also

[External connections &amp; query tables](<https://epplussoftware.com/docs/import-export-data/external-connections-and-query-tables/index.md>) (introduced in EPPlus 8.3) for using pivot tables with an external connection.

[Pivot Table Sample 7.2-C\#](<https://github.com/EPPlusSoftware/EPPlus.Samples.CSharp/tree/master/07-Tables pivot tables and slicers/02-Pivot tables>) or [Pivot Table Sample 7.2-VB](<https://github.com/EPPlusSoftware/EPPlus.Samples.VB/blob/master/07-Tables pivot tables and slicers/02-Pivot tables>)
