I need to write a formatted date to Excel using EPPlus, leaving empty cells where there are no dates to write. I have tried the following:
How can I write dates to Excel, using EPPlus, where the dates are recognised as date types (not strings), but where missing date values are not written at all?
Make sure the data you bind to the date column is of type DateTime?
(nullable).
Only if you provide a null
value, an empty column will be rendered.
For example:
// Date format on first column
sheet.Column(1).Style.Numberformat.Format = "yyyy-mm-dd";
// Some date values
var columnValues = new List<DateTime?> {
DateTime.Now,
null,
DateTime.Now.AddDays(1) };
// Bind values to column
sheet.Cells[1, 1].LoadFromArrays(columnValues.Select(v => new object[] { v }));
Result: