c# - Interior.Color 不工作

c# - Interior.Color not working

using Excel = Microsoft.Office.Interop.Excel;
using Microsoft.Office.Interop.Excel;

代码

oSheet = (Excel.Worksheet)oWB.ActiveSheet;
oSheet.Name = "Data";
oSheet.Cells[1, 1].Interior.Color = 4;

我的目标是获取单元格值,需要将背景颜色设置为颜色索引 4。

示例。

如果单元格[5,2] 包含文本为"NO",需要设置背景颜色为彩色。 索引 4.

但我收到错误

does not contain a definition for 'Interior' and no extension method 'Interior' accepting a first argument of type 'object' could be found

我自己还没有测试过,但如果你想使用 Color,你必须使用 RGB 值。但是要小心,因为 B 和 R 是互换的,所以你必须使用 B G R 来设置你的 Color.

如果您想使用颜色索引 4,请改用 ColorIndex

oSheet.Cells[1, 1].Interior.ColorIndex = 4;