diff --git a/ClosedXML/Excel/PivotTables/PivotValues/IXLPivotValueFormat.cs b/ClosedXML/Excel/PivotTables/PivotValues/IXLPivotValueFormat.cs index c0988d5..3c6391c 100644 --- a/ClosedXML/Excel/PivotTables/PivotValues/IXLPivotValueFormat.cs +++ b/ClosedXML/Excel/PivotTables/PivotValues/IXLPivotValueFormat.cs @@ -2,7 +2,7 @@ namespace ClosedXML.Excel { - public interface IXLPivotValueFormat : IXLNumberFormatBase + public interface IXLPivotValueFormat : IXLNumberFormatBase, IEquatable { IXLPivotValue SetNumberFormatId(Int32 value); diff --git a/ClosedXML/Excel/PivotTables/PivotValues/XLPivotValueFormat.cs b/ClosedXML/Excel/PivotTables/PivotValues/XLPivotValueFormat.cs index 819232e..7e5b918 100644 --- a/ClosedXML/Excel/PivotTables/PivotValues/XLPivotValueFormat.cs +++ b/ClosedXML/Excel/PivotTables/PivotValues/XLPivotValueFormat.cs @@ -79,19 +79,17 @@ } #region Overrides - public bool Equals(IXLPivotValueFormat other) + public bool Equals(IXLNumberFormatBase other) { - var otherNf = other as XLPivotValueFormat; - return - _numberFormatId == otherNf._numberFormatId - && _format == otherNf._format + _numberFormatId == other.NumberFormatId + && _format == other.Format ; } public override bool Equals(object obj) { - return Equals((XLPivotValueFormat)obj); + return Equals((IXLNumberFormatBase)obj); } public override int GetHashCode() diff --git a/ClosedXML/Excel/Style/IXLNumberFormat.cs b/ClosedXML/Excel/Style/IXLNumberFormat.cs index ac12786..2a8ef5e 100644 --- a/ClosedXML/Excel/Style/IXLNumberFormat.cs +++ b/ClosedXML/Excel/Style/IXLNumberFormat.cs @@ -2,7 +2,7 @@ namespace ClosedXML.Excel { - public interface IXLNumberFormat : IXLNumberFormatBase, IEquatable + public interface IXLNumberFormat : IXLNumberFormatBase, IEquatable { IXLStyle SetNumberFormatId(Int32 value); diff --git a/ClosedXML/Excel/Style/XLNumberFormat.cs b/ClosedXML/Excel/Style/XLNumberFormat.cs index 1b6620a..7a15cc7 100644 --- a/ClosedXML/Excel/Style/XLNumberFormat.cs +++ b/ClosedXML/Excel/Style/XLNumberFormat.cs @@ -6,13 +6,11 @@ { #region IXLNumberFormat Members - public bool Equals(IXLNumberFormat other) + public bool Equals(IXLNumberFormatBase other) { - var otherNf = other as XLNumberFormat; - return - _numberFormatId == otherNf._numberFormatId - && _format == otherNf._format + _numberFormatId == other.NumberFormatId + && _format == other.Format ; } @@ -25,7 +23,7 @@ public override bool Equals(object obj) { - return Equals((XLNumberFormat)obj); + return Equals((IXLNumberFormatBase)obj); } public override int GetHashCode() @@ -114,4 +112,4 @@ #endregion } -} \ No newline at end of file +} diff --git a/ClosedXML/Excel/XLWorkbook_Save.NestedTypes.cs b/ClosedXML/Excel/XLWorkbook_Save.NestedTypes.cs index 8c5b0ce..4bd5979 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.NestedTypes.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.NestedTypes.cs @@ -16,6 +16,8 @@ [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly Dictionary _sharedStyles; [DebuggerBrowsable(DebuggerBrowsableState.Never)] + private readonly Dictionary _sharedNumberFormats; + [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly Dictionary _sharedFonts; [DebuggerBrowsable(DebuggerBrowsableState.Never)] private readonly HashSet _tableNames; @@ -27,6 +29,7 @@ { _relIdGenerator = new RelIdGenerator(); _sharedStyles = new Dictionary(); + _sharedNumberFormats = new Dictionary(); _sharedFonts = new Dictionary(); _tableNames = new HashSet(); _tableId = 0; @@ -43,6 +46,11 @@ [DebuggerStepThrough] get { return _sharedStyles; } } + public Dictionary SharedNumberFormats + { + [DebuggerStepThrough] + get { return _sharedNumberFormats; } + } public Dictionary SharedFonts { [DebuggerStepThrough] diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index 791b00c..88617f9 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -1820,7 +1820,7 @@ var pivotTablePart = worksheetPart.AddNewPart(context.RelIdGenerator.GetNext(RelType.Workbook)); - GeneratePivotTablePartContent(pivotTablePart, pt, pivotCache.CacheId); + GeneratePivotTablePartContent(pivotTablePart, pt, pivotCache.CacheId, context); pivotTablePart.AddPart(pivotTableCacheDefinitionPart, context.RelIdGenerator.GetNext(RelType.Workbook)); } @@ -1914,7 +1914,7 @@ } // Generates content of pivotTablePart - private static void GeneratePivotTablePartContent(PivotTablePart pivotTablePart, IXLPivotTable pt, uint cacheId) + private static void GeneratePivotTablePartContent(PivotTablePart pivotTablePart, IXLPivotTable pt, uint cacheId, SaveContext context) { var pivotTableDefinition = new PivotTableDefinition { @@ -2020,14 +2020,17 @@ } } - // -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)) + if (pt.Values.Count() > 1) { - pivotTableDefinition.DataOnRows = true; - rowFields.AppendChild(new Field { Index = -2 }); + // -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)) + { + pivotTableDefinition.DataOnRows = true; + rowFields.AppendChild(new Field { Index = -2 }); + } } foreach (var xlpf in pt.Fields) @@ -2174,13 +2177,19 @@ pt.SourceRange.Columns().FirstOrDefault(c => c.Cell(1).Value.ToString() == value.SourceName); if (sourceColumn == null) continue; + UInt32 numberFormatId = 0; + if (value.NumberFormat.NumberFormatId != -1 || context.SharedNumberFormats.ContainsKey(value.NumberFormat.NumberFormatId)) + numberFormatId = (UInt32)value.NumberFormat.NumberFormatId; + else if (context.SharedNumberFormats.Any(snf => snf.Value.NumberFormat.Format == value.NumberFormat.Format)) + numberFormatId = (UInt32)context.SharedNumberFormats.First(snf => snf.Value.NumberFormat.Format == value.NumberFormat.Format).Key; + var df = new DataField { Name = value.CustomName, Field = (UInt32)sourceColumn.ColumnNumber() - 1, Subtotal = value.SummaryFormula.ToOpenXml(), ShowDataAs = value.Calculation.ToOpenXml(), - NumberFormatId = (UInt32)value.NumberFormat.NumberFormatId + NumberFormatId = numberFormatId }; if (!String.IsNullOrEmpty(value.BaseField)) @@ -2639,6 +2648,11 @@ } var allSharedNumberFormats = ResolveNumberFormats(workbookStylesPart, sharedNumberFormats, defaultFormatId); + foreach (var nf in allSharedNumberFormats) + { + context.SharedNumberFormats.Add(nf.Value.NumberFormatId, nf.Value); + } + ResolveFonts(workbookStylesPart, context); var allSharedFills = ResolveFills(workbookStylesPart, sharedFills); var allSharedBorders = ResolveBorders(workbookStylesPart, sharedBorders);