private void CreateTitle(SheetTitle title, ISheet sheet, int columnCount) { var 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); if (columnCount > 1) { CellRangeAddress region = new CellRangeAddress(0, 0, 0, columnCount - 1); sheet.AddMergedRegion(region); } sheet.GetRow(0).GetCell(0).CellStyle = cellStyle; }
public ISheet CreateSheet <T>(Type dateType, string sheetName, SheetTitle title, List <T> data, List <string> filterColumn) where T : SheetRow { var sheet = string.IsNullOrWhiteSpace(sheetName) ? _workBook.CreateSheet() : _workBook.CreateSheet(sheetName); if (data == null) { return(sheet); } var columnProperties = GetColumnProperties(dateType, filterColumn); int rowIndex = 0; if (title != null) { this.CreateTitle(title, sheet, columnProperties.Count); rowIndex++; } this.CreateHeader(sheet, columnProperties, rowIndex++); var cacheStyles = new Dictionary <string, ICellStyle>(); 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); IBaseStyle cellStyle = null; if ((item.CellStyles?.Any() ?? false) && item.CellStyles.ContainsKey(property.PropertyInfo.Name)) { cellStyle = item.CellStyles[property.PropertyInfo.Name]; } SetCellStyle(cell, cacheStyles, property, cellStyle); } } this.RowMerged(data, sheet, columnProperties); this.SetColumnWidth(sheet, columnProperties); return(sheet); }