NPOI background color does not work

I need to set and remove the background color of the strings. In a way, when I set the style to a string, it doesn't work - the background color is still missing. I tried to set the CellStyle property for a single cell and use HSSFStyle. The result is the same - the background color. Here is the code:

        XSSFCellStyle style = (XSSFCellStyle)_myBook.CreateCellStyle();
        // Define cell style according to input color parameter
        XSSFColor colorToFill;
        switch (color)
        {
            default:
                colorToFill = new XSSFColor(Color.Gray);
                break;
        }
        style.SetFillBackgroundColor(colorToFill);
        for (int i = From; i <= To; i++)
        {
            var row = GetCreateRow(i);
            row.RowStyle = style;
        }

PS The document opens in XSSF format

+4
source share
1 answer

And, each of us had a part of the answer! (Casting for XSSFCellStyle was what I was missing, because ICellStyle has no SetFill methods, so I could not get my own color, only one of the indexed ones ...)

You need to do two things:

  • Call SetFillForegroundColor, not SetFillBackgroundColor.
  • . FillPattern = FillPattern.SolidForeground;

-, Excel , , , , , , FillPattern . SolidForeground - , , , . -, "" " ", "SetBackgroundForegroundColor" / "SetBackgroundBackgroundColor"

+10

All Articles