I want to use conditional formatting feature of excel 2007 programmatically. I have following scenario.
I have 6 columns of numbers
A B C D E F
100 25 25 15 20 50
....
if (C1/A1)*100 >= B1
I have to color it as red. The same rule applies to D1, E1,F1 columns.
I saw conditional formatting feature in excel 2007. But I want some pointers on how to implement it in C# code.
I'm going to assume that you're happy to use Interop, since you didn't say otherwise. Here's what I use to set conditional formatting in Excel. It assumes you already have your Worksheet
referenced in a variable called xlWorksheet
.
// Create a FormatCondition and add it to the specified range of cells, using the
// passed-in condition and cell colour
Excel.Range range = xlWorksheet.get_Range("C1", "C1");
Excel.FormatConditions fcs = range.FormatConditions;
Excel.FormatCondition fc = (Excel.FormatCondition)fcs.Add
(Excel.XlFormatConditionType.xlExpression, Type.Missing, "=IF($C$1/$A$1)*100 >= $B$1");
Excel.Interior interior = fc.Interior;
interior.Color = ColorTranslator.ToOle(Color.Red);
interior = null;
fc = null;
fcs = null;
range = null;
I've guessed a bit at your intended usage but you can fiddle with it to get the formulae and cell references right.
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.