C# 无法设置FillBackgroundColor

C# 无法设置FillBackgroundColor,c#,npoi,C#,Npoi,我在一个C#项目中使用npoi2.0.6.0,更改样式时遇到了困难。 虽然我可以影响字体和单元格边框,但我不能更改背景颜色 private void buildSheet(HSSFWorkbook wb, DataTable data, string sheetName) { var cHelp = wb.GetCreationHelper(); var sheet = wb.CreateSheet(sheetName); HSSFFont hFont = (HSSFF

我在一个
C#
项目中使用
npoi2.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;
    }
}
private void buildSheet(HSSF工作簿wb、数据表数据、字符串sheetName)
{
var cHelp=wb.GetCreationHelper();
var sheet=wb.CreateSheet(sheetName);
hssfont hFont=(hssfont)wb.CreateFont();
hFont.Boldweight=(短)fontboldwweight.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(colHeaders中的字符串str)
{
HSSFCell cell=(HSSFCell)headerRow.CreateCell(cellCount);
cell.SetCellValue(cHelp.CreateRichTextString((str));
cell.CellStyle=hStyle;
细胞计数+=1;
}
int rowCount=2;
foreach(data.Rows中的DataRow dr)
{
var row=sheet.CreateRow(rowCount);
对于(int i=1;i
附加调试器后,我注意到hStyle.FillBackgroundColor从不从0x0040更改,尽管黑色的索引是8

因此,基本上问题是:

  • 为什么
    HSSFCellStyle.FillBackgroundColor
    不可修改

A
FillBackgroundcolor
在未指定
FillPattern
的情况下无法应用

i、 e


你的回答很有前景。问题在于背景。我按照你说的做了,得到了黑色的单元格,即使我把背景改成黄色。如果我将FillPattern更改为FillPattern.LeastDots,我只能看到黄色背景,这非常烦人!!还有人去吗clue@GregJF见:
        hStyle = (HSSFCellStyle)workBook.CreateCellStyle();
        hStyle.SetFont(xFont);
        hStyle.BorderBottom = BorderStyle.Medium;
        hStyle.FillForegroundColor = IndexedColors.Black.Index;
        hStyle.FillPattern = FillPattern.SolidForeground;