protected virtual IWorkbook CreateExcelWorkBook(string locale, ExportTemplate exportTemplate, SearchArgs <T> searchArgs) { var result = Search(searchArgs); IWorkbook workbook = new XSSFWorkbook(); ISheet sheet = workbook.CreateSheet("Sheet1"); var row = sheet.CreateRow(0); var exportProperties = typeof(TDTO).GetProperties(BindingFlags.Public | BindingFlags.Instance); var exportFields = exportTemplate.Headers.Select(y => y.Field).ToList(); for (int i = 0; i < exportFields.Count; i++) { var header = exportTemplate.Headers.FirstOrDefault(x => x.Field.Equals(exportFields[i])); var cell = row.CreateCell(i, CellType.String); cell.SetCellValue("en-us".EqualsIgnoreCase(locale) ? header.EnHeaderText : header.CnHeaderText); //表头 } for (int i = 1; i < result.Items.Count() + 1; i++) { row = sheet.CreateRow(i); for (int j = 0; j < exportFields.Count; j++) { var cell = row.CreateCell(j, CellType.String); var value = exportProperties.FirstOrDefault(x => x.Name == exportFields[j])?.GetValue(result.Items.ElementAt(i - 1)); var header = exportTemplate.Headers.FirstOrDefault(x => x.Field.Equals(exportFields[j])); if (header.Formatter != null) { value = header.Formatter(value); } cell.SetCellValue(value?.ToString()); } } return(workbook); }
public virtual string Export(string locale, ExportTemplate exportTemplate, SearchArgs <T> searchArgs) { var webRootPath = PlatformServices.Default.Application.ApplicationBasePath; var dir = Path.Combine(webRootPath, "files", "export"); if (!Directory.Exists(dir)) { Directory.CreateDirectory(dir); } string fileNamePrefix = "en-us".EqualsIgnoreCase(locale) ? exportTemplate.EnFileNamePrefix : exportTemplate.CnFileNamePrefix; string fileName = $"{fileNamePrefix}_{DateTime.Now.ToStringIgnoreCulture("yyyyMMddHHmmss")}.xlsx"; var filePath = Path.Combine(dir, fileName); if (File.Exists(filePath)) { var attachment = AttachmentRepository.SingleOrDefault(x => x.OrginalFileName == fileName); if (attachment != null) { attachment.DownloadTimes++; AttachmentRepository.Update(attachment); return(attachment.FileGuid); } } IWorkbook workbook = CreateExcelWorkBook(locale, exportTemplate, searchArgs); using (var stream = new FileStream(filePath, FileMode.Create, FileAccess.Write)) { workbook.Write(stream); var md5 = MD5Util.MD5Stream(filePath); var size = new FileInfo(filePath).Length; Attachment attachment = new Attachment() { Type = "export", ContentType = "application/vnd.ms-excel", DownloadTimes = 0, FileExtention = ".xlsx", FileName = fileName, OrginalFileName = fileName, MD5 = md5, Size = size, }; var user = GetUserIdentity(); if (user != null) { attachment.CreateTime = DateTime.Now; attachment.CreateUserName = user.UserName; attachment.ModifyTime = DateTime.Now; attachment.ModifyUserName = user.UserName; } AttachmentRepository.Insert(attachment); return(attachment.FileGuid); } }