private void CreatetTitle(IWorkbook workbook, ExportTitle title, ISheet sheet, int columnCount) { ICellStyle cellstyle = workbook.CreateCellStyle(); cellstyle.Alignment = (HorizontalAlignment)title.HorizontalAlign; var font = workbook.CreateFont(); font.IsBold = title.IsBold; font.FontName = title.FontName; font.FontHeightInPoints = title.FontSize; font.Color = title.FontColor; cellstyle.SetFont(font); var titleRow = sheet.CreateRow(0); titleRow.CreateCell(0).SetCellValue(title.Title); CellRangeAddress region = new CellRangeAddress(0, 0, 0, columnCount); sheet.AddMergedRegion(region); sheet.GetRow(0).GetCell(0).CellStyle = cellstyle; }
public ISheet CreateSheet <T>(IWorkbook workbook, string sheetName, ExportTitle title, IEnumerable <T> data, IEnumerable <string> filterColumn) where T : ExportModel { var sheet = string.IsNullOrWhiteSpace(sheetName) ? workbook.CreateSheet() : workbook.CreateSheet(sheetName); if (data == null) { return(sheet); } var columnProperties = this.GetColumnProperties(data, filterColumn); int rowIndex = 0; if (title != null) { this.CreatetTitle(workbook, title, sheet, columnProperties.Count - 1); rowIndex++; } this.CreateHeader(workbook, sheet, columnProperties, rowIndex++); var columnStyles = this.CreateColumnStyles(workbook, columnProperties); foreach (var item in data) { var columnId = 0; var dataRow = sheet.CreateRow(rowIndex); item.ExportRowIndex = rowIndex; rowIndex++; foreach (var property in columnProperties) { var cell = dataRow.CreateCell(columnId++); var value = property.PropertyInfo.GetValue(item); if (value == null) { cell.SetCellValue(string.Empty); continue; } string valueStr; if (property.PropertyInfo.PropertyType == typeof(DateTime) || property.PropertyInfo.PropertyType == typeof(DateTime?)) { var format = string.IsNullOrWhiteSpace(property.StringFormat) ? DefaultDateFormat : property.StringFormat; valueStr = ((DateTime)property.PropertyInfo.GetValue(item)).ToString(format); } else { valueStr = property.PropertyInfo.GetValue(item).ToString(); } cell.SetCellValue(valueStr); cell.CellStyle = columnStyles[property.ColumnIndex]; } } this.RowMerged(data, sheet, columnProperties); this.SetColumnWidth(sheet, columnProperties); return(sheet); }