Add Some Pizazz, or at Least Differentiation, to Various Sections of your Spreadsheet
To spiffify your Excel spreadsheet, you will sometimes want to change the background color of a range of cells, and sometimes the font (text) color, too, from the default black (especially when the background color is dark, you might want to change the font color to white or yellow or something else that will be more legible than black). Here's how.
Note: This assumes that you will declare constants for row and column indexes named COLUMN_HEADING_ROW
, FIRST_COL
, and LAST_COL
, and that "_xlSheet
" is the name of the ExcelSheet (using Microsoft.Interop.Excel
).
First, define the range:
var columnHeadingsRange = _xlSheet.Range[_xlSheet.Cells[COLUMN_HEADING_ROW, FIRST_COL],
_xlSheet.Cells[COLUMN_HEADING_ROW, LAST_COL]];
Then, set the background color of that range:
columnHeadingsRange.Interior.Color = XlRgbColor.rgbSkyBlue;
Finally, set the font color:
columnHeadingsRange.Font.Color = XlRgbColor.rgbWhite;
And here's the code combined:
var columnHeadingsRange = _xlSheet.Range[_xlSheet.Cells[COLUMN_HEADING_ROW, FIRST_COL],
_xlSheet.Cells[COLUMN_HEADING_ROW, LAST_COL]];
columnHeadingsRange.Interior.Color = XlRgbColor.rgbSkyBlue;
columnHeadingsRange.Font.Color = XlRgbColor.rgbWhite;