在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; } } } }
- Excel的AddOLEObject函数不能在窗口2008 R2上工作
- 如何从Excel中读取包含C#中函数的单元格值
- 如何创build读取自定义数据的Excel加载项?
- 关于Excel工作簿在C#中打开问题的说明
- 类库中的System.NullReferenceException
- C#Excel Interop Shapes.AddPicture图像大小不正确第二次尝试导出到Excel文件后
- 如果Excel未打开,不能更新Excel文件?
- 如何从Microsoft.Office.Interop.Excel.Range对象的“value”属性中获取dynamictypesvariables的值
- 如何使用.NET读取Excel文件(.xls)的二进制数据?