diff --git a/ClosedXML/Excel/PivotTables/IXLPivotFields.cs b/ClosedXML/Excel/PivotTables/IXLPivotFields.cs index 08c5312..df5a497 100644 --- a/ClosedXML/Excel/PivotTables/IXLPivotFields.cs +++ b/ClosedXML/Excel/PivotTables/IXLPivotFields.cs @@ -1,17 +1,22 @@ using System; using System.Collections.Generic; -using System.Linq; -using System.Text; namespace ClosedXML.Excel { - public interface IXLPivotFields: IEnumerable + public interface IXLPivotFields : IEnumerable { IXLPivotField Add(String sourceName); - IXLPivotField Add(String sourceName, String customName); - void Clear(); - void Remove(String sourceName); - int IndexOf(IXLPivotField pf); + IXLPivotField Add(String sourceName, String customName); + + void Clear(); + + Boolean Contains(String sourceName); + + IXLPivotField Get(String sourceName); + + Int32 IndexOf(IXLPivotField pf); + + void Remove(String sourceName); } } diff --git a/ClosedXML/Excel/PivotTables/XLPivotFields.cs b/ClosedXML/Excel/PivotTables/XLPivotFields.cs index c1a52f2..19accb7 100644 --- a/ClosedXML/Excel/PivotTables/XLPivotFields.cs +++ b/ClosedXML/Excel/PivotTables/XLPivotFields.cs @@ -1,13 +1,40 @@ using System; using System.Collections.Generic; using System.Linq; -using System.Text; namespace ClosedXML.Excel { - public class XLPivotFields: IXLPivotFields + public class XLPivotFields : IXLPivotFields { private readonly Dictionary _pivotFields = new Dictionary(); + + public IXLPivotField Add(String sourceName) + { + return Add(sourceName, sourceName); + } + + public IXLPivotField Add(String sourceName, String customName) + { + var pivotField = new XLPivotField(sourceName) { CustomName = customName }; + _pivotFields.Add(sourceName, pivotField); + return pivotField; + } + + public void Clear() + { + _pivotFields.Clear(); + } + + public Boolean Contains(String sourceName) + { + return _pivotFields.ContainsKey(sourceName); + } + + public IXLPivotField Get(string sourceName) + { + return _pivotFields[sourceName]; + } + public IEnumerator GetEnumerator() { return _pivotFields.Values.GetEnumerator(); @@ -18,32 +45,17 @@ return GetEnumerator(); } - public IXLPivotField Add(String sourceName) + public Int32 IndexOf(IXLPivotField pf) { - return Add(sourceName, sourceName); - } - public IXLPivotField Add(String sourceName, String customName) - { - var pivotField = new XLPivotField(sourceName) {CustomName = customName}; - _pivotFields.Add(sourceName, pivotField); - return pivotField; - } - - public void Clear() - { - _pivotFields.Clear(); - } - public void Remove(String sourceName) - { - _pivotFields.Remove(sourceName); - } - - public int IndexOf(IXLPivotField pf) - { - var selectedItem = _pivotFields.Select((item, index) => new {Item = item, Position = index}).FirstOrDefault(i => i.Item.Key == pf.SourceName); + var selectedItem = _pivotFields.Select((item, index) => new { Item = item, Position = index }).FirstOrDefault(i => i.Item.Key == pf.SourceName); if (selectedItem == null) throw new IndexOutOfRangeException("Invalid field name."); return selectedItem.Position; } + + public void Remove(String sourceName) + { + _pivotFields.Remove(sourceName); + } } } diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index 57edc21..91f8e8c 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -146,7 +146,6 @@ if (worksheet == null) return; - string sheetName = worksheet.Name; // Get the pivot Table Parts IEnumerable pvtTableCacheParts = wbPart.PivotTableCacheDefinitionParts; @@ -204,11 +203,9 @@ foreach (CalculationCell Item in calChainEntries) calcsToDelete.Add(Item); - foreach (CalculationCell Item in calcsToDelete) Item.Remove(); - if (!calChainPart.CalculationChain.Any()) wbPart.DeletePart(calChainPart); } @@ -1964,7 +1961,11 @@ { var columnNumber = c.ColumnNumber(); var columnName = c.FirstCell().Value.ToString(); - var xlpf = pt.Fields.Add(columnName); + IXLPivotField xlpf; + if (pt.Fields.Contains(columnName)) + xlpf = pt.Fields.Get(columnName); + else + xlpf = pt.Fields.Add(columnName); var field = pt.RowLabels.Union(pt.ColumnLabels).Union(pt.ReportFilters).FirstOrDefault(f => f.SourceName == columnName); @@ -2150,10 +2151,12 @@ case XLPivotSubtotals.DoNotShow: pf.DefaultSubtotal = false; break; + case XLPivotSubtotals.AtBottom: pf.DefaultSubtotal = true; pf.SubtotalTop = false; break; + case XLPivotSubtotals.AtTop: pf.DefaultSubtotal = true; pf.SubtotalTop = true;