diff --git a/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/Excel/XLWorkbook_Load.cs index b1a38ef..ae4eab7 100644 --- a/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/Excel/XLWorkbook_Load.cs @@ -481,60 +481,70 @@ pt.ErrorValueReplacement = pivotTableDefinition.ErrorCaption.Value; // Row labels - foreach (var rf in pivotTableDefinition.RowFields.Cast()) + if (pivotTableDefinition.RowFields != null) { - if (rf.Index.Value == -2) - pt.RowLabels.Add(XLConstants.PivotTableValuesSentinalLabel); - else if (rf.Index < pivotTableDefinition.PivotFields.Count) + foreach (var rf in pivotTableDefinition.RowFields.Cast()) { - var pf = pivotTableDefinition.PivotFields.ElementAt(rf.Index.Value) as PivotField; - if (pf != null && pf.Name != null) pt.RowLabels.Add(pf.Name.Value); + if (rf.Index.Value == -2) + pt.RowLabels.Add(XLConstants.PivotTableValuesSentinalLabel); + else if (rf.Index < pivotTableDefinition.PivotFields.Count) + { + var pf = pivotTableDefinition.PivotFields.ElementAt(rf.Index.Value) as PivotField; + if (pf != null && pf.Name != null) pt.RowLabels.Add(pf.Name.Value); + } } } // Column labels - foreach (var cf in pivotTableDefinition.ColumnFields.Cast()) + if (pivotTableDefinition.ColumnFields != null) { - if (cf.Index.Value == -2) - pt.ColumnLabels.Add(XLConstants.PivotTableValuesSentinalLabel); - - else if (cf.Index < pivotTableDefinition.PivotFields.Count) + foreach (var cf in pivotTableDefinition.ColumnFields.Cast()) { - var pf = pivotTableDefinition.PivotFields.ElementAt(cf.Index.Value) as PivotField; - if (pf != null && pf.Name != null) pt.ColumnLabels.Add(pf.Name.Value); + if (cf.Index.Value == -2) + pt.ColumnLabels.Add(XLConstants.PivotTableValuesSentinalLabel); + + else if (cf.Index < pivotTableDefinition.PivotFields.Count) + { + var pf = pivotTableDefinition.PivotFields.ElementAt(cf.Index.Value) as PivotField; + if (pf != null && pf.Name != null) pt.ColumnLabels.Add(pf.Name.Value); + } } } // Values - foreach (var df in pivotTableDefinition.DataFields.Cast()) + if (pivotTableDefinition.DataFields != null) { - if ((int)df.Field.Value == -2) - pt.Values.Add(XLConstants.PivotTableValuesSentinalLabel); - - else if (df.Field.Value < pivotTableDefinition.PivotFields.Count) + foreach (var df in pivotTableDefinition.DataFields.Cast()) { - var pf = pivotTableDefinition.PivotFields.ElementAt((int)df.Field.Value) as PivotField; - if (pf != null && pf.Name != null) + if ((int)df.Field.Value == -2) + pt.Values.Add(XLConstants.PivotTableValuesSentinalLabel); + + else if (df.Field.Value < pivotTableDefinition.PivotFields.Count) { - var pv = pt.Values.Add(pf.Name.Value, df.Name.Value); - if (df.NumberFormatId != null) pv.NumberFormat.SetNumberFormatId((int)df.NumberFormatId.Value); - if (df.Subtotal != null) pv = pv.SetSummaryFormula(df.Subtotal.Value.ToClosedXml()); - if (df.ShowDataAs != null) + var pf = pivotTableDefinition.PivotFields.ElementAt((int)df.Field.Value) as PivotField; + if (pf != null && pf.Name != null) { - var calculation = pv.Calculation; - calculation = df.ShowDataAs.Value.ToClosedXml(); - pv = pv.SetCalculation(calculation); - } - if (df.BaseField != null) { - var col = pt.SourceRange.Column(df.BaseField.Value + 1); + var pv = pt.Values.Add(pf.Name.Value, df.Name.Value); + if (df.NumberFormatId != null) pv.NumberFormat.SetNumberFormatId((int)df.NumberFormatId.Value); + if (df.Subtotal != null) pv = pv.SetSummaryFormula(df.Subtotal.Value.ToClosedXml()); + if (df.ShowDataAs != null) + { + var calculation = pv.Calculation; + calculation = df.ShowDataAs.Value.ToClosedXml(); + pv = pv.SetCalculation(calculation); + } + if (df.BaseField != null) + { + var col = pt.SourceRange.Column(df.BaseField.Value + 1); - var items = col.CellsUsed() - .Select(c => c.Value) - .Skip(1) // Skip header column - .Distinct().ToList(); + var items = col.CellsUsed() + .Select(c => c.Value) + .Skip(1) // Skip header column + .Distinct().ToList(); - pv.BaseField = col.FirstCell().GetValue(); - if (df.BaseItem != null) pv.BaseItem = items[(int)df.BaseItem.Value].ToString(); + pv.BaseField = col.FirstCell().GetValue(); + if (df.BaseItem != null) pv.BaseItem = items[(int)df.BaseItem.Value].ToString(); + } } } } @@ -545,7 +555,6 @@ } #endregion - } #region Comment Helpers diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index 4d14f46..aee1e92 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -2110,10 +2110,9 @@ pf.Axis = PivotTableAxisValues.AxisPage; pageFields.AppendChild(new PageField { Hierarchy = -1, Field = pt.Fields.IndexOf(xlpf) }); } - else if (pt.Values.Any(p => p.SourceName == xlpf.SourceName)) - { + + if (pt.Values.Any(p => p.SourceName == xlpf.SourceName)) pf.DataField = true; - } var fieldItems = new Items(); diff --git a/ClosedXML_Examples/PivotTables/PivotTables.cs b/ClosedXML_Examples/PivotTables/PivotTables.cs index 9af9775..92d28a7 100644 --- a/ClosedXML_Examples/PivotTables/PivotTables.cs +++ b/ClosedXML_Examples/PivotTables/PivotTables.cs @@ -63,10 +63,10 @@ for (int i = 1; i <= 3; i++) { // Add a new sheet for our pivot table - ptSheet = wb.Worksheets.Add("PivotTable" + i); + ptSheet = wb.Worksheets.Add("pvt" + i); // Create the pivot table, using the data from the "PastrySalesData" table - pt = ptSheet.PivotTables.AddNew("PivotTable", ptSheet.Cell(1, 1), dataRange); + pt = ptSheet.PivotTables.AddNew("pvt", ptSheet.Cell(1, 1), dataRange); // The rows in our pivot table will be the names of the pastries pt.RowLabels.Add("Name"); @@ -96,8 +96,8 @@ } // Different kind of pivot - ptSheet = wb.Worksheets.Add("PivotTableNoColumnLabels"); - pt = ptSheet.PivotTables.AddNew("PivotTableNoColumnLabels", ptSheet.Cell(1, 1), dataRange); + ptSheet = wb.Worksheets.Add("pvtNoColumnLabels"); + pt = ptSheet.PivotTables.AddNew("pvtNoColumnLabels", ptSheet.Cell(1, 1), dataRange); pt.RowLabels.Add("Name"); pt.RowLabels.Add("Month"); @@ -105,6 +105,15 @@ pt.Values.Add("NumberOfOrders").SetSummaryFormula(XLPivotSummary.Sum);//.NumberFormat.Format = "#0.00"; pt.Values.Add("Quality").SetSummaryFormula(XLPivotSummary.Sum);//.NumberFormat.Format = "#0.00"; + // Pivot table with a field both as a value and as a row/column/filter label + ptSheet = wb.Worksheets.Add("pvtFieldAsValueAndLabel"); + pt = ptSheet.PivotTables.AddNew("pvtFieldAsValueAndLabel", ptSheet.Cell(1, 1), dataRange); + + pt.RowLabels.Add("Name"); + pt.RowLabels.Add("Month"); + + pt.Values.Add("Name").SetSummaryFormula(XLPivotSummary.Count);//.NumberFormat.Format = "#0.00"; + wb.SaveAs(filePath); } } diff --git a/ClosedXML_Tests/Resource/Examples/PivotTables/PivotTables.xlsx b/ClosedXML_Tests/Resource/Examples/PivotTables/PivotTables.xlsx index d28109f..04cd992 100644 --- a/ClosedXML_Tests/Resource/Examples/PivotTables/PivotTables.xlsx +++ b/ClosedXML_Tests/Resource/Examples/PivotTables/PivotTables.xlsx Binary files differ