private static ICellStyle GetDataStyle(HSSFWorkbook workbook) { //数据样式 var dataStyle = workbook.CreateCellStyle(); dataStyle.Alignment = HorizontalAlignment.LEFT;//左对齐 //数据单元格的边框 dataStyle.BorderTop = BorderStyle.THIN; dataStyle.TopBorderColor = HSSFColor.BLACK.index; dataStyle.BorderRight = BorderStyle.THIN; dataStyle.RightBorderColor = HSSFColor.BLACK.index; dataStyle.BorderBottom = BorderStyle.THIN; dataStyle.BottomBorderColor = HSSFColor.BLACK.index; dataStyle.BorderLeft = BorderStyle.THIN; dataStyle.LeftBorderColor = HSSFColor.BLACK.index; //数据的字体 var datafont = workbook.CreateFont(); datafont.FontHeightInPoints = 11;//字号 dataStyle.SetFont(datafont); return dataStyle; }
private static ICellStyle GetHeadStyle(HSSFWorkbook workbook) { //表头样式 var headStyle = workbook.CreateCellStyle(); headStyle.Alignment = HorizontalAlignment.LEFT;//居中对齐 //表头单元格背景色 headStyle.FillForegroundColor = HSSFColor.LIGHT_GREEN.index; headStyle.FillPattern = FillPatternType.SOLID_FOREGROUND; //表头单元格边框 headStyle.BorderTop = BorderStyle.THIN; headStyle.TopBorderColor = HSSFColor.BLACK.index; headStyle.BorderRight = BorderStyle.THIN; headStyle.RightBorderColor = HSSFColor.BLACK.index; headStyle.BorderBottom = BorderStyle.THIN; headStyle.BottomBorderColor = HSSFColor.BLACK.index; headStyle.BorderLeft = BorderStyle.THIN; headStyle.LeftBorderColor = HSSFColor.BLACK.index; //表头字体设置 var font = workbook.CreateFont(); font.FontHeightInPoints = 12;//字号 font.Boldweight = 600;//加粗 //font.Color = HSSFColor.WHITE.index;//颜色 headStyle.SetFont(font); return headStyle; }
/** * This method attempt to find an already existing HSSFCellStyle that matches * what you want the style to be. If it does not find the style, then it * Creates a new one. If it does Create a new one, then it applies the * propertyName and propertyValue to the style. This is necessary because * Excel has an upper limit on the number of Styles that it supports. * *@param workbook The workbook that is being worked with. *@param propertyName The name of the property that is to be * changed. *@param propertyValue The value of the property that is to be * changed. *@param cell The cell that needs it's style changes *@exception NestableException Thrown if an error happens. */ public static void SetCellStyleProperty(LF.Utils.NPOI.SS.UserModel.ICell cell, HSSFWorkbook workbook, String propertyName, Object propertyValue) { LF.Utils.NPOI.SS.UserModel.ICellStyle originalStyle = cell.CellStyle; LF.Utils.NPOI.SS.UserModel.ICellStyle newStyle = null; Hashtable values = GetFormatProperties(originalStyle); values[propertyName] = propertyValue; // index seems like what index the cellstyle is in the list of styles for a workbook. // not good to compare on! short numberCellStyles = workbook.NumCellStyles; for (short i = 0; i < numberCellStyles; i++) { LF.Utils.NPOI.SS.UserModel.ICellStyle wbStyle = workbook.GetCellStyleAt(i); Hashtable wbStyleMap = GetFormatProperties(wbStyle); if (wbStyleMap.Equals(values)) { newStyle = wbStyle; break; } } if (newStyle == null) { newStyle = workbook.CreateCellStyle(); SetFormatProperties(newStyle, workbook, values); } cell.CellStyle = (newStyle); }