dotnetcore / dotnetcore/Magicodes.IE
写了一个动态导出字段的方法,最后设置的属性没有带出来,这是哪里出的问题
- Dominant language
- C#
- Stars
- 2.2k
- Forks
- 495
- PR merge metrics
- No merged PRs in 30d
Description
`///
/// 根据字段导出Excel[动态导出]
///
///
///
///
///
///
///
public async Task ExportActity(string fields, List list, string fileName) where T : class, new()
{
IExporter exporter = new ExcelExporter();
var expandoObjectList = new List(list.Count);
var propertyInfoList = new List();
var fieldsAfterSplit = fields.Split(',');
foreach (var field in fieldsAfterSplit)
{
var propertyName = field.Trim();
var propertyInfo = typeof(T).GetProperty(propertyName);
if (propertyInfo == null)
{
throw new Exception($"Property: {propertyName} 没有找到:{typeof(T)}");
}
propertyInfoList.Add(propertyInfo);
}
foreach (var person in list)
{
var shapedObj = new ExpandoObject();
foreach (var propertyInfo in propertyInfoList)
{
var propertyValue = propertyInfo.GetValue(person);
((IDictionary)shapedObj).Add(propertyInfo.Name, propertyValue);
}
expandoObjectList.Add(shapedObj);
}
string pathFile = Path.Combine(Directory.GetCurrentDirectory(), fileName);
var result = await exporter.ExportAsByteArray(expandoObjectList);
File.WriteAllBytes(pathFile, result);
//文件上传OSS
string ossPath = await _oSSHelper.Upload(fileName, pathFile);
return ossPath;
}
`
`
[ExcelExporter(Name = "用户信息_123", TableStyle = TableStyles.Light12,HeaderFontSize = 14, FontSize = 13,AutoFitAllColumn = true)]
public class userInfo
{
[Display(Name = "系统编码")]
[ExporterHeader(IsIgnore = true)]
public string? id { get; set; }
///
/// 用户名称
///
[Display(Name = "用户名称")]
[ExporterHeader(DisplayName = "用户名称", AutoCenterColumn = true, IsBold = true)]
public string? user_name { get; set; }
///
/// 用户编码
///
[Display(Name = "用户编码")]
[ExporterHeader(DisplayName = "用户编码", AutoCenterColumn = true, IsBold = true)]
public string? user_code { get; set; }
///
/// 密码
///
[Display(Name = "密码")]
public string? pass_word { get; set; }
///
/// 性别
///
[ExporterHeader(DisplayName = "性别", AutoCenterColumn = true, IsBold = true)]
public string? sex { get; set; }
///
/// 头像
///
public string? atvatar_url { get; set; }
///
/// 生日
///
[ExporterHeader(DisplayName = "生日", AutoCenterColumn = true, IsBold = true)]
public string? birthday { get; set; }
///
/// 地址
///
public string? address { get; set; }
///
/// 手机号码
///
[ExporterHeader(DisplayName = "手机号码", AutoCenterColumn = true, IsBold = true)]
public string? phone { get; set; }
///
/// 备注
///
[ExporterHeader(DisplayName = "备注", AutoCenterColumn = true, IsBold = true)]
public string? remark { get; set; }
///
/// 创建时间
///
public DateTime? create_date { get; set; } = DateTime.Now;
///
/// 创建人
///
public string? create_user { get; set; } = "system";
///
/// 更新日期
///
public DateTime? modify_date { get; set; }
///
/// 更新人
///
public string? modify_user { get; set; }
}
`
Contributor guide
No contributing guide indexed for this repository
Assessment
This issue has not been assessed yet.