![](/img/trans.png)
[英]Changing color of Excel cell throws "'System.__ComObject' does not contain a definition for 'Interior'"
[英]getting interior color of the cell in excel
我需要在 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;
}
}
}
}
尝试:
sheet.Cells[x, y].Interior.ColorIndex = -4142; //xlNone
这样做的方法是使用ColorIndex
。 可以在使用 ColorIndex 属性将颜色添加到 Excel 2007 工作表中找到完整的值列表。
在代码中,只需使用上面链接中图 1 中的索引。
// For example
sheet.Cells[x, y].Interior.ColorIndex = 3; // Set to RED
如果需要比较颜色,可以简单的比较ColorIndex
值
我实现了我想要做的事情。 这是解决方案:
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;
}
}
}
}
(太大,无法评论)
因此,您希望根据单元格的内容与其他单元格的内容相比,为其着色。 为了实现这一点,您创建了一个宏,您需要启动它才能查看其结果。
相反,我建议您使用条件格式<\/a>。 它完全符合您的要求,不再需要启动宏,这一切都会立即发生。
您能否举一个您所谓的“错误数据”的示例,我可能会尝试找到配置条件格式所需的条件。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.