导出Excel时,通过自定义Attribute设置表头

发布时间 2023-11-30 17:48:55作者: LiXiang98
var shipmentColumnNames = new List<string>();
var shipmentColumnNames = new List<string>();
var shipmentPropNames = new List<string>();
props.ForEach((p) => {
     if (p.IsDefined(typeof(ExcelColumnNameAttribute), false))
     {
           ExcelColumnNameAttribute x = (ExcelColumnNameAttribute)p.GetCustomAttribute(typeof(ExcelColumnNameAttribute));
           shipmentColumnNames.Add(x?.ColumnName ?? p.Name);
           shipmentPropNames.Add(p.Name);
     }
     else
     {
          shipmentColumnNames.Add(p.Name);
          shipmentPropNames.Add(p.Name);
     }
});
[AttributeUsage(AttributeTargets.Property)]
public class ExcelColumnNameAttribute:Attribute
{
     public string ColumnName { get; set; }
}