asp.net-coreentity-framework-coreasp.net-core-mvcclosedxmlclosedxml.report

How to get typed data from sql select statement to Excel


ASP.NET Core 8 MVC application controller should create an Excel spreadsheet with typed data in columns (numbers, strings, dates, logical) from arbitrary SQL select.

I tried this code:

public async Task<IActionResult> RawData() 
{
    // https://blog.elmah.io/export-data-to-excel-with-asp-net-core/
    using var workbook = new XLWorkbook();
    var worksheet = workbook.Worksheets.Add("Test");
    using IDataReader dr = ExecuteReader("select * from foo", out IDbConnection _);

    int currentRow= 1 ;

    while (dr.Read())
    {
        for (var j = 0; j < dr.FieldCount; j++)
            if (dr.GetFieldType(j) != typeof(byte[]))
                worksheet.Cell(currentRow, j + 1).Value = dr.GetValue(j).ToString();

        currentRow++;
    }

    using var stream = new MemoryStream();
    workbook.SaveAs(stream);
    var content = stream.ToArray();

    return File(content, "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "test.xlsx");
}

Numeric, double, float, integer, boolean appear as strings in Excel.

Removing ToString() using

worksheet.Cell(currentRow, j + 1).Value = dr.GetValue(j);

throws a compile error.

How to get typed data from IDataReader into Excel or ods file?

Application uses Entity Framework Core. Maybe datareader can be replaced with Database.FromSQLRawAsync() and dynamic used.


Solution

  • ClosedXML requires XLCellValue for cell values. There is factory method which creates appropriate XLCellValue from object

    worksheet.Cell(currentRow, j + 1).Value = XLCellValue.FromObject(dr.GetValue(j));