diff --git a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs index 26c40eb..cebe6ec 100644 --- a/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/ClosedXML/ClosedXML/Excel/XLWorkbook_Save.cs @@ -1856,16 +1856,11 @@ var pageFields = new PageFields {Count = (uint)pt.ReportFilters.Count()}; var pivotFields = new PivotFields {Count = Convert.ToUInt32(pt.SourceRange.ColumnCount())}; - foreach (var xlpf in pt.Fields) + foreach (var xlpf in pt.Fields.OrderBy(f => pt.RowLabels.Any(p => p.SourceName == f.SourceName) ? pt.RowLabels.IndexOf(f) : Int32.MaxValue )) { - var pf = new PivotField {ShowAll = false, Name = xlpf.CustomName}; - - if (pt.RowLabels.FirstOrDefault(p => p.SourceName == xlpf.SourceName) != null) { - pf.Axis = PivotTableAxisValues.AxisRow; - - var f = new Field { Index = pt.RowLabels.IndexOf(xlpf) }; + var f = new Field {Index = pt.Fields.IndexOf(xlpf)}; rowFields.AppendChild(f); for (var i = 0; i < xlpf.SharedStrings.Count; i++) @@ -1881,9 +1876,7 @@ } else if (pt.ColumnLabels.FirstOrDefault(p => p.SourceName == xlpf.SourceName) != null) { - pf.Axis = PivotTableAxisValues.AxisColumn; - - var f = new Field { Index = pt.ColumnLabels.IndexOf(xlpf) }; + var f = new Field {Index = pt.Fields.IndexOf(xlpf)}; columnFields.AppendChild(f); for (var i = 0; i < xlpf.SharedStrings.Count; i++) @@ -1897,6 +1890,20 @@ rowItemTotal.AppendChild(new MemberPropertyIndex()); columnItems.AppendChild(rowItemTotal); } + } + + foreach (var xlpf in pt.Fields) + { + var pf = new PivotField {ShowAll = false, Name = xlpf.CustomName}; + + if (pt.RowLabels.FirstOrDefault(p => p.SourceName == xlpf.SourceName) != null) + { + pf.Axis = PivotTableAxisValues.AxisRow; + } + else if (pt.ColumnLabels.FirstOrDefault(p => p.SourceName == xlpf.SourceName) != null) + { + pf.Axis = PivotTableAxisValues.AxisColumn; + } else if (pt.ReportFilters.FirstOrDefault(p => p.SourceName == xlpf.SourceName) != null) { location.ColumnsPerPage = 1; diff --git a/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs b/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs index 7f73d0d..7f57f17 100644 --- a/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs +++ b/ClosedXML/ClosedXML/ClosedXML_Sandbox/Program.cs @@ -16,23 +16,47 @@ private static void Main(string[] args) { var wb = new XLWorkbook(); - var ws = wb.AddWorksheet("Sheet1"); - ws.FirstCell().SetValue("Hello") - .CellBelow().SetValue("Hellos") - .CellBelow().SetValue("Hell") - .CellBelow().SetValue("Holl"); + var sheet = wb.Worksheets.Add("orderlines"); - ws.RangeUsed().AddConditionalFormat().WhenStartsWith("Hell") - .Fill.SetBackgroundColor(XLColor.Red) - .Border.SetOutsideBorder(XLBorderStyleValues.Thick) - .Border.SetOutsideBorderColor(XLColor.Blue) - .Font.SetBold(); + int row = 1; + int column = 1; + sheet.Cell(row, column++).Value = "Date"; + sheet.Cell(row, column++).Value = "Quantity"; + sheet.Cell(row, column++).Value = "Category"; + sheet.Cell(row, column++).Value = "Item"; + sheet.Cell(row, column++).Value = "Unit price"; + sheet.Cell(row, column++).Value = "Total price"; + + // Sample data row + row++; + column = 1; + sheet.Cell(row, column++).Value = new DateTime(2014, 6, 21); + sheet.Cell(row, column++).Value = 1; + sheet.Cell(row, column++).Value = "Widgets"; + sheet.Cell(row, column++).Value = "Pro widget"; + sheet.Cell(row, column++).Value = "1.23"; + sheet.Cell(row, column++).Value = "1.23"; + + var dataRange = sheet.RangeUsed(); + + // Add a new sheet for our pivot table + var pivotTableSheet = wb.Worksheets.Add("PivotTable"); + + // Create the pivot table, using the data from the "PastrySalesData" table + var pt = pivotTableSheet.PivotTables.AddNew("PivotTable", pivotTableSheet.Cell(1, 1), dataRange); + + // The rows in our pivot table will be the names of the categories + pt.RowLabels.Add("Item"); + pt.RowLabels.Add("Category"); - wb.SaveAs(@"c:\temp\saved.xlsx"); + + pt.Values.Add("Total price"); + + wb.SaveAs(@"c:\temp\saved3.xlsx"); Console.WriteLine("Done"); - Console.ReadLine(); + //Console.ReadLine(); } } }