2009-06-09 78 views
4

我剛剛發現了以下頁面:Setting Conditional Formatting in Excel 2007這與我想要做的事很相似,但我似乎找不到適當的功能來做一些稍微不同的事情。Excel Interop條件格式

我想知道是否有人知道一種方法將條件格式應用到範圍,基於一組文本值。例如。我想說:

如果你看到「InvalidValue1」 OR「InvalidValue2」高亮紅色 否則,如果你看到「警告」突出黃色

我有無效值的整個範圍,以及可能的警告值。我還需要爲大型數據集逐列進行此操作,因此在可能的情況下,我想使用內置的Excel功能突出顯示範圍內的錯誤。

有誰知道這是否可能?

問候

回答

5

我相信我設法找到一個解決問題的方法(雖然小區選擇是相當奇怪的,我還沒有完全整理了這一點呢。如我的公式使用A1這實際上意味着,由於C1所選範圍)。

下面是我用別人感興趣的代碼:

string condition = @"=OR(ERROR1, ERROR2, ERROR3)"; 
var cfOR = (FormatCondition)targetSheet.get_Range("C1", "C10").FormatConditions.Add(XlFormatConditionType.xlExpression, Type.Missing,condition), Type.Missing, Type.Missing, Type.Missing, Type.Missing, Type.Missing); 

cfOR.Interior.Color = 0x000000FF; 
cfOR.Font.Bold = true; 
cfOR.Font.Color = 0x00FFFFFF; 

注意,FormatConditions.Add()方法有不同版本的Excel的互操作的不同的簽名。

1

如果你使用.NET 4.0,以下是使用動態和命名參數

dynamic range = sheet.Range("A2").Resize(rowCount, 11); 

const string redCondition = "=OR(ERROR1, ERROR2, ERROR3)"; 

dynamic format = range.FormatConditions.Add(XlFormatConditionType.xlExpression, Formula1: redCondition); 
format.Interior.Color = 0x0000FF; 
format.Font.Color = 0x00FFFF; 
2
using Excel = Microsoft.Office.Interop.Excel; 
... 
object mis = Type.Missing; 

Excel.FormatCondition cond = 
    (Excel.FormatCondition)range.FormatConditions.Add(Excel.XlFormatConditionType.xlCellValue, 
    Excel.XlFormatConditionOperator.xlEqual, "1", 
    mis, mis, mis, mis, mis); 
    cond.Interior.PatternColorIndex = Excel.Constants.xlAutomatic; 
    cond.Interior.TintAndShade = 0; 
    cond.Interior.Color = ColorTranslator.ToWin32(Color.White); 
    cond.StopIfTrue = false; 
+0

注重寫:`ColorTranslator`需要使用`System.Drawing` – 2018-01-21 02:29:50