2017-07-27 88 views
0

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

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

我用这个页面:

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

回答

1

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)) 
+0

谢谢您的回答但我已经完成了这一行:dataTable.Columns.Add(ColumnsToTake.ElementAt(i).Value,(property.PropertyType == typeof(System.DateTime)?typeof(System.String):(Nullable.GetUnderlyingType property.PropertyType)?? property.PropertyType))); 但我不需要时间部分的日期,我不能删除它。你能帮我吗,请问? –

+0

这是否意味着我必须浏览datetime列的所有单元格,并将其值设置为“yyyy.MM.dd.”格式? –

+0

如果这对你更容易,你可以浏览所有的日期。或者检查日期是否在1900年前,并将其格式化为文本,并按照现在的方式保留其余部分。 –

相关问题