Skip to content

Cell Background Color

New Discussion
Rob
Rob asked on Jun 16, 2011 9:13 PM

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?

Sign In to post a reply

Replies

  • 0
    Derek Harmon
    Derek Harmon answered on Sep 30, 2010 4:33 PM

    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,

    • 0
      Ryan
      Ryan answered on Jun 16, 2011 6:20 PM

      It appears FillPatternStyle.Solid in combination with FillPatternForegroundColor does the trick, at least as of v10.3. (not FillPatternBackgroundColor; obvious, right? …) 

      • 0
        Mike Dour
        Mike Dour answered on Jun 16, 2011 9:13 PM

        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.

  • You must be logged in to reply to this topic.
Discussion created by
Favorites
Replies
Created On
Last Post
Discussion created by
Rob
Favorites
0
Replies
3
Created On
Jun 16, 2011
Last Post
14 years, 8 months ago

Suggested Discussions

Created by

Rob

Created on

Jun 16, 2011 9:13 PM

Last activity on

Feb 20, 2026 11:45 AM