作者热门文章
- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
我在 C#
项目中使用 NPOI 2.0.6.0
,但在更改样式时遇到了困难。虽然我可以影响字体和单元格边框,但我无法更改背景颜色。
private void buildSheet(HSSFWorkbook wb, DataTable data, string sheetName)
{
var cHelp = wb.GetCreationHelper();
var sheet = wb.CreateSheet(sheetName);
HSSFFont hFont = (HSSFFont)wb.CreateFont();
hFont.Boldweight = (short)FontBoldWeight.Bold;
hFont.Color = HSSFColor.White.Index;
hFont.FontHeightInPoints = 18;
HSSFCellStyle hStyle = (HSSFCellStyle)wb.CreateCellStyle();
hStyle.SetFont(hFont);
hStyle.BorderBottom = BorderStyle.Medium;
hStyle.FillBackgroundColor = HSSFColor.Black.Index;
IRow headerRow = sheet.CreateRow(1);
int cellCount = 1;
foreach (string str in colHeaders)
{
HSSFCell cell = (HSSFCell)headerRow.CreateCell(cellCount);
cell.SetCellValue(cHelp.CreateRichTextString((str)));
cell.CellStyle = hStyle;
cellCount += 1;
}
int rowCount = 2;
foreach (DataRow dr in data.Rows)
{
var row = sheet.CreateRow(rowCount);
for (int i = 1; i < data.Columns.Count + 1; i++)
{
row.CreateCell(i).SetCellValue(cHelp.CreateRichTextString((dr[i - 1]).ToString()));
}
rowCount += 1;
}
}
附加调试器后,我注意到 hStyle.FillBackgroundColor 从未从 0x0040 改变,尽管黑色的索引是 8。
所以本质上问题是:
HSSFCellStyle.FillBackgroundColor
不可修改?最佳答案
如果不指定 FillPattern
,则无法应用 FillBackgroundcolor
。
即
hStyle = (HSSFCellStyle)workBook.CreateCellStyle();
hStyle.SetFont(xFont);
hStyle.BorderBottom = BorderStyle.Medium;
hStyle.FillForegroundColor = IndexedColors.Black.Index;
hStyle.FillPattern = FillPattern.SolidForeground;
关于c# - 无法设置 FillBackgroundColor,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27323839/
我在 C# 项目中使用 NPOI 2.0.6.0,但在更改样式时遇到了困难。虽然我可以影响字体和单元格边框,但我无法更改背景颜色。 private void buildSheet(HSSFWorkbo
我是一名优秀的程序员,十分优秀!