I'm creating an Excel workbook from scratch in a web application. I'm using the Workbook and Worksheet classes. My goal is to make every other row with an alternating background color so that the spreadsheet can be read.
I've tried using the following code:
...
Worksheet pipelineWorksheet = workbook.Worksheets[0]; if (rowIndex % 2 == 0){ pipelineWorksheet.GetCell("A" + rowIndex).CellFormat.FillPatternBackgroundColor = System.Drawing.Color.Cornsilk;
}
This gives me alternating rows, but all of the background colors are black! I want to accomplish the same effect as if I opened Excel, selected a cell, and used the fill command on the background color. How should I be doing this?
Actually, you only need to set the FillPatternForegroundColor to a non-empty value and the FillPatternStyle will default to Solid. I know the naming is a little strange, but the FillPatternBackgroundColor is only used when you have some sort of pattern filling the cell and that color is then the alternate color for the pattern.
It appears FillPatternStyle.Solid in combination with FillPatternForegroundColor does the trick, at least as of v10.3. (not FillPatternBackgroundColor; obvious, right? ...)
I think each CellFormat must have a solid FillPatternStyle, which would cause all of its foreground pixels (i.e., black pixels) to be turned "on" and so they cover-up whatever background fill color is being used.
Try something like the following code to see if it works for you this way,
IWorksheetCellFormat cf = pipelineWorksheet.GetCell("A" + rowIndex).CellFormat;cf.FillPatternBackgroundColor = System.Drawing.Color.Cornsilk;cf.FillPatternStyle = FillPatternStyle.None;
Regards,