简体   繁体   中英

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. 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 . A full list of values can be found at Adding Color to Excel 2007 Worksheets by Using the ColorIndex Property .

In the code, just use the index from figure 1 in the link above.

// 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

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.

It does exactly what you want and there's no need for launching a macro anymore, it all happens immediately.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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