简体   繁体   English

更改 Excel 单元格的颜色会引发“'System.__ComObject' 不包含 'Interior' 的定义”

[英]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.当我想更改我的 excel 表的颜色时,我目前正面临着这种情况。 With my code I am already inserting entries into my excel file.使用我的代码,我已经将条目插入到我的 excel 文件中。 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.分析 Google/Stackoverflow 结果,我没有找到解决方案,尽管对此有一些抱怨。

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.我正在使用 Microsoft Office Interop,它非常有用并且完成了它的工作......直到现在。

You have to use the XlRgbColor to implement Color (for Excel interop 14.0):您必须使用 XlRgbColor 来实现 Color(对于 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Microsoft.CSharp.RuntimeBinder.RuntimeBinderException:“System.__ComObject”不包含“语言”的定义 - Microsoft.CSharp.RuntimeBinder.RuntimeBinderException: ''System.__ComObject' does not contain a definition for 'Language'' 访问Excel文件时返回C#System .__ ComObject - C# System.__ComObject returned when accessing Excel file 在 C# 中读取 Excel 文件总是导致 System.__ComObject? - Reading Excel Files In C# Always Results In System.__ComObject? 为什么类型System .__ ComObject声称(有时)不公开? - Why does the type System.__ComObject claim (sometimes) to be public when it is not? System .__ ComObject的动态转换 - Dynamic cast of System.__ComObject 在excel中获取单元格的内部颜色 - getting interior color of the cell in excel 如何将System .__ ComObject转换为字符串? - How to convert System.__ComObject to a string? 将System .__ ComObject强制转换为已知类型的反射 - Casting System.__ComObject to known type reflection 如何将'System .__ ComObject'转换为记录集或数据集? - How to typecast 'System.__ComObject' to recordset or dataset? 在C#中加载COM对象抛出异常“无法将类型为'System .__ ComObject'的COM对象转换为接口类型...”,但C ++或VB没有 - Loading COM object in C# throws exception “Unable to cast COM object of type 'System.__ComObject' to interface type …”, but C++ or VB not
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM