代码:
![](https://i-blog.csdnimg.cn/blog_migrate/8f900a89c6347c561fdf2122f13be562.gif)
![](https://i-blog.csdnimg.cn/blog_migrate/961ddebeb323a10fe0623af514929fc1.gif)
ICellStyle cellStyle = workbook.CreateCellStyle(); cellStyle.BorderBottom = BorderStyle.Thin; cellStyle.BorderLeft = BorderStyle.Thin; cellStyle.BorderRight = BorderStyle.Thin; cellStyle.BorderTop = BorderStyle.Thin; cellStyle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; cellStyle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; ICellStyle cellStyleTitle = workbook.CreateCellStyle(); cellStyleTitle.BorderBottom = BorderStyle.Thin; cellStyleTitle.BorderLeft = BorderStyle.Thin; cellStyleTitle.BorderRight = BorderStyle.Thin; cellStyleTitle.BorderTop = BorderStyle.Thin; cellStyleTitle.VerticalAlignment = NPOI.SS.UserModel.VerticalAlignment.Center; cellStyleTitle.Alignment = NPOI.SS.UserModel.HorizontalAlignment.Center; IFont font = workbook.CreateFont(); font.IsBold = false; font.FontHeightInPoints = 12; cellStyleTitle.SetFont(font); HSSFPalette palette = ((HSSFWorkbook)workbook).GetCustomPalette(); palette.SetColorAtIndex((short)9, 98, 185, 106); var color = palette.FindColor(98, 185, 106); //下面两行设置单元格背景色 cellStyleTitle.FillPattern = FillPattern.SolidForeground; cellStyleTitle.FillForegroundColor = color.Indexed; ICellStyle cellStyleTitle2 = workbook.CreateCellStyle(); cellStyleTitle2.CloneStyleFrom(cellStyleTitle); font = workbook.CreateFont(); font.IsBold = false; font.FontHeightInPoints = 14; cellStyleTitle2.SetFont(font);