EPPlus Excel 报告日期时间早于 1900.01.00

EPPlus Excel report with DateTime before 1900.01.00

我想在 EPPlus 的帮助下创建一个 Excel 报告,但它把日期放在 1900.01.00 之前。在 Excel 工作表中,它会转换为负数或长“#####...”行出现在 Excel 中的日期位置。 我已经设置了格式,但没有帮助:

workSheet.Column(columnIndex).Style.Numberformat.Format = "yyyy.mm.dd.";

我用过这个页面:

http://www.c-sharpcorner.com/article/export-to-excel-in-asp-net-mvc/

MS Excel 无法识别 1900 年之前的日期。日期数字格式无用。您需要在 C# 中格式化日期并将值作为字符串而不是数字。

为了将数字格式设置为文本,请使用:

workSheet.Column(columnIndex).Style.Numberformat.Format = "@";

为了格式化日期,如果日期是日期时间,请使用以下代码:

System.IFormatProvider formatProvider = new System.Globalization.CultureInfo("en-US", true);//or a different culture
dateTime.ToString("yyyy.MM.dd.", formatProvider))