Table of Contents

Pivot tables

XlsxDocumentSheet.PivotTables lists a sheet's pivot tables as their definitions and caches state them: where each is, what it reads, the fields on its rows, columns and filters with their items, its value fields and how they summarize, its calculated fields, its layout and its style. Measured against Excel: nine pivots Excel made, every field, position, caption and function as Excel's object model lists them. A pivot table that is read and not changed is saved as the bytes it was; one is made of a range or a table, and computed again from its source, as Excel does.

dotnet add package DocWright.Spreadsheet

List a sheet's pivot tables

using DocWright.Spreadsheet;

XlsxDocument workbook = XlsxDocument.Open(File.OpenRead("sales-pivots.xlsx"));
XlsxDocumentSheet sheet = workbook.FindSheet("Pivots")!;
foreach (XlsxPivotTable pivot in sheet.PivotTables)
{
    Console.WriteLine($"{pivot} reads {pivot.Source}, {pivot.Layout} layout");
    foreach (XlsxPivotField row in pivot.RowFields)
    {
        Console.WriteLine($"  rows: {row.Name} [{string.Join(", ", row.Items)}]");
    }

    foreach (XlsxPivotField column in pivot.ColumnFields)
    {
        Console.WriteLine($"  columns: {column.Name}");
    }

    foreach (XlsxPivotDataField value in pivot.DataFields)
    {
        Console.WriteLine($"  values: {value}");
    }
}

Location is the range the table's body covers; a filter field's rows above it are not in it. Source is a range of a sheet, as Sales!$A$1:$E$25, or a table's name. A field's Items are in the order the table shows them, each as the cache holds it. A value field prints as its caption, its function and the field it summarizes.

Filters, sorting, groups and calculated fields

using DocWright.Spreadsheet;

XlsxPivotTables pivots = sheet.PivotTables;
XlsxPivotField product = pivots.Find("page")!.Filters[0];
Console.WriteLine($"filter {product.Name} shows {product.SelectedItem}");

XlsxPivotField region = pivots.Find("sorted")!.RowFields[0];
Console.WriteLine($"{region.Name} sorted {region.Sort}, hiding {string.Join(", ", region.HiddenItems)}");

XlsxPivotField revenue = pivots.Find("calc")!.CalculatedFields[0];
Console.WriteLine($"calculated {revenue.Name} = {revenue.Formula}");

The whole run prints:

cross at E3:I9 reads Sales!$A$1:$E$25, Compact layout
  rows: Region [East, North, South, West]
  columns: Product
  values: Sum of Units = Sum(Units)
calc at I30:J35 reads Sales!$A$1:$E$25, Compact layout
  rows: Region [East, North, South, West]
  values: Sum of Revenue = Sum(Revenue)
multi at K3:O20 reads Sales!$A$1:$E$25, Outline layout
  rows: Region [East, North, South, West]
  rows: Product [A, B, C]
  values: Sum of Units = Sum(Units)
  values: Average of Price = Average(Price)
  values: Count of Units = Count(Units)
table at M30:N34 reads SalesTable, Compact layout
  rows: Product [A, B, C]
  values: Sum of Price = Sum(Price)
sorted at Q30:R34 reads Sales!$A$1:$E$25, Compact layout
  rows: Region [East, North, South, West]
  values: Sum of Units = Sum(Units)
page at Q3:R8 reads Sales!$A$1:$E$25, Compact layout
  rows: Region [East, North, South, West]
  values: Max of Units = Max(Units)
tabular at A30:C42 reads Sales!$A$1:$E$25, Tabular layout
  rows: Region [East, North, South, West]
  rows: Product [A, B, C]
  values: Sum of Units = Sum(Units)
dates at E30:F35 reads Sales!$A$1:$E$25, Compact layout
  rows: Months (Date) [<1/01/2025, Jan, Feb, Mar, Apr, May, Jun, Jul, Aug, Sept, Oct, Nov, Dec, >29/04/2025]
  values: Sum of Units = Sum(Units)
rows at A3:B8 reads Sales!$A$1:$E$25, Compact layout
  rows: Region [East, North, South, West]
  values: Sum of Units = Sum(Units)
filter Product shows B
Region sorted Descending, hiding West
calculated Revenue = Units*Price

A date grouped by months is a field of its own, Months (Date), whose items are the names Excel wrote when it grouped them, in the language it ran in.

Make a pivot table

PivotTables.Add makes a pivot table of a range (its first row the field names) or a table, with the fields arranged as XlsxPivotTableOptions says, and computes it at once: the cache, the definition and the cells are written as Excel's own refresh writes them, so the saved workbook shows the table in Excel and in any viewer.

using DocWright.Spreadsheet;

XlsxDocument workbook = XlsxDocument.Open(File.OpenRead("sales-pivots.xlsx"));
XlsxDocumentSheet sheet = workbook.FindSheet("Table")!;

var options = new XlsxPivotTableOptions { Name = "By region" };
options.RowFields.Add("Region");
options.ColumnFields.Add("Product");
options.DataFields.Add(new XlsxPivotDataField("Units"));
XlsxPivotTable pivot = sheet.PivotTables.Add("Sales!A1:E25", "H3", options);
Console.WriteLine($"{pivot}: {sheet.GetCell("H5").Value.TextValue} {sheet.GetCell("I5").Value.NumberValue}, grand total {sheet.GetCell("L9").Value.NumberValue}");

A value field is captioned as Excel captions it ("Sum of Units", "Average of Price") unless you give a name. Layout chooses the compact, outline or tabular form; Filters puts report filters above the table.

Refresh it

When the source changes, Refresh() computes the table again. New items come after the items the table keeps, in the order the source first shows them; a field filtered by hand keeps new items hidden, as Excel's refresh does.

using DocWright.Spreadsheet;

workbook.FindSheet("Sales")!.SetValue("A2", XlsxCellValue.Text("Central"));
pivot.Refresh();
Console.WriteLine($"{pivot}: rows {string.Join(", ", pivot.RowFields[0].Items)}");
By region at H3:L9: East 32, grand total 269
By region at H3:L10: rows East, North, South, West, Central

Download the workbook: sales-pivots-made.xlsx. Measured against Excel: after the same change to their source, Excel's nine pivots refreshed here are written as Excel writes them, part for part, and Excel's own refresh of a pivot made here changes none of its cells.

Note

A pivot table that uses dates grouped by months or a calculated field is read but not refreshed (UnsupportedFeatureException); the fields of an existing table are not moved, so make a new table for a new arrangement.