EPPlus: Excel not opening after applying conditional formatting

T.Zacks 3,986 Reputation points
2021-02-27T13:07:50.533+00:00

I am generating excel with EPPlus and it was opening but the moment i applied condition formatting excel generation slow and file is opening in my pc but not opening in another pc.

For conditional formatting right font color is coming. my objective of conditional formatting is if cell value >-1 then color will be Green and if cell value is <0 then color will be red. The code i got from google search for conditional formatting which is working fine when i open excel file in my pc but when same excel open in another pc there error is coming for conditional formatting.

Here i am sharing my conditional formatting code. please have a look and tell me does it applied properly or code is not right one ?

#region Conditional Formatting
address = new ExcelAddress(AvgPeriod3 + row.ToString());
_statement1 = "=AND($" + address + ">-1)";
condition = ws.ConditionalFormatting.AddExpression(address);
condition.Formula = _statement1;
condition.Style.Font.Color.Color = System.Drawing.Color.Green;

address = new ExcelAddress(AvgPeriod3 + row.ToString());
_statement1 = "=AND($" + address + "<0)";
condition = ws.ConditionalFormatting.AddExpression(address);
condition.Formula = _statement1;
condition.Style.Font.Color.Color = System.Drawing.Color.Red;
#endregion

if the above code is not right one for my requirement then please suggest me what to change in the above code.

another things not clear that excel file is opening in my pc but throwing error regarding conditional formatting when try to open in another pc. excel version is more less same or close in two pc.

Thanks

C#
C#
An object-oriented and type-safe programming language that has its roots in the C family of languages and includes support for component-oriented programming.
10,534 questions
{count} votes

Accepted answer
  1. T.Zacks 3,986 Reputation points
    2021-03-13T07:15:35.163+00:00

    I have figured out right conditional formatting. here sharing which worked.

                        ExcelAddress formatRangeAddress = new ExcelAddress(_StartPeriod + ":" + _EndPeriod);
                        var cond1 = ws.ConditionalFormatting.AddLessThan(formatRangeAddress);
                        cond1.Style.Font.Color.Color = CSMUtils.SetRGBColor(156, 0, 0);
                        cond1.Formula = "0";
    
                        formatRangeAddress = new ExcelAddress(_StartPeriod + ":" + _EndPeriod);
                        var cond2 = ws.ConditionalFormatting.AddGreaterThan(formatRangeAddress);
                        cond2.Style.Font.Color.Color = CSMUtils.SetRGBColor(0,97,0);
                        cond2.Formula = "0";
    
    public static System.Drawing.Color SetRGBColor(int r, int g, int b)
    {
         return System.Drawing.Color.FromArgb(255, r, g, b);
    }
    
    1 person found this answer helpful.

0 additional answers

Sort by: Most helpful