简体   繁体   中英

Changing color of Excel cell throws "'System.__ComObject' does not contain a definition for 'Interior'"

I am currently facing when I want to change the color of my excel sheet. With my code I am already inserting entries into my excel file. Some specific cells should receive a special color.

When I run my code, I always get the same error.

Analyzing the Google/Stackoverflow results, I did not find a solution, even though there have been some complaints about this.

Workbooks wbs = excel.Workbooks;
Workbook sheet = wbs.Open(fileName);
excel.DisplayAlerts = false;
Worksheet y = sheet.ActiveSheet;
y.Copy(y, Type.Missing);
int index = y.Index;
int addRow = 2;
Worksheet x = (Worksheet)excel.Worksheets[index];
//...
//this line throws the error
x.Cells[addRow++, 1].Interior.Color = System.Drawing.Color.Blue;
//...

I am using Microsoft Office Interop which was very useful and did its job...until now.

You have to use the XlRgbColor to implement Color (for Excel interop 14.0):

x.Cells[addRow++, 1].Interior.Color = XlRgbColor.xlBlue;  

if you have older version: you have to use translator

x.Cells[addRow++, 1].Interior.Color = System.Drawing.ColorTranslator.ToOle(System.Drawing.Color.Silver

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM