1. 程式人生 > 其它 >NPOI 自定義單元格背景顏色-Excel

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