| | | 1 | | using ArturRios.Data.Export.Abstractions; |
| | | 2 | | using ArturRios.Data.Export.Excel.Configuration; |
| | | 3 | | using ArturRios.Data.Export.Exporters; |
| | | 4 | | using ClosedXML.Excel; |
| | | 5 | | |
| | | 6 | | namespace ArturRios.Data.Export.Excel.Exporters; |
| | | 7 | | |
| | | 8 | | /// <summary>Writes records to a single-worksheet .xlsx workbook using ClosedXML and the shared column map.</summary> |
| | | 9 | | /// <typeparam name="T">The record type.</typeparam> |
| | | 10 | | /// <param name="options">Excel options.</param> |
| | 4 | 11 | | public class ExcelExporter<T>(ExcelExportOptions options) : ExporterBase<T> where T : class |
| | | 12 | | { |
| | | 13 | | /// <inheritdoc /> |
| | | 14 | | protected override Task WriteCoreAsync(IEnumerable<T> data, Stream destination, CancellationToken ct) |
| | 3 | 15 | | { |
| | 3 | 16 | | var columns = ColumnMap.For<T>(); |
| | 3 | 17 | | using var workbook = new XLWorkbook(); |
| | 3 | 18 | | var worksheet = workbook.Worksheets.Add(options.SheetName); |
| | 3 | 19 | | var row = 1; |
| | | 20 | | |
| | 3 | 21 | | if (options.IncludeHeader) |
| | 3 | 22 | | { |
| | 24 | 23 | | for (var i = 0; i < columns.Count; i++) |
| | 9 | 24 | | { |
| | 9 | 25 | | var cell = worksheet.Cell(row, i + 1); |
| | 9 | 26 | | cell.Value = columns[i].Header; |
| | 18 | 27 | | if (options.BoldHeader) cell.Style.Font.Bold = true; |
| | 9 | 28 | | } |
| | | 29 | | |
| | 3 | 30 | | row++; |
| | 3 | 31 | | } |
| | | 32 | | |
| | 13 | 33 | | foreach (var item in data) |
| | 2 | 34 | | { |
| | 2 | 35 | | ct.ThrowIfCancellationRequested(); |
| | 16 | 36 | | for (var i = 0; i < columns.Count; i++) |
| | 6 | 37 | | { |
| | 6 | 38 | | SetCell(worksheet.Cell(row, i + 1), columns[i].Getter(item)); |
| | 6 | 39 | | } |
| | | 40 | | |
| | 2 | 41 | | row++; |
| | 2 | 42 | | } |
| | | 43 | | |
| | 3 | 44 | | if (options.AutoFitColumns && columns.Count > 0) |
| | 3 | 45 | | { |
| | 3 | 46 | | worksheet.Columns().AdjustToContents(); |
| | 3 | 47 | | } |
| | | 48 | | |
| | 3 | 49 | | workbook.SaveAs(destination); |
| | 3 | 50 | | return Task.CompletedTask; |
| | 3 | 51 | | } |
| | | 52 | | |
| | | 53 | | private static void SetCell(IXLCell cell, object? value) |
| | 6 | 54 | | { |
| | 6 | 55 | | switch (value) |
| | | 56 | | { |
| | | 57 | | case null: |
| | 0 | 58 | | break; |
| | | 59 | | case bool b: |
| | 0 | 60 | | cell.Value = b; |
| | 0 | 61 | | break; |
| | | 62 | | case DateTime dt: |
| | 0 | 63 | | cell.Value = dt; |
| | 0 | 64 | | break; |
| | | 65 | | // xlsx stores all numbers as IEEE-754 double; long/ulong > 2^53 and high-precision decimals lose precision. |
| | | 66 | | case sbyte or byte or short or ushort or int or uint or long or ulong or float or double or decimal: |
| | 4 | 67 | | cell.Value = Convert.ToDouble(value, System.Globalization.CultureInfo.InvariantCulture); |
| | 4 | 68 | | break; |
| | | 69 | | default: |
| | 2 | 70 | | cell.Value = ValueRenderer.Render(value); |
| | 2 | 71 | | break; |
| | | 72 | | } |
| | 6 | 73 | | } |
| | | 74 | | } |