在Excel中获取单元格的内部颜色

我需要突出显示与我能够做的金黄颜色excel中的错误数据的单元格。 但只要用户纠正数据,点击确认button,内部颜色应该恢复到原来的内部颜色。 这没有发生。 请指出错误。 而且请build议准确的代码,因为我已经尝试了很多东西,但迄今没有任何工作。

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; } } } } 

尝试:

 sheet.Cells[x, y].Interior.ColorIndex = -4142; //xlNone 

做到这一点的方法是使用ColorIndex 。 通过使用ColorIndex属性 ,可以在将颜色添加到Excel 2007工作表中find完整的值列表。

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

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

如果您需要比较颜色,则可以简单比较ColorIndex

我达到了我想要做的。 这是解决scheme:

 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; } } } }