我想将数据从数据表导出到excel,并将当前时间戳导出到excel的底部。如何在Excel中添加当前时间戳。
答案 0 :(得分:1)
我使用EPPlus。这是我为我的应用程序创建的通用函数。该函数采用通用的List<T>
并导出到Excel。 (T)类属性名称成为Excel电子表格中的标题。
public const string ExcelMimeType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
public FileContentResult ExportToExcel<T>(string reportName, IEnumerable<T> reportLines) where T : class
{
MemberInfo[] info = typeof(T).GetProperties().Select(c => (MemberInfo)c).ToArray();
ExcelPackage package = new ExcelPackage();
ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Data");
worksheet.Cells["A1"].LoadFromCollection(reportLines, true, TableStyles.Light1, BindingFlags.GetProperty, info);
string fileName = $"{reportName}_{DateTime.Now:ddMMyyyy}.xlsx".Replace(" ", "-");
return File(package.GetAsByteArray(), ExcelMimeType, fileName);
}