Skip to content
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
59 changes: 37 additions & 22 deletions src/NetCore.Utilities.Spreadsheet/OpenXmlSpreadsheetGenerator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -314,16 +314,22 @@ private static SheetData CreateExportSheet(ISpreadsheetConfiguration exportConfi
var itemValue = prop.Descriptor.GetValue(item);
var dataCell = CellFromValue(prop.Descriptor.PropertyType, itemValue);

dataCell.StyleIndex = prop.Format switch
if (!string.IsNullOrEmpty(prop.Format))
{
ColumnFormats.Currency => (int)FontStyleIndex.NormalCurrency,
ColumnFormats.Date => (int)FontStyleIndex.NormalDate,
ColumnFormats.Fixed0 => (int)FontStyleIndex.Fixed0,
ColumnFormats.Fixed1 => (int)FontStyleIndex.Fixed1,
ColumnFormats.Fixed2 => (int)FontStyleIndex.Fixed2,
ColumnFormats.Fixed3 => (int)FontStyleIndex.Fixed3,
_ => dataCell.StyleIndex
};
dataCell.StyleIndex = prop.Format.ToLowerInvariant() switch
{
ColumnFormats.Currency => (int)FontStyleIndex.NormalCurrency,
ColumnFormats.Date => (int)FontStyleIndex.NormalDate,
ColumnFormats.Fixed0 => (int)FontStyleIndex.Fixed0,
ColumnFormats.Fixed1 => (int)FontStyleIndex.Fixed1,
ColumnFormats.Fixed2 => (int)FontStyleIndex.Fixed2,
ColumnFormats.Fixed3 => (int)FontStyleIndex.Fixed3,
ColumnFormats.Currency3 => (int)FontStyleIndex.Currency3,
ColumnFormats.Currency4 => (int)FontStyleIndex.Currency4,
_ => dataCell.StyleIndex
};
}

outputMap[prop].Cells.Add(dataCell);
dataRow.Append(dataCell);
}
Expand All @@ -347,18 +353,23 @@ private static SheetData CreateExportSheet(ISpreadsheetConfiguration exportConfi
CellFormula = new CellFormula($"{prop.Formula}({GetCellReferenceByRowAndColumn(dataRowIndex, prop.Order)}:{GetCellReferenceByRowAndColumn(currentRow - 1, prop.Order)})")
};

//Match the formatting of the column for this
dataCell.StyleIndex = prop.Format switch
if (!string.IsNullOrEmpty(prop.Format))
{
ColumnFormats.Currency => (int)FontStyleIndex.NormalCurrency,
ColumnFormats.Date => (int)FontStyleIndex.NormalDate,
ColumnFormats.Fixed0 => (int)FontStyleIndex.Fixed0,
ColumnFormats.Fixed1 => (int)FontStyleIndex.Fixed1,
ColumnFormats.Fixed2 => (int)FontStyleIndex.Fixed2,
ColumnFormats.Fixed3 => (int)FontStyleIndex.Fixed3,
_ => dataCell.StyleIndex
};

//Match formatting
dataCell.StyleIndex = prop.Format.ToLowerInvariant() switch
{
ColumnFormats.Currency => (int)FontStyleIndex.NormalCurrency,
ColumnFormats.Date => (int)FontStyleIndex.NormalDate,
ColumnFormats.Fixed0 => (int)FontStyleIndex.Fixed0,
ColumnFormats.Fixed1 => (int)FontStyleIndex.Fixed1,
ColumnFormats.Fixed2 => (int)FontStyleIndex.Fixed2,
ColumnFormats.Fixed3 => (int)FontStyleIndex.Fixed3,
ColumnFormats.Currency3 => (int)FontStyleIndex.Currency3,
ColumnFormats.Currency4 => (int)FontStyleIndex.Currency4,
_ => dataCell.StyleIndex
};
}

outputMap[prop].Cells.Add(dataCell);
dataRow.Append(dataCell);
}
Expand Down Expand Up @@ -432,7 +443,9 @@ private enum FontStyleIndex
Fixed0 = 7,
Fixed1=8,
Fixed2=9,
Fixed3=10
Fixed3=10,
Currency3=11,
Currency4=12,
}

/// <summary>
Expand Down Expand Up @@ -488,7 +501,9 @@ private static Stylesheet CreateStylesheet()
new NumberingFormat { NumberFormatId = 301, FormatCode = "0" },
new NumberingFormat { NumberFormatId = 302, FormatCode = "0.0" },
new NumberingFormat { NumberFormatId = 303, FormatCode = "0.00" },
new NumberingFormat { NumberFormatId = 304, FormatCode = "0.000" }
new NumberingFormat { NumberFormatId = 304, FormatCode = "0.000" },
new NumberingFormat { NumberFormatId = 401, FormatCode = "\"$\"#,##0.000" },
new NumberingFormat { NumberFormatId = 402, FormatCode = "\"$\"#,##0.0000" }
);

styles.CellFormats = new CellFormats(
Expand Down
10 changes: 10 additions & 0 deletions src/NetCore.Utilities.Spreadsheet/SpreadsheetColumnAttribute.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,16 @@ public static class ColumnFormats
/// </summary>
public const string Currency = "c";

/// <summary>
/// Formats as a currency layout with 3 decimal places
/// </summary>
public const string Currency3 = "c3";

/// <summary>
/// Formats as a currently layout with 4 decimal places
/// </summary>
public const string Currency4 = "c4";

/// <summary>
/// Formats the column as a number with no decimal places
/// </summary>
Expand Down
Loading