Change cell color in Excel using C #

I am using a windows application to export a data table to Excel. It works. Now I want to give a color to a specific text in a cell. How should I do it?

+46
c # excel ms-office
Mar 16 '10 at 6:03
source share
2 answers

Text:

[RangeObject].Font.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 

For cell background

 [RangeObject].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Red); 
+103
Mar 16 '10 at 6:39
source share

Note. It is assumed that you will declare constants for row and column indices named COLUMN_HEADING_ROW , FIRST_COL and LAST_COL , and _xlSheet is the name ExcelSheet (using Microsoft.Interop.Excel )

First determine 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 this range:

 columnHeadingsRange.Interior.Color = XlRgbColor.rgbSkyBlue; 

Finally, set the font color:

 columnHeadingsRange.Font.Color = XlRgbColor.rgbWhite; 

And here is 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; 
+7
Nov 15 '15 at 1:21
source share



All Articles