< Summary

Information
Class: ArturRios.Data.Export.Excel.Exporters.ExcelExporter<T>
Assembly: ArturRios.Data.Export.Excel
File(s): D:\Repositories\dotnet-data\src\ArturRios.Data.Export.Excel\Exporters\ExcelExporter.cs
Line coverage
88%
Covered lines: 39
Uncovered lines: 5
Coverable lines: 44
Total lines: 74
Line coverage: 88.6%
Branch coverage
92%
Covered branches: 39
Total branches: 42
Branch coverage: 92.8%
Method coverage
100%
Covered methods: 3
Fully covered methods: 1
Total methods: 3
Method coverage: 100%
Full method coverage: 33.3%

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
.ctor(...)100%11100%
WriteCoreAsync(...)92.85%1414100%
SetCell(...)92.85%852858.33%

File(s)

D:\Repositories\dotnet-data\src\ArturRios.Data.Export.Excel\Exporters\ExcelExporter.cs

#LineLine coverage
 1using ArturRios.Data.Export.Abstractions;
 2using ArturRios.Data.Export.Excel.Configuration;
 3using ArturRios.Data.Export.Exporters;
 4using ClosedXML.Excel;
 5
 6namespace 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>
 411public 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)
 315    {
 316        var columns = ColumnMap.For<T>();
 317        using var workbook = new XLWorkbook();
 318        var worksheet = workbook.Worksheets.Add(options.SheetName);
 319        var row = 1;
 20
 321        if (options.IncludeHeader)
 322        {
 2423            for (var i = 0; i < columns.Count; i++)
 924            {
 925                var cell = worksheet.Cell(row, i + 1);
 926                cell.Value = columns[i].Header;
 1827                if (options.BoldHeader) cell.Style.Font.Bold = true;
 928            }
 29
 330            row++;
 331        }
 32
 1333        foreach (var item in data)
 234        {
 235            ct.ThrowIfCancellationRequested();
 1636            for (var i = 0; i < columns.Count; i++)
 637            {
 638                SetCell(worksheet.Cell(row, i + 1), columns[i].Getter(item));
 639            }
 40
 241            row++;
 242        }
 43
 344        if (options.AutoFitColumns && columns.Count > 0)
 345        {
 346            worksheet.Columns().AdjustToContents();
 347        }
 48
 349        workbook.SaveAs(destination);
 350        return Task.CompletedTask;
 351    }
 52
 53    private static void SetCell(IXLCell cell, object? value)
 654    {
 655        switch (value)
 56        {
 57            case null:
 058                break;
 59            case bool b:
 060                cell.Value = b;
 061                break;
 62            case DateTime dt:
 063                cell.Value = dt;
 064                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:
 467                cell.Value = Convert.ToDouble(value, System.Globalization.CultureInfo.InvariantCulture);
 468                break;
 69            default:
 270                cell.Value = ValueRenderer.Render(value);
 271                break;
 72        }
 673    }
 74}