簡體   English   中英

如何使用Excel COM Interop以編程方式在帶有數字的單元格區域上應用條件格式?

[英]How to programmatically apply conditional formatting on a cell range with numbers using Excel COM Interop?

在MS Excel中,您可以選擇一系列單元格並在此范圍上應用條件格式。 是否有使用Microsoft.Office.Interop.Excel在C#中執行此操作的方法?

給定一系列包含數字的單元格,我需要應用Red-Yellow-Green color scale 如果沒有這方法,有沒有人根據數字范圍和單元格中的數字知道所應用顏色的公式?

excel中的命令

產量

根據Don評論 ,Microsoft提供了C#和VB中關於如何使用Microsoft.Office.Interop.Excel在Excel中進行條件格式化的完整示例:在給定范圍內,使用.FormatConditions.AddColorScale()獲取顏色,或.FormatConditions.AddIconSetCondition()用於圖標集條件格式

根據SO指南,如果鏈接消失,這是應用顏色格式的本質,從該鏈接獲取:

// Fill cells A1:A10 with sample data.
targetSheet.get_Range("A1",
paramMissing).set_Value(XlRangeValueDataType.xlRangeValueDefault, 1);
targetSheet.get_Range("A2", paramMissing).set_Value(XlRangeValueDataType.xlRangeValueDefault, 2);
targetSheet.get_Range("A1:A2",
paramMissing).AutoFill(targetSheet.get_Range("A1:A10", paramMissing), XlAutoFillType.xlFillSeries);

// Create a two-color ColorScale object for the created sample data
// range.
cfColorScale = (ColorScale)(targetSheet.get_Range("A1:A10",
    Type.Missing).FormatConditions.AddColorScale(2));

// Set the minimum threshold to red (0x000000FF) and maximum threshold
// to blue (0x00FF0000). Values are in 00BBGGRR format.
cfColorScale.ColorScaleCriteria[1].FormatColor.Color = 0x000000FF;
cfColorScale.ColorScaleCriteria[2].FormatColor.Color = 0x00FF0000;

重要說明 :通過COM與Excel使用的顏色值要求顏色為00BBGGRR格式(第一個字節始終為零)。 默認情況下,.NET在System.Drawing.Color類中使用AARRGGBB ,因此不能直接使用這些顏色 (作為助記符 ,COM顏色按字母順序排列:藍色,綠色,紅色)。

與.NET中的任何Excel互操作一樣,您需要引用Excel 12.0對象庫並導入Microsoft.Office.Interop.Excel命名空間。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

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