|
1 | 1 | using System; |
2 | 2 | using System.Collections.Generic; |
| 3 | +using System.Globalization; |
3 | 4 | using System.IO; |
4 | 5 | using System.Linq; |
5 | 6 | using System.Threading; |
@@ -308,23 +309,40 @@ private static Cell CreateCell(object value) |
308 | 309 | { |
309 | 310 | return value switch |
310 | 311 | { |
311 | | - string s => new Cell(s), |
312 | | - bool b => new Cell(b), |
313 | | - int i => new Cell(i), |
314 | | - long l => new Cell(l), |
315 | | - short sh => new Cell(sh), |
316 | | - byte b8 => new Cell(b8), |
317 | | - uint ui => new Cell((double)ui), |
318 | | - ulong ul => new Cell((double)ul), |
319 | | - float f => new Cell((double)f), |
320 | | - double d => new Cell(d), |
321 | | - decimal m => new Cell((double)m), |
322 | | - DateTime dt => new Cell(dt), |
323 | | - DateOnly d => new Cell(d.ToDateTime(TimeOnly.MinValue)), |
324 | | - TimeOnly t => new Cell(DateTime.Today.Add(t.ToTimeSpan())), |
| 312 | + string s => new Cell(s), |
| 313 | + bool b => new Cell(b), |
| 314 | + int i => new Cell(i), |
| 315 | + long l => new Cell(l), |
| 316 | + short sh => new Cell(sh), |
| 317 | + byte b8 => new Cell(b8), |
| 318 | + uint ui => new Cell((double)ui), |
| 319 | + ulong ul => new Cell((double)ul), |
| 320 | + float f => new Cell((double)f), |
| 321 | + double d => new Cell(d), |
| 322 | + decimal m => new Cell((double)m), |
| 323 | + |
| 324 | + DateTime dt => CreateDateCell(dt), |
| 325 | + DateOnly d => CreateDateCell(d.ToDateTime(TimeOnly.MinValue)), |
| 326 | + TimeOnly t => CreateDateCell(DateTime.Today.Add(t.ToTimeSpan())), |
| 327 | + |
325 | 328 | _ => new Cell(value.ToString() ?? string.Empty) |
326 | 329 | }; |
327 | 330 | } |
| 331 | + |
| 332 | + private static Cell CreateDateCell(DateTime dt) |
| 333 | + { |
| 334 | + try |
| 335 | + { |
| 336 | + return new Cell(dt); |
| 337 | + } |
| 338 | + catch (Exception) |
| 339 | + { |
| 340 | + // Fallback: keep the value, but as text to avoid breaking the whole export. |
| 341 | + // ISO 8601 is unambiguous and sorts nicely in Excel. |
| 342 | + var asText = dt.ToString("O", CultureInfo.InvariantCulture); |
| 343 | + return new Cell(asText); |
| 344 | + } |
| 345 | + } |
328 | 346 |
|
329 | 347 | private static async Task<byte[]> CreateMultiSheetXlsxFileAsync<T>(IList<T> list, |
330 | 348 | List<ExportColumn> columns, |
|
0 commit comments