簡體   English   中英

在excel中獲取單元格的內部顏色

[英]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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM