NPOI 自定义单元格背景颜色-Excel

使用

在线颜色选择器 | RGB颜色查询对照表

(http://tools.jb51.net/static/colorpicker/)

查找颜色RGB

HSSFWorkbook workbook = new HSSFWorkbook();
HSSFSheet sheet = workbook.CreateSheet("计划") as HSSFSheet;

//调色板实例
HSSFPalette palette = workbook.GetCustomPalette();
palette.SetColorAtIndex((short)10, (byte)228, (byte)223, (byte)236);
HSSFColor hssFColor = palette.FindColor((byte)228, (byte)223, (byte)236);

第一个参数:设置调色板新增颜色的编号,自已设置即可;取值范围8-64

如果是8,9会导致边框被覆盖,其中,8是改变边框的颜色,9是改变整个sheet的背景颜色

第二、第三、第四个参数,组成RGB值

ICellStyle title_style = workbook.CreateCellStyle();
//设置单元格上下左右边框线
title_style.BorderTop = BorderStyle.Thin;
title_style.BorderBottom = BorderStyle.Thin;
title_style.BorderLeft = BorderStyle.Thin;
title_style.BorderRight = BorderStyle.Thin;
title_style.WrapText = true;
title_style.Alignment = HorizontalAlignment.Left;
title_style.VerticalAlignment = VerticalAlignment.Center;
title_style.FillPattern = FillPattern.SolidForeground;
title_style.FillForegroundColor = hssFColor.GetIndex();

HSSFRow dataRow2 = sheet.CreateRow(1) as HSSFRow;
for (int i = 0; i < det_title_arr.Length; i++)
{
ICell Cell = dataRow2.CreateCell(i);
Cell.CellStyle = title_style;
}

参考:https://www.cnblogs.com/yxhblog/p/6225018.html

posted @ 2022-03-30 10:10  ebigear  阅读(1214)  评论(0编辑  收藏  举报