Skip to content
This repository was archived by the owner on Aug 26, 2022. It is now read-only.

Use standard built-it style formats #37

Merged
merged 1 commit into from
Apr 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
56 changes: 29 additions & 27 deletions src/Simplexcel.TestApp/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -71,33 +71,35 @@ static void Main()
sheet.Cells[0, 6] = "👪";
sheet.Cells[0, 7] = "👨‍👩‍👧‍👦";

sheet.Cells["D4"] = DateTime.Now;
sheet.Cells["D5"] = new Cell(CellType.Date, DateTime.Now, BuiltInCellFormat.DateOnly);
sheet.Cells["D6"] = new Cell(CellType.Date, DateTime.Now, BuiltInCellFormat.TimeOnly);
sheet.Cells["D7"] = long.MaxValue;
sheet.Cells["D8"] = long.MinValue;
sheet.Cells["D9"] = decimal.MaxValue;
sheet.Cells["D10"] = decimal.MinValue;

sheet.Cells["D11"] = 9999999999L;
sheet.Cells["D12"] = 99999999999L;
sheet.Cells["D13"] = 100000000000L;
sheet.Cells["D14"] = 100000000001L;
sheet.Cells["D15"] = 1000000000000L;
sheet.Cells["D16"] = 1000000000001L;
sheet.Cells["D17"] = Cell.LargeNumberPositiveLimit;
sheet.Cells["D18"] = Cell.LargeNumberPositiveLimit + 1;
sheet.Cells["D19"] = Cell.LargeNumberPositiveLimit - 1;

sheet.Cells["D20"] = -9999999999L;
sheet.Cells["D21"] = -99999999999L;
sheet.Cells["D22"] = -100000000000L;
sheet.Cells["D23"] = -100000000001L;
sheet.Cells["D24"] = -1000000000000L;
sheet.Cells["D25"] = -1000000000001L;
sheet.Cells["D26"] = Cell.LargeNumberNegativeLimit;
sheet.Cells["D27"] = Cell.LargeNumberNegativeLimit + 1;
sheet.Cells["D28"] = Cell.LargeNumberNegativeLimit - 1;
var dateTime = new DateTime(2022, 4, 26, 13, 14, 15);
sheet.Cells["D4"] = dateTime;
sheet.Cells["D5"] = new Cell(CellType.Date, dateTime, BuiltInCellFormat.DateOnly);
sheet.Cells["D6"] = new Cell(CellType.Date, dateTime, BuiltInCellFormat.TimeOnly);
sheet.Cells["D7"] = new Cell(CellType.Date, dateTime, "yyyy\"_\"mm\"_\"dd\"_\"hh\"_\"mm\"_\"ss");
sheet.Cells["D8"] = long.MaxValue;
sheet.Cells["D9"] = long.MinValue;
sheet.Cells["D10"] = decimal.MaxValue;
sheet.Cells["D11"] = decimal.MinValue;

sheet.Cells["D12"] = 9999999999L;
sheet.Cells["D13"] = 99999999999L;
sheet.Cells["D14"] = 100000000000L;
sheet.Cells["D15"] = 100000000001L;
sheet.Cells["D16"] = 1000000000000L;
sheet.Cells["D17"] = 1000000000001L;
sheet.Cells["D18"] = Cell.LargeNumberPositiveLimit;
sheet.Cells["D19"] = Cell.LargeNumberPositiveLimit + 1;
sheet.Cells["D20"] = Cell.LargeNumberPositiveLimit - 1;

sheet.Cells["D21"] = -9999999999L;
sheet.Cells["D22"] = -99999999999L;
sheet.Cells["D23"] = -100000000000L;
sheet.Cells["D24"] = -100000000001L;
sheet.Cells["D25"] = -1000000000000L;
sheet.Cells["D26"] = -1000000000001L;
sheet.Cells["D27"] = Cell.LargeNumberNegativeLimit;
sheet.Cells["D28"] = Cell.LargeNumberNegativeLimit + 1;
sheet.Cells["D29"] = Cell.LargeNumberNegativeLimit - 1;
sheet.LargeNumberHandlingMode = LargeNumberHandlingMode.StoreAsText;

sheet.Cells["C5"] = "ThinDiagonalCrosshatch";
Expand Down
8 changes: 4 additions & 4 deletions src/Simplexcel/Cells/BuiltInCellFormat.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,14 @@ public static class BuiltInCellFormat
public const string Text = "@";

/// <summary>
/// m/d/yyyy h:mm
/// m/d/yy h:mm
/// </summary>
public const string DateAndTime = "m/d/yyyy h:mm";
public const string DateAndTime = "m/d/yy h:mm";

/// <summary>
/// m/d/yyyy
/// mm-dd-yy
/// </summary>
public const string DateOnly = "m/d/yyyy";
public const string DateOnly = "mm-dd-yy";

/// <summary>
/// h:mm
Expand Down
39 changes: 37 additions & 2 deletions src/Simplexcel/XlsxInternal/StyleWriter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,41 @@ internal static class StyleWriter
// There are up to 164 built in number formats (0-163), all else are custom (164 and above)
private const int CustomFormatIndex = 164;

/// <summary>
/// Standard format codes as defined in ECMA-376, 3rd Edition, Part 1, 18.8.30 numFmt (Number Format)
/// </summary>
private static Dictionary<string, int> StandardFormatIds = new Dictionary<string, int>
{
["General"] = 0,
["0"] = 1,
["0.00"] = 2,
["#,##0"] = 3,
["#,##0.00"] = 4,
["0%"] = 9,
["0.00%"] = 10,
["0.00E+00"] = 11,
["# ?/?"] = 12,
["# ??/??"] = 13,
["mm-dd-yy"] = 14,
["d-mmm-yy"] = 15,
["d-mmm"] = 16,
["mmm-yy"] = 17,
["h:mm AM/PM"] = 18,
["h:mm:ss AM/PM"] = 19,
["h:mm"] = 20,
["h:mm:ss"] = 21,
["m/d/yy h:mm"] = 22,
["#,##0 ;(#,##0)"] = 37,
["#,##0 ;[Red](#,##0)"] = 38,
["#,##0.00;(#,##0.00)"] = 39,
["#,##0.00;[Red](#,##0.00)"] = 40,
["mm:ss"] = 45,
["[h]:mm:ss"] = 46,
["mmss.0"] = 47,
["##0.0E+0"] = 48,
["@"] = 49,
};

/// <summary>
/// Create a styles.xml file
/// </summary>
Expand All @@ -29,7 +64,7 @@ internal static XmlFile CreateStyleXml(IList<XlsxCellStyle> styles)
uniqueBorders.Add(style.Border);
}

if (!numberFormats.Contains(style.Format))
if (!numberFormats.Contains(style.Format) && !StandardFormatIds.ContainsKey(style.Format))
{
numberFormats.Add(style.Format);
}
Expand Down Expand Up @@ -79,7 +114,7 @@ private static void StyleAddCellXfsElement(XDocument doc, IList<XlsxCellStyle> s

foreach (var style in styles)
{
var numFmtId = numberFormats.IndexOf(style.Format) + CustomFormatIndex;
var numFmtId = StandardFormatIds.TryGetValue(style.Format, out var standardNumFmtId) ? standardNumFmtId : numberFormats.IndexOf(style.Format) + CustomFormatIndex;
var fontId = fontInfos.IndexOf(style.Font);
var fillId = fills.IndexOf(style.Fill);
var borderId = uniqueBorders.IndexOf(style.Border);
Expand Down