/// <summary> /// draw the cell font and style /// </summary> /// <param name="cell">the excel cell</param> /// <param name="drawing">the ColumnDrawing object</param> /// <param name="alternate">is alternate excel cell</param> protected virtual void DrawCellFontAndStyle(ICell cell, ColumnDrawing drawing, bool alternate) { if (UseTemplate) { return; } ICellStyle style; IFont font; if (alternate) { style = drawing.AlternateCellStyle; font = drawing.AlternateCellFont; } else { style = drawing.CellStyle; font = drawing.CellFont; } if (style == null) { style = cell.Sheet.Workbook.GetCellStyleAt(0); } cell.CellStyle = style; if (font != null) { cell.CellStyle.SetFont(font); } }
/// <summary> /// draw the header cell font and style /// </summary> /// <param name="cell">the excel cell</param> /// <param name="drawing">the ColumnDrawing object</param> protected virtual void DrawHeaderFontAndStyle(ICell cell, ColumnDrawing drawing) { cell.SetCellType(CellType.String); cell.SetCellValue(drawing.ColumnName); if (UseTemplate) { return; } cell.CellStyle = drawing.HeaderStyle; }
/// <summary> /// get the ColumnDrawing info from the model type /// </summary> /// <param name="classType">the type of the model class</param> /// <returns>the ColumnDrawing array</returns> protected virtual ColumnDrawing[] GetColumnDrawings(Type classType) { var cellList = new List <ColumnDrawing>(); var classProperties = classType.GetProperties(BindingFlags.Instance | BindingFlags.Public | BindingFlags.GetProperty); if (classProperties.Length <= 0) { return(new ColumnDrawing[0]); } var columnIndexes = new List <int>(); foreach (var property in classProperties) { var ignoreAttr = property.GetCustomAttribute <DrawingIgnoreAttribute>(); var propAttr = property.GetCustomAttribute <NPOIColumnAttribute>(); if (ignoreAttr != null || propAttr == null) { continue; } var cellInfo = new ColumnDrawing(); // Column basic information if (propAttr.Index < 0) { throw new Exception("Column Index is out of range.\r\nThe value must not be smaller than 0."); } if (columnIndexes.Contains(propAttr.Index)) { throw new Exception("Duplicate column index " + propAttr.Index); } cellInfo.ColumnIndex = propAttr.Index; columnIndexes.Add(propAttr.Index); if (!string.IsNullOrEmpty(propAttr.Name)) { cellInfo.ColumnName = propAttr.Name; } if (string.IsNullOrEmpty(cellInfo.ColumnName)) { cellInfo.ColumnName = property.Name; } // Column style information var headerStyleAttr = property.GetCustomAttribute <HeaderStyleAttribute>(); var headerStyle = FillCellStyle(headerStyleAttr); if (headerStyle != null) { cellInfo.HeaderStyle = headerStyle; } if (headerStyleAttr != null) { cellInfo.ColumnWidth = headerStyleAttr.ColumnWidth; } var cellStyleAttr = property.GetCustomAttribute <CellStyleAttribute>(); if (cellStyleAttr != null) { var cellStyle = FillCellStyle(cellStyleAttr); if (cellStyle != null) { cellInfo.CellStyle = cellStyle; } var cellFont = FillFont(cellStyleAttr); if (cellFont != null) { cellInfo.CellFont = cellFont; } } var alternateCellStyleAttr = property.GetCustomAttribute <AlternateCellStyleAttribute>(); if (alternateCellStyleAttr != null) { cellInfo.HasAlternate = true; var cellStyle = FillCellStyle(alternateCellStyleAttr); if (cellStyle != null) { cellInfo.AlternateCellStyle = cellStyle; } var cellFont = FillFont(alternateCellStyleAttr); if (cellFont != null) { cellInfo.AlternateCellFont = cellFont; } } else { cellInfo.HasAlternate = false; } cellInfo.Property = property; cellList.Add(cellInfo); } return(cellList.OrderBy(x => x.ColumnIndex).ToArray()); }