diff --git a/ClosedXML/Excel/PivotTables/IXLPivotField.cs b/ClosedXML/Excel/PivotTables/IXLPivotField.cs index 20b3ef4..cc6fe91 100644 --- a/ClosedXML/Excel/PivotTables/IXLPivotField.cs +++ b/ClosedXML/Excel/PivotTables/IXLPivotField.cs @@ -35,6 +35,7 @@ Boolean ShowBlankItems { get; set; } Boolean InsertPageBreaks { get; set; } Boolean Collapsed { get; set; } + XLPivotSortType SortType { get; set; } IXLPivotField SetCustomName(String value); @@ -48,6 +49,7 @@ IXLPivotField SetShowBlankItems(); IXLPivotField SetShowBlankItems(Boolean value); IXLPivotField SetInsertPageBreaks(); IXLPivotField SetInsertPageBreaks(Boolean value); IXLPivotField SetCollapsed(); IXLPivotField SetCollapsed(Boolean value); + IXLPivotField SetSort(XLPivotSortType value); IList SelectedValues { get; } IXLPivotField AddSelectedValue(Object value); diff --git a/ClosedXML/Excel/PivotTables/IXLPivotTable.cs b/ClosedXML/Excel/PivotTables/IXLPivotTable.cs index 027dd94..1b2d171 100644 --- a/ClosedXML/Excel/PivotTables/IXLPivotTable.cs +++ b/ClosedXML/Excel/PivotTables/IXLPivotTable.cs @@ -91,6 +91,13 @@ PivotStyleMedium9 } + public enum XLPivotSortType + { + Manual = 0, + Ascending = 1, + Descending = 2 + } + public enum XLPivotSubtotals { DoNotShow, diff --git a/ClosedXML/Excel/PivotTables/XLPivotField.cs b/ClosedXML/Excel/PivotTables/XLPivotField.cs index acf0b1c..920759d 100644 --- a/ClosedXML/Excel/PivotTables/XLPivotField.cs +++ b/ClosedXML/Excel/PivotTables/XLPivotField.cs @@ -12,6 +12,7 @@ SourceName = sourceName; Subtotals = new List(); SelectedValues = new List(); + SortType = XLPivotSortType.Manual; } public String SourceName { get; private set; } @@ -69,6 +70,10 @@ public IXLPivotField SetCollapsed(Boolean value) { Collapsed = value; return this; } + public XLPivotSortType SortType { get; set; } + + public IXLPivotField SetSort(XLPivotSortType value) { SortType = value; return this; } + public IList SelectedValues { get; private set; } public IXLPivotField AddSelectedValue(Object value) { diff --git a/ClosedXML/Excel/XLWorkbook_Load.cs b/ClosedXML/Excel/XLWorkbook_Load.cs index 75f9cee..1ff2e90 100644 --- a/ClosedXML/Excel/XLWorkbook_Load.cs +++ b/ClosedXML/Excel/XLWorkbook_Load.cs @@ -557,6 +557,11 @@ var items = pf.Items.OfType().Where(i => i.Index != null && i.Index.HasValue); if (!items.Any(i => i.HideDetails == null || BooleanValue.ToBoolean(i.HideDetails))) pivotField.SetCollapsed(); + + if (pf.SortType != null) + { + pivotField.SetSort((XLPivotSortType)pf.SortType.Value); + } } } } @@ -590,6 +595,11 @@ var items = pf.Items.OfType().Where(i => i.Index != null && i.Index.HasValue); if (!items.Any(i => i.HideDetails == null || BooleanValue.ToBoolean(i.HideDetails))) pivotField.SetCollapsed(); + + if (pf.SortType != null) + { + pivotField.SetSort((XLPivotSortType)pf.SortType.Value); + } } } } diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index a0e3fdc..0c4ddfc 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -2020,6 +2020,7 @@ if (field != null) { xlpf.CustomName = field.CustomName; + xlpf.SortType = field.SortType; xlpf.Subtotals.AddRange(field.Subtotals); } @@ -2295,6 +2296,11 @@ pf.Compact = false; } + if (xlpf.SortType != XLPivotSortType.Manual) + { + pf.SortType = new EnumValue((FieldSortValues)xlpf.SortType); + } + switch (pt.Subtotals) { case XLPivotSubtotals.DoNotShow: