diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index 88617f9..7ad2546 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -2025,8 +2025,7 @@ // -2 is the sentinal value for "Values" if (pt.ColumnLabels.Any(cl => cl.SourceName == XLConstants.PivotTableValuesSentinalLabel)) columnFields.AppendChild(new Field { Index = -2 }); - - if (pt.RowLabels.Any(rl => rl.SourceName == XLConstants.PivotTableValuesSentinalLabel)) + else if (pt.RowLabels.Any(rl => rl.SourceName == XLConstants.PivotTableValuesSentinalLabel)) { pivotTableDefinition.DataOnRows = true; rowFields.AppendChild(new Field { Index = -2 }); @@ -2052,7 +2051,7 @@ 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 && p.CustomName != xlpf.SourceName)) + else if (pt.Values.Any(p => p.SourceName == xlpf.SourceName)) { pf.DataField = true; } @@ -2148,17 +2147,25 @@ rowItems.Count = Convert.ToUInt32(rowItems.Count()); pivotTableDefinition.AppendChild(rowItems); - if (!pt.ColumnLabels.Any()) + if (!pt.ColumnLabels.Any(cl => cl.CustomName != XLConstants.PivotTableValuesSentinalLabel)) { - columnItems.AppendChild(new RowItem()); - columnItems.Count = Convert.ToUInt32(columnItems.Count()); - pivotTableDefinition.AppendChild(columnItems); + for (int i = 0; i < pt.Values.Count(); i++) + { + var rowItem = new RowItem(); + rowItem.Index = Convert.ToUInt32(i); + rowItem.AppendChild(new MemberPropertyIndex() { Val = i }); + columnItems.AppendChild(rowItem); + } } - else + + if (columnFields.Any()) { columnFields.Count = Convert.ToUInt32(columnFields.Count()); pivotTableDefinition.AppendChild(columnFields); + } + if (columnItems.Any()) + { columnItems.Count = Convert.ToUInt32(columnItems.Count()); pivotTableDefinition.AppendChild(columnItems); } diff --git a/ClosedXML_Examples/PivotTables/PivotTables.cs b/ClosedXML_Examples/PivotTables/PivotTables.cs index 9daca5a..5678228 100644 --- a/ClosedXML_Examples/PivotTables/PivotTables.cs +++ b/ClosedXML_Examples/PivotTables/PivotTables.cs @@ -55,13 +55,16 @@ var header = sheet.Range(1, 1, 1, 3); var dataRange = sheet.Range(header.FirstCell(), range.LastCell()); + IXLWorksheet ptSheet; + IXLPivotTable pt; + for (int i = 1; i <= 3; i++) { // Add a new sheet for our pivot table - var ptSheet = wb.Worksheets.Add("PivotTable" + i); + ptSheet = wb.Worksheets.Add("PivotTable" + i); // Create the pivot table, using the data from the "PastrySalesData" table - var pt = ptSheet.PivotTables.AddNew("PivotTable", ptSheet.Cell(1, 1), dataRange); + pt = ptSheet.PivotTables.AddNew("PivotTable", ptSheet.Cell(1, 1), dataRange); // The rows in our pivot table will be the names of the pastries pt.RowLabels.Add("Name"); @@ -90,6 +93,16 @@ ptSheet.Columns().AdjustToContents(); } + // Different kind of pivot + ptSheet = wb.Worksheets.Add("PivotTableNoColumnLabels"); + pt = ptSheet.PivotTables.AddNew("PivotTableNoColumnLabels", ptSheet.Cell(1, 1), dataRange); + + pt.RowLabels.Add("Name"); + pt.RowLabels.Add("Month"); + + pt.Values.Add("NumberOfOrders").SetSummaryFormula(XLPivotSummary.Sum);//.NumberFormat.Format = "#0.00"; + pt.Values.Add("Quality").SetSummaryFormula(XLPivotSummary.Sum);//.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 eeb4cca..c4025e5 100644 --- a/ClosedXML_Tests/Resource/Examples/PivotTables/PivotTables.xlsx +++ b/ClosedXML_Tests/Resource/Examples/PivotTables/PivotTables.xlsx Binary files differ