| | | 1 | | using ArturRios.Data.Export.Abstractions; |
| | | 2 | | using ArturRios.Data.Export.Excel.Configuration; |
| | | 3 | | using ArturRios.Data.Export.Exporters; |
| | | 4 | | using ClosedXML.Excel; |
| | | 5 | | using Microsoft.Extensions.Logging; |
| | | 6 | | |
| | | 7 | | namespace ArturRios.Data.Export.Excel.Exporters; |
| | | 8 | | |
| | | 9 | | /// <summary>Writes records to a single-worksheet .xlsx workbook using ClosedXML and the shared column map.</summary> |
| | | 10 | | /// <typeparam name="T">The record type.</typeparam> |
| | | 11 | | /// <param name="options">Excel options.</param> |
| | | 12 | | /// <param name="logger">Optional logger; see <see cref="ExporterBase{T}" />.</param> |
| | | 13 | | public class ExcelExporter<T>(ExcelExportOptions options, ILogger<ExcelExporter<T>>? logger = null) |
| | 18 | 14 | | : ExporterBase<T>(logger) where T : class |
| | | 15 | | { |
| | | 16 | | /// <inheritdoc /> |
| | | 17 | | protected override Task WriteCoreAsync(IEnumerable<T> data, Stream destination, CancellationToken ct) |
| | | 18 | | { |
| | 16 | 19 | | var columns = ColumnMap.For<T>(); |
| | 16 | 20 | | using var workbook = new XLWorkbook(); |
| | 16 | 21 | | var worksheet = workbook.Worksheets.Add(options.SheetName); |
| | 16 | 22 | | var row = 1; |
| | | 23 | | |
| | 16 | 24 | | if (options.IncludeHeader) |
| | | 25 | | { |
| | 168 | 26 | | for (var i = 0; i < columns.Count; i++) |
| | | 27 | | { |
| | 68 | 28 | | var cell = worksheet.Cell(row, i + 1); |
| | 68 | 29 | | cell.Value = columns[i].Header; |
| | 68 | 30 | | if (options.BoldHeader) |
| | 68 | 31 | | cell.Style.Font.Bold = true; |
| | | 32 | | } |
| | | 33 | | |
| | 16 | 34 | | row++; |
| | | 35 | | } |
| | | 36 | | |
| | 60 | 37 | | foreach (var item in data) |
| | | 38 | | { |
| | 14 | 39 | | ct.ThrowIfCancellationRequested(); |
| | 152 | 40 | | for (var i = 0; i < columns.Count; i++) |
| | | 41 | | { |
| | 62 | 42 | | SetCell(worksheet.Cell(row, i + 1), columns[i].Getter(item)); |
| | | 43 | | } |
| | | 44 | | |
| | 14 | 45 | | row++; |
| | | 46 | | } |
| | | 47 | | |
| | 16 | 48 | | if (options.AutoFitColumns && columns.Count > 0) |
| | | 49 | | { |
| | 16 | 50 | | worksheet.Columns().AdjustToContents(); |
| | | 51 | | } |
| | | 52 | | |
| | 16 | 53 | | workbook.SaveAs(destination); |
| | 16 | 54 | | return Task.CompletedTask; |
| | 16 | 55 | | } |
| | | 56 | | |
| | | 57 | | // Built-in number format 14: the short date in the reader's locale. |
| | | 58 | | private const int ShortDateFormatId = 14; |
| | | 59 | | |
| | | 60 | | private static void SetCell(IXLCell cell, object? value) |
| | | 61 | | { |
| | 62 | 62 | | switch (value) |
| | | 63 | | { |
| | | 64 | | case null: |
| | | 65 | | break; |
| | | 66 | | case bool b: |
| | 0 | 67 | | cell.Value = b; |
| | 0 | 68 | | break; |
| | | 69 | | case DateTime dt: |
| | 0 | 70 | | cell.Value = dt; |
| | 0 | 71 | | break; |
| | | 72 | | case DateOnly date: |
| | 10 | 73 | | cell.Value = date.ToDateTime(TimeOnly.MinValue); |
| | 10 | 74 | | cell.Style.NumberFormat.NumberFormatId = ShortDateFormatId; |
| | 10 | 75 | | break; |
| | | 76 | | case TimeSpan span: |
| | 10 | 77 | | cell.Value = span; |
| | 10 | 78 | | break; |
| | | 79 | | case TimeOnly time: |
| | 10 | 80 | | cell.Value = time.ToTimeSpan(); |
| | 10 | 81 | | break; |
| | | 82 | | // A cell cannot hold NaN or an infinity - ClosedXML rejects them, which would fail the whole |
| | | 83 | | // export - so they are written as text, the way CSV renders them. |
| | 10 | 84 | | case double d when !double.IsFinite(d): |
| | 6 | 85 | | cell.Value = ValueRenderer.Render(d); |
| | 6 | 86 | | break; |
| | 0 | 87 | | case float f when !float.IsFinite(f): |
| | 0 | 88 | | cell.Value = ValueRenderer.Render(f); |
| | 0 | 89 | | break; |
| | | 90 | | // xlsx stores all numbers as IEEE-754 double; long/ulong > 2^53 and high-precision decimals lose precision. |
| | | 91 | | case sbyte or byte or short or ushort or int or uint or long or ulong or float or double or decimal: |
| | 12 | 92 | | cell.Value = Convert.ToDouble(value, System.Globalization.CultureInfo.InvariantCulture); |
| | 12 | 93 | | break; |
| | | 94 | | default: |
| | 14 | 95 | | cell.Value = ValueRenderer.Render(value); |
| | | 96 | | break; |
| | | 97 | | } |
| | 14 | 98 | | } |
| | | 99 | | } |