Click here to Skip to main content
15,887,135 members
Articles / Productivity Apps and Services / Microsoft Office
Tip/Trick

How to Set the Background and Foreground (Font Text) Colors on an Excel Range

Rate me:
Please Sign up or sign in to vote.
0.00/5 (No votes)
14 Nov 2015CPOL 9.5K   2  
Set the cell and font colors to an Excel Range in C# in three lines of code

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:

C#
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:

C#
columnHeadingsRange.Interior.Color = XlRgbColor.rgbSkyBlue;

Finally, set the font color:

C#
columnHeadingsRange.Font.Color = XlRgbColor.rgbWhite;

And here's the code combined:

C#
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;

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Founder Across Time & Space
United States United States
I am in the process of morphing from a software developer into a portrayer of Mark Twain. My monologue (or one-man play, entitled "The Adventures of Mark Twain: As Told By Himself" and set in 1896) features Twain giving an overview of his life up till then. The performance includes the relating of interesting experiences and humorous anecdotes from Twain's boyhood and youth, his time as a riverboat pilot, his wild and woolly adventures in the Territory of Nevada and California, and experiences as a writer and world traveler, including recollections of meetings with many of the famous and powerful of the 19th century - royalty, business magnates, fellow authors, as well as intimate glimpses into his home life (his parents, siblings, wife, and children).

Peripatetic and picaresque, I have lived in eight states; specifically, besides my native California (where I was born and where I now again reside) in chronological order: New York, Montana, Alaska, Oklahoma, Wisconsin, Idaho, and Missouri.

I am also a writer of both fiction (for which I use a nom de plume, "Blackbird Crow Raven", as a nod to my Native American heritage - I am "½ Cowboy, ½ Indian") and nonfiction, including a two-volume social and cultural history of the U.S. which covers important events from 1620-2006: http://www.lulu.com/spotlight/blackbirdcraven

Comments and Discussions

 
-- There are no messages in this forum --