diff --git a/ExcelMapper.Tests/Tests.cs b/ExcelMapper.Tests/Tests.cs index 13c3ead..9f162ed 100644 --- a/ExcelMapper.Tests/Tests.cs +++ b/ExcelMapper.Tests/Tests.cs @@ -3197,7 +3197,7 @@ record Stato [Test] public void StatoTest() { - var excel = new ExcelMapper { CreateMissingHeaders = true, HeaderRow = true }; + var excel = new ExcelMapper { CreateMissingHeaders = true, HeaderRow = true }; var statos = new Stato[] { @@ -3210,5 +3210,22 @@ public void StatoTest() AssertEquivalent(statos, statos2); } + + record IdNumber + { + [DataFormat(1)] + public string IdNo { get; set; } + } + + [Test] + public void FormatTest() + { + var rows = new ExcelMapper(@"../../../xlsx/Format.xlsx").Fetch().ToList(); + AssertEquivalent(rows, new[] + { + new IdNumber { IdNo = "8005065137086" }, + new IdNumber { IdNo = "8402155792088" } + }); + } } } diff --git a/ExcelMapper.Tests/xlsx/Format.xlsx b/ExcelMapper.Tests/xlsx/Format.xlsx new file mode 100644 index 0000000..856ba2d Binary files /dev/null and b/ExcelMapper.Tests/xlsx/Format.xlsx differ diff --git a/ExcelMapper/ExcelMapper.cs b/ExcelMapper/ExcelMapper.cs index 7b067af..cbc7259 100644 --- a/ExcelMapper/ExcelMapper.cs +++ b/ExcelMapper/ExcelMapper.cs @@ -1495,7 +1495,18 @@ object GetCellValue(ICell cell, ColumnInfo targetColumn) case CellType.Numeric: if (!formulaResult && targetColumn.PropertyType == typeof(string)) { - return DataFormatter.FormatCellValue(cell); + if (targetColumn.BuiltinFormat != 0 || targetColumn.CustomFormat != null) + { + var formatIndex = targetColumn.BuiltinFormat != 0 ? targetColumn.BuiltinFormat : cell.CellStyle.DataFormat; + var formatString = targetColumn.BuiltinFormat != 0 ? BuiltinFormats.GetBuiltinFormat(targetColumn.BuiltinFormat) + : targetColumn.CustomFormat ?? cell.CellStyle.GetDataFormatString(); + var formattedValue = DataFormatter.FormatRawCellContents(cell.NumericCellValue, formatIndex, formatString); + return formattedValue; + } + else + { + return DataFormatter.FormatCellValue(cell); + } } else if (cell.NumericCellValue <= maxDate && DateUtil.IsCellDateFormatted(cell)) {