diff --git a/ClosedXML/Excel/Cells/XLCell.cs b/ClosedXML/Excel/Cells/XLCell.cs index d667470..151e47b 100644 --- a/ClosedXML/Excel/Cells/XLCell.cs +++ b/ClosedXML/Excel/Cells/XLCell.cs @@ -1478,7 +1478,6 @@ private IXLStyle GetStyle() { - //return _style ?? (_style = new XLStyle(this, Worksheet.Workbook.GetStyleById(_styleCacheId))); if (_style != null) return _style; diff --git a/ClosedXML/Excel/PivotTables/PivotValues/IXLPivotValueFormat.cs b/ClosedXML/Excel/PivotTables/PivotValues/IXLPivotValueFormat.cs index 6ccaedf..c0988d5 100644 --- a/ClosedXML/Excel/PivotTables/PivotValues/IXLPivotValueFormat.cs +++ b/ClosedXML/Excel/PivotTables/PivotValues/IXLPivotValueFormat.cs @@ -1,13 +1,11 @@ using System; -using System.Collections.Generic; -using System.Linq; -using System.Text; namespace ClosedXML.Excel { - public interface IXLPivotValueFormat: IXLNumberFormatBase + public interface IXLPivotValueFormat : IXLNumberFormatBase { IXLPivotValue SetNumberFormatId(Int32 value); + IXLPivotValue SetFormat(String value); } } diff --git a/ClosedXML/Excel/PivotTables/PivotValues/XLPivotValue.cs b/ClosedXML/Excel/PivotTables/PivotValues/XLPivotValue.cs index e3ef842..50753cb 100644 --- a/ClosedXML/Excel/PivotTables/PivotValues/XLPivotValue.cs +++ b/ClosedXML/Excel/PivotTables/PivotValues/XLPivotValue.cs @@ -14,7 +14,7 @@ } public IXLPivotValueFormat NumberFormat { get; private set; } - public String SourceName { get; private set; } + public String SourceName { get; private set; } public String CustomName { get; set; } public IXLPivotValue SetCustomName(String value) { CustomName = value; return this; } public XLPivotSummary SummaryFormula { get; set; } public IXLPivotValue SetSummaryFormula(XLPivotSummary value) { SummaryFormula = value; return this; } diff --git a/ClosedXML/Excel/Style/IXLNumberFormat.cs b/ClosedXML/Excel/Style/IXLNumberFormat.cs index 1490413..ac12786 100644 --- a/ClosedXML/Excel/Style/IXLNumberFormat.cs +++ b/ClosedXML/Excel/Style/IXLNumberFormat.cs @@ -2,9 +2,10 @@ namespace ClosedXML.Excel { - public interface IXLNumberFormat: IXLNumberFormatBase, IEquatable + public interface IXLNumberFormat : IXLNumberFormatBase, IEquatable { IXLStyle SetNumberFormatId(Int32 value); + IXLStyle SetFormat(String value); } } diff --git a/ClosedXML/Excel/XLWorkbook_Save.NestedTypes.cs b/ClosedXML/Excel/XLWorkbook_Save.NestedTypes.cs index 0f1c4ca..8c5b0ce 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.NestedTypes.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.NestedTypes.cs @@ -133,7 +133,7 @@ internal struct NumberFormatInfo { public Int32 NumberFormatId; - public IXLNumberFormat NumberFormat; + public IXLNumberFormatBase NumberFormat; } #endregion #region Nested type: StyleInfo @@ -148,8 +148,8 @@ } #endregion - - - + + + } -} \ No newline at end of file +} diff --git a/ClosedXML/Excel/XLWorkbook_Save.cs b/ClosedXML/Excel/XLWorkbook_Save.cs index e6924a3..791b00c 100644 --- a/ClosedXML/Excel/XLWorkbook_Save.cs +++ b/ClosedXML/Excel/XLWorkbook_Save.cs @@ -2522,7 +2522,7 @@ var sharedBorders = new Dictionary {{defaultStyle.Border, new BorderInfo {BorderId = 0, Border = defaultStyle.Border as XLBorder}}}; - var sharedNumberFormats = new Dictionary + var sharedNumberFormats = new Dictionary { { defaultStyle.NumberFormat, @@ -2573,6 +2573,7 @@ UInt32 borderCount = 1; var numberFormatCount = 1; var xlStyles = new HashSet(); + var pivotTableNumberFormats = new HashSet(); foreach (var worksheet in WorksheetsInternal) { @@ -2591,6 +2592,24 @@ s => !xlStyles.Contains(s)) ) xlStyles.Add(s); + + foreach (var ptnf in worksheet.PivotTables.SelectMany(pt => pt.Values.Select(ptv => ptv.NumberFormat)).Distinct().Where(nf => !pivotTableNumberFormats.Contains(nf))) + pivotTableNumberFormats.Add(ptnf); + } + + foreach (var numberFormat in pivotTableNumberFormats) + { + if (numberFormat.NumberFormatId != -1 + || sharedNumberFormats.ContainsKey(numberFormat)) + continue; + + sharedNumberFormats.Add(numberFormat, + new NumberFormatInfo + { + NumberFormatId = numberFormatCount + 164, + NumberFormat = numberFormat + }); + numberFormatCount++; } foreach (var xlStyle in xlStyles.Select(GetStyleById)) @@ -3317,9 +3336,9 @@ return nf.Equals(xlFont); } - private static Dictionary ResolveNumberFormats( + private static Dictionary ResolveNumberFormats( WorkbookStylesPart workbookStylesPart, - Dictionary sharedNumberFormats, + Dictionary sharedNumberFormats, UInt32 defaultFormatId) { if (workbookStylesPart.Stylesheet.NumberingFormats == null) @@ -3332,7 +3351,7 @@ }); } - var allSharedNumberFormats = new Dictionary(); + var allSharedNumberFormats = new Dictionary(); foreach (var numberFormatInfo in sharedNumberFormats.Values.Where(nf => nf.NumberFormatId != defaultFormatId)) { var numberingFormatId = 164; @@ -3368,7 +3387,7 @@ return allSharedNumberFormats; } - private static bool NumberFormatsAreEqual(NumberingFormat nf, IXLNumberFormat xlNumberFormat) + private static bool NumberFormatsAreEqual(NumberingFormat nf, IXLNumberFormatBase xlNumberFormat) { var newXLNumberFormat = new XLNumberFormat();