EPPlus 日期格式正确生成
EPPlus dates FORMAT gengerated properly
我的日期格式有问题,我有一个通用的 class,它显示 属性 和 属性 类型。
EPPlus 为日期列生成了良好的格式,但另一个却不是。
public class ExcelHelper
{
public static byte[] GenerateXls<T>(List<T> dataSource, string sheetName)
{
using (var pck = new ExcelPackage())
{
var modelProperties = typeof(T).GetProperties();
Type typeNullDatetime = typeof(DateTime?);
Type typeDateTime = typeof(DateTime);
Type typeDecimal = typeof(decimal);
Type typeNullDecimal = typeof(decimal?);
var ws = pck.Workbook.Worksheets.Add(sheetName);
ws.Cells[1, 1].LoadFromCollection(dataSource, true, OfficeOpenXml.Table.TableStyles.Light1);
for (var j = 0; j < modelProperties.Length; j++)
{
if (modelProperties[j].PropertyType == typeNullDatetime
&& DateTimeFormatInfo.CurrentInfo != null)
ws.Column(j).Style.Numberformat.Format =
DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
if (modelProperties[j].PropertyType == typeDateTime)
if (DateTimeFormatInfo.CurrentInfo != null)
ws.Column(j).Style.Numberformat.Format =
DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
if (modelProperties[j].PropertyType == typeDecimal)
ws.Column(j).Style.Numberformat.Format = "0.00";
if (modelProperties[j].PropertyType == typeNullDecimal)
ws.Column(j).Style.Numberformat.Format = "0.00";
}
ws.Cells.AutoFitColumns();
return pck.GetAsByteArray();
}
}
}
var result = ExcelHelper.GenerateXls(dtos, "data");
为包含以下两个属性的 class 生成的 excel:
public DateTime IssueDate { get; set; }
public DateTime DeadlineDate { get; set; }
Issue Date Deadline Date
25/06/2019 43641
25/06/2019 43641
05/06/2019 43622
27/05/2019 43612
24/04/2019 43579
15/04/2019 43570
在 EPPlus 中,列和行是不是基于零的索引而是基于 1 的索引。所以第一行的第一列有 Row=1 和 Column=1。
您需要像下面这样更改循环
...
for (var j = 1; j <= modelProperties.Length; j++)
...
或
...
ws.Column(j+1)
...
当我想根据给定的类型动态地格式化列时,我遇到了同样的问题。我想出了一个解决空类型检查的解决方案。您当然可以将数字格式(和其他样式)设置为您喜欢的任何格式。希望对您有所帮助!
public static void FormatColumns<T>(ExcelWorksheet sheet)
{
var properties = typeof(T).GetProperties();
foreach (var property in properties)
{
var columnIndex = Array.IndexOf(properties, property) + 1; //Since it is not zero based
var type = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; // Checks if it's a nullable
var typeCode = Type.GetTypeCode(type);
FormatRange(sheet.Column(columnIndex), typeCode);
}
}
public static void FormatRange(ExcelColumn range, TypeCode typeCode)
{
switch (typeCode)
{
case TypeCode.Int32:
range.Style.Numberformat.Format = "General";
break;
case TypeCode.Double:
range.Style.Numberformat.Format = "_(* #,##0.00_);_(* (#,##0.00);_(* \" - \"??_);_(@_)";
break;
case TypeCode.Decimal:
range.Style.Numberformat.Format = "_ * #,##0.00_ ;[red]_ * -#,##0.00_ ;_ * \"-\"??_ ;_ @_ ";
break;
case TypeCode.DateTime:
range.Style.Numberformat.Format = "dd-MMM-yy";
break;
default:
range.Style.Numberformat.Format = "General";
break;
}
}
我的日期格式有问题,我有一个通用的 class,它显示 属性 和 属性 类型。
EPPlus 为日期列生成了良好的格式,但另一个却不是。
public class ExcelHelper
{
public static byte[] GenerateXls<T>(List<T> dataSource, string sheetName)
{
using (var pck = new ExcelPackage())
{
var modelProperties = typeof(T).GetProperties();
Type typeNullDatetime = typeof(DateTime?);
Type typeDateTime = typeof(DateTime);
Type typeDecimal = typeof(decimal);
Type typeNullDecimal = typeof(decimal?);
var ws = pck.Workbook.Worksheets.Add(sheetName);
ws.Cells[1, 1].LoadFromCollection(dataSource, true, OfficeOpenXml.Table.TableStyles.Light1);
for (var j = 0; j < modelProperties.Length; j++)
{
if (modelProperties[j].PropertyType == typeNullDatetime
&& DateTimeFormatInfo.CurrentInfo != null)
ws.Column(j).Style.Numberformat.Format =
DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
if (modelProperties[j].PropertyType == typeDateTime)
if (DateTimeFormatInfo.CurrentInfo != null)
ws.Column(j).Style.Numberformat.Format =
DateTimeFormatInfo.CurrentInfo.ShortDatePattern;
if (modelProperties[j].PropertyType == typeDecimal)
ws.Column(j).Style.Numberformat.Format = "0.00";
if (modelProperties[j].PropertyType == typeNullDecimal)
ws.Column(j).Style.Numberformat.Format = "0.00";
}
ws.Cells.AutoFitColumns();
return pck.GetAsByteArray();
}
}
}
var result = ExcelHelper.GenerateXls(dtos, "data");
为包含以下两个属性的 class 生成的 excel:
public DateTime IssueDate { get; set; }
public DateTime DeadlineDate { get; set; }
Issue Date Deadline Date
25/06/2019 43641
25/06/2019 43641
05/06/2019 43622
27/05/2019 43612
24/04/2019 43579
15/04/2019 43570
在 EPPlus 中,列和行是不是基于零的索引而是基于 1 的索引。所以第一行的第一列有 Row=1 和 Column=1。 您需要像下面这样更改循环
...
for (var j = 1; j <= modelProperties.Length; j++)
...
或
...
ws.Column(j+1)
...
当我想根据给定的类型动态地格式化列时,我遇到了同样的问题。我想出了一个解决空类型检查的解决方案。您当然可以将数字格式(和其他样式)设置为您喜欢的任何格式。希望对您有所帮助!
public static void FormatColumns<T>(ExcelWorksheet sheet)
{
var properties = typeof(T).GetProperties();
foreach (var property in properties)
{
var columnIndex = Array.IndexOf(properties, property) + 1; //Since it is not zero based
var type = Nullable.GetUnderlyingType(property.PropertyType) ?? property.PropertyType; // Checks if it's a nullable
var typeCode = Type.GetTypeCode(type);
FormatRange(sheet.Column(columnIndex), typeCode);
}
}
public static void FormatRange(ExcelColumn range, TypeCode typeCode)
{
switch (typeCode)
{
case TypeCode.Int32:
range.Style.Numberformat.Format = "General";
break;
case TypeCode.Double:
range.Style.Numberformat.Format = "_(* #,##0.00_);_(* (#,##0.00);_(* \" - \"??_);_(@_)";
break;
case TypeCode.Decimal:
range.Style.Numberformat.Format = "_ * #,##0.00_ ;[red]_ * -#,##0.00_ ;_ * \"-\"??_ ;_ @_ ";
break;
case TypeCode.DateTime:
range.Style.Numberformat.Format = "dd-MMM-yy";
break;
default:
range.Style.Numberformat.Format = "General";
break;
}
}