C# 在excel中获取单元格的内部颜色

C# 在excel中获取单元格的内部颜色,c#,excel,C#,Excel,我需要用金色突出显示excel中有错误数据的单元格,这是我能够做到的。但一旦用户更正数据并单击“验证”按钮,内饰颜色应恢复为原始内饰颜色。这并没有发生。请指出错误。请给出确切的代码,因为我已经尝试了很多方法,但到目前为止都没有效果 private void ValidateButton_Click(object sender, RibbonControlEventArgs e) { bool LeftUntagged = false; Exce

我需要用金色突出显示excel中有错误数据的单元格,这是我能够做到的。但一旦用户更正数据并单击“验证”按钮,内饰颜色应恢复为原始内饰颜色。这并没有发生。请指出错误。请给出确切的代码,因为我已经尝试了很多方法,但到目前为止都没有效果

private void ValidateButton_Click(object sender, RibbonControlEventArgs e)
      {
          bool LeftUntagged = false;
          Excel.Workbook RawExcel = Globals.ThisAddIn.Application.ActiveWorkbook;
          Excel.Worksheet sheet = null;
          Excel.Range matrix = sheet.UsedRange;
          for (int x = 1; x <= matrix.Rows.Count; x++)
          {
              for (int y = 1; y <= matrix.Columns.Count; y++)
              {
                  string CellColor = sheet.Cells[x, y].Interior.Color.ToString();
                  if (sheet.Cells[x, y].Value != null && (Excel.XlRgbColor.rgbGold.Equals(sheet.Cells[x, y].Interior.Color) || Excel.XlRgbColor.rgbWhite.Equals(sheet.Cells[x, y].Interior.Color)))
                  {
                      sheet.Cells[x, y].Interior.Color = Color.Transparent;
                  }
              }
          }
      }
private void ValidateButton_单击(对象发送方,RibbonControlEventArgs e)
{
bool leftuntaged=false;
Excel.Workbook RawExcel=Globals.ThisAddIn.Application.ActiveWorkbook;
Excel.工作表=null;
Excel.Range矩阵=sheet.UsedRange;
对于(int x=1;x尝试:


方法是使用
ColorIndex
。可以在中找到完整的值列表

在代码中,只需在上面的链接中使用图1中的索引

// For example
sheet.Cells[x, y].Interior.ColorIndex = 3; // Set to RED

如果需要比较颜色,只需比较
颜色索引

就可以了。以下是解决方案:

private void ValidateButton_Click(object sender, RibbonControlEventArgs e)
      {
          bool LeftUntagged = false;
          Excel.Workbook RawExcel = Globals.ThisAddIn.Application.ActiveWorkbook;
          Excel.Worksheet sheet = null;
          Excel.Range matrix = sheet.UsedRange;
          for (int x = 1; x <= matrix.Rows.Count; x++)
          {
              for (int y = 1; y <= matrix.Columns.Count; y++)
              {
                  string CellColor = sheet.Cells[x, y].Interior.Color.ToString(); //Here I go double value which is converted to string.
                  if (sheet.Cells[x, y].Value != null && (CellColor == Color.Transparent.ToArgb().ToString() || **CellColor == Excel.XlRgbColor.rgbGold.GetHashCode().ToString()**))
                  {
                      sheet.Cells[x, y].Interior.Color = Color.Transparent;
                  }
              }
          }
      }
private void ValidateButton_单击(对象发送方,RibbonControlEventArgs e)
{
bool leftuntaged=false;
Excel.Workbook RawExcel=Globals.ThisAddIn.Application.ActiveWorkbook;
Excel.工作表=null;
Excel.Range矩阵=sheet.UsedRange;

对于(int x=1;x,但是我应该如何比较颜色?我看不到
单元格[x,y]
之后的
.Interior
。它表示找不到该单元格,或者我缺少汇编指令。
private void ValidateButton_Click(object sender, RibbonControlEventArgs e)
      {
          bool LeftUntagged = false;
          Excel.Workbook RawExcel = Globals.ThisAddIn.Application.ActiveWorkbook;
          Excel.Worksheet sheet = null;
          Excel.Range matrix = sheet.UsedRange;
          for (int x = 1; x <= matrix.Rows.Count; x++)
          {
              for (int y = 1; y <= matrix.Columns.Count; y++)
              {
                  string CellColor = sheet.Cells[x, y].Interior.Color.ToString(); //Here I go double value which is converted to string.
                  if (sheet.Cells[x, y].Value != null && (CellColor == Color.Transparent.ToArgb().ToString() || **CellColor == Excel.XlRgbColor.rgbGold.GetHashCode().ToString()**))
                  {
                      sheet.Cells[x, y].Interior.Color = Color.Transparent;
                  }
              }
          }
      }