Warm tip: This article is reproduced from stackoverflow.com, please click
c# closedxml

Unable to insert minimum datetime as cell value in ClosedXML

发布于 2020-03-27 10:31:18

ClosedXml does not allow to insert minimum datetime into cell. My datatable includes minimum datetime values that ClosedXml does not like.

using (var workbook = new XLWorkbook())
{
    var dataTable = GetDataTable();
    workbook.Worksheets.Add(dataTable, "myWorksheet"); // Exception thrown
    workbook.SaveAs(filePath);
}

Exception is thrown when adding a worksheet: 'Unable to set cell value to 01/01/0001 00:00:00'

How could I allow ClosedXml to insert this value without throwing exception? For example it could write empty values instead of throwing exception. Would it be possible to get around this without modifying the dataTable to clean up these minimum datetime values?

Questioner
Pegaz
Viewed
225
15.2k 2019-07-04 19:57

Excel itself doesn't allow you to set a cell value to such a low date value. Try it for yourself. Previously, one could set the value in ClosedXML, but Excel would then believe that the file is corrupt. The check was added to avoid the creation of a seemingly corrupt file.