c# - NPOI:将单元格颜色设置为自定义颜色
问题描述
如何使用 C# NPOI 将单元格颜色设置为自定义颜色?我知道如何使用颜色索引更改带有 NPOI 的单元格的颜色:
XSSFCellStyle cellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
cellStyle.FillBackgroundColor = IndexedColors.LightCornflowerBlue.Index;
cellStyle.FillForegroundColor = IndexedColors.LightCornflowerBlue.Index;
cellStyle.FillPattern = FillPattern.SolidForeground;
ICell cell = CurrentRow.CreateCell(CellIndex);
cell.CellStyle = cellStyle;
但是,我想使用自定义颜色。IndexedColors 是有限的,没有那么有用。我已经四处寻找答案很长时间了。
几年前我发现了这篇文章,但它似乎不再相关。SetFillForegroundColor 现在是短的,不再是 XSSFColor。
byte[] rgb = new byte[3] { 192, 0, 0 };
XSSFCellStyle HeaderCellStyle1 = (XSSFCellStyle)xssfworkbook.CreateCellStyle();
HeaderCellStyle1.SetFillForegroundColor(new XSSFColor(rgb));
解决方案
我应该一直使用 FillBackgroundXSSFColor 而不是 FillBackgroundColor
XSSFCellStyle cellStyle = (XSSFCellStyle)workbook.CreateCellStyle();
byte[] rgb = new byte[3] { 255, 221, 221 };
XSSFColor color = new XSSFColor(rgb);
cellStyle.FillBackgroundXSSFColor = color;
ICell cell = CurrentRow.CreateCell(CellIndex);
cell.CellStyle = cellStyle;
推荐阅读
- oauth - 为什么需要密码,为什么在 OAuth2.0 中不需要
- ios - 尝试使用 TOSMBClient 使用 SMB1 上传文件时的 EXC_BAD_ACCESS
- javascript - TypeError:无法将 undefined 或 null 转换为对象 React js 材料表
- sql - 在 Excel 中过滤和显示唯一的列对
- android - 在 Android 中从 uri 显示 PDF
- user-interface - 如何让我的 gridView 滚动占据 Flutter 中的整个页面?
- javascript - 使用 Swiper 5 模拟 3d 轮子的自定义效果
- json - 扑。将 json.decode 值添加到可在 UI 中使用的 String
- jenkins - 在 Jenkins 中存储 Kubernetes 集群凭证并在声明式管道中使用
- excel - Application.DisplayAlerts = False 后出错