1

For the values i=4 and i=6, following will apply background color as LightGrey to the entire 4th and 6th rows. Question: Are there any ways so we change the Background color (or any style for that matter) of a row within certain range, say, all the rows from 1st column to the 10th column?

int i;
ExcelRow rowRange = ws.Row(i);
ExcelFill RowFill = rowRange.Style.Fill;
RowFill.PatternType = ExcelFillStyle.Solid;
RowFill.BackgroundColor.SetColor(System.Drawing.Color.LightGray);
Tseng
  • 61,549
  • 15
  • 193
  • 205
nam
  • 21,967
  • 37
  • 158
  • 332
  • 1
    Well you need to start with getting a reference to a range that only points to the cells you want. Doing `ws.Row(i)` isn't going to get you that, because that will be the entire row. – mason Jun 07 '17 at 20:20
  • @mason I tried with `ExcelRange ExlRange = ws.Cells[1, 1, 50, 10];` (covering first 50 rows and 10 columns) but `ExcelRange` does not seem to have `ExcelRow` or `ExcelColumn` so the above code did not work for `ExlRange`. Any other suggestions? – nam Jun 07 '17 at 20:30
  • Did you look at my answer? – mason Jun 07 '17 at 20:34

1 Answers1

1

Instead of using ws.Row(rowNumber) use

var firstColumn =  1;
var lastColumn  = 10;
var rowRange    = ws.Cells[rowNumber, firstColumn, rowNumber, lastColumn];
//now do styling on rowRange

rowRange will contain references to all the cells in the rectangle defined by the values you passed to it.

mason
  • 31,774
  • 10
  • 77
  • 121
  • Line `ExcelFill rowFill = ws.Cells[........]` gives the error `cannot implicitly convert ExcelRange to ExcelFill`. I can't use `var` since we need to have `RowFill.PatternType` property set first in order to use `RowFill.BackgroundColor` – nam Jun 07 '17 at 20:40
  • Look at my edited answer. I was using different variable names than you were. – mason Jun 07 '17 at 20:41