This project has moved and is read-only. For the latest updates, please go here.

Add Great Than Condition Format

Feb 18, 2013 at 1:51 AM

I'm trying to add great than condition format to Excel. However, I cannot find any function that can set the rule (e.g. set Red color for all value great than 1000). How can I set the rule?

My coding is following:
            ExcelAddress formatAddress = new ExcelAddress("A1");
            OfficeOpenXml.ConditionalFormatting.Contracts.IExcelConditionalFormattingRule conditionalFormat = ws.ConditionalFormatting[ws.ConditionalFormatting.Count - 1];
            conditionalFormat.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
            conditionalFormat.Style.Fill.BackgroundColor.Color = System.Drawing.Color.Red;
Mar 19, 2013 at 8:30 PM
All you need to do is assign the result of AddGreaterThan to a variable, then set its values. Here's how I do a NotBetween condition:
ExcelAddress addr = new ExcelAddress(myrange.Address);
IExcelConditionalFormattingNotBetween cond = sheet.ConditionalFormatting.AddNotBetween(addr);
String first_cell = sheet.Cells[y, x].Address;
cond.Formula = "IF(" + first_cell + "<-1,1,0)";
cond.Formula2 = "IF(" + first_cell + ">1,1,0)";
Then modify cond.Style to suit your needs.

Hope this helps,