简体   繁体   English

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

[英]getting interior color of the cell in excel

I need to highlight the cells with wrong data in excel with golden color which I am able to do.我需要在 excel 中用我能做到的金色突出显示错误数据的单元格。 But as soon as the user corrects the data and clicks on validate button the interior color should be revert back to original interior color.但是一旦用户更正数据并单击验证按钮,内部颜色应该恢复为原始内部颜色。 This is not happening.这没有发生。 Please point out the error.请指出错误。 And please suggest exact code because I've tried many thing but nothing has worked so far.请建议确切的代码,因为我尝试了很多东西,但到目前为止没有任何效果。

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

The way to do this is to use the ColorIndex .这样做的方法是使用ColorIndex A full list of values can be found at Adding Color to Excel 2007 Worksheets by Using the ColorIndex Property .可以在使用 ColorIndex 属性将颜色添加到 Excel 2007 工作表中找到完整的值列表。

In the code, just use the index from figure 1 in the link above.在代码中,只需使用上面链接中图 1 中的索引。

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

If you need to compare colors, you can simply compare the ColorIndex value如果需要比较颜色,可以简单的比较ColorIndex

I achieved what I was trying to do.我实现了我想要做的事情。 Here is the solution:这是解决方案:

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

(too large for a comment) (太大,无法评论)
So you want to colour a cell, based on its contents, compared to some other cell's contents.因此,您希望根据单元格的内容与其他单元格的内容相比,为其着色。 In order to achieve that, you have created a macro which you need to launch in order to see its results.为了实现这一点,您创建了一个宏,您需要启动它才能查看其结果。

Instead of that, I'd advise you to use conditional formatting<\/a> .相反,我建议您使用条件格式<\/a>。 It does exactly what you want and there's no need for launching a macro anymore, it all happens immediately.它完全符合您的要求,不再需要启动宏,这一切都会立即发生。

Can you give an example of what you call "wrong data", I might try to find the condition, needed for configuring the conditional formatting.您能否举一个您所谓的“错误数据”的示例,我可能会尝试找到配置条件格式所需的条件。

"

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM