diff --git a/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/Excel/XLWorkbook_Load.cs index 15268d9..6df9e8e 100644 --- a/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/Excel/XLWorkbook_Load.cs @@ -701,12 +701,11 @@ if (df.Subtotal != null) pivotValue = pivotValue.SetSummaryFormula(df.Subtotal.Value.ToClosedXml()); if (df.ShowDataAs != null) { - var calculation = pivotValue.Calculation; - calculation = df.ShowDataAs.Value.ToClosedXml(); + var calculation = df.ShowDataAs.Value.ToClosedXml(); pivotValue = pivotValue.SetCalculation(calculation); } - if (df.BaseField != null) + if (df.BaseField?.Value != null) { var col = pt.SourceRange.Column(df.BaseField.Value + 1); @@ -716,7 +715,13 @@ .Distinct().ToList(); pivotValue.BaseField = col.FirstCell().GetValue(); - if (df.BaseItem != null) pivotValue.BaseItem = items[(int)df.BaseItem.Value].ToString(); + + if (df.BaseItem?.Value != null) + { + var bi = (int)df.BaseItem.Value; + if (bi.Between(0, items.Count - 1)) + pivotValue.BaseItem = items[(int)df.BaseItem.Value].ToString(); + } } } } diff --git a/ClosedXML_Tests/Excel/Loading/LoadingTests.cs b/ClosedXML_Tests/Excel/Loading/LoadingTests.cs index a82dca2..b7694d6 100644 --- a/ClosedXML_Tests/Excel/Loading/LoadingTests.cs +++ b/ClosedXML_Tests/Excel/Loading/LoadingTests.cs @@ -35,7 +35,8 @@ @"Misc\DuplicateImageNames.xlsx", @"Misc\InvalidPrintArea.xlsx", @"Misc\Date1904System.xlsx", - @"Misc\LoadImageWithoutTransform2D.xlsx" + @"Misc\LoadImageWithoutTransform2D.xlsx", + @"Misc\PivotTableWithTableSource.xlsx" }; foreach (var file in files) diff --git a/ClosedXML_Tests/Resource/Misc/PivotTableWithTableSource.xlsx b/ClosedXML_Tests/Resource/Misc/PivotTableWithTableSource.xlsx new file mode 100644 index 0000000..2a866e7 --- /dev/null +++ b/ClosedXML_Tests/Resource/Misc/PivotTableWithTableSource.xlsx Binary files differ