无法设置FillBackgroundColor
问题描述:
我在C#
项目中使用NPOI 2.0.6.0
,我遇到了更改样式的困难。 虽然我可以影响字体和单元格边框,但我无法更改背景颜色。无法设置FillBackgroundColor
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;
你的答案设置前景。问题是关于BACK的问题。我遵循你所说的并获得了黑色单元格,即使我将“背景”更改为“黄色”。如果将FillPattern更改为FillPattern.LeastDots,我只能看到黄色背景,这非常烦人!任何其他人去线索 – GregJF 2015-05-26 00:47:26
@GregJF请参阅:http://*.com/questions/2803841/setting-foreground-color-for-hssfcellstyle-is-always-coming-out-black – GamerJ5 2015-05-27 02:14:02