应用条件格式后 EPPlus Excel 无法打开

EPPlus Excel not opening after applying conditional formatting

我正在使用 EPPlus 生成 excel 并且它正在打开,但是当我应用条件格式时 excel 生成速度变慢并且文件在我的电脑中打开但在另一台电脑中没有打开。

条件格式正确的字体颜色即将到来。 我的 objective 条件格式是,如果单元格值 >-1,则颜色将为绿色,如果单元格值 <0,则颜色将为红色。我从 google 搜索条件格式得到的代码在我的电脑中打开 excel 文件时工作正常但 在另一台电脑中打开相同的 excel 时出现错误即将用于条件格式。

在这里我分享我的条件格式代码。请看看并告诉我它是否正确应用或代码不正确。

#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

如果上面的代码不符合我的要求,那么请建议我在上面的代码中更改什么。

还有一点不清楚 excel 文件正在我的电脑中打开,但 抛出有关条件格式的错误 当尝试在另一台电脑中打开时。 excel 两台电脑的版本差不多。

谢谢

您可以通过这种方式添加条件格式。

string _StartPeriod = "", _EndPeriod = "";
                //_EarningID_Periods
                for (int p = 0; p <= _EarningID_Periods.Count - 1; p++)
                {
                    Period = _EarningID_Periods[p].NewPeriod.Replace("A", "").Replace("E", ""); //ds.Tables[1].Rows[p]["old_Periods"].ToString().Replace("A", "").Replace("E", "");

                    _StartPeriod = listOfCell.Where(a => a.EarningsType == "NEW"
                    && a.PeriodType == "DELTA_PERCENTAGE_PERIOD" && a.Period.Replace("A", "").Replace("E", "") == Period
                    ).FirstOrDefault().CoorDinate + "4";

                    _EndPeriod = listOfCell.Where(a => a.EarningsType == "NEW"
                    && a.PeriodType == "DELTA_ABSOLUTE" && a.Period.Replace("A", "").Replace("E", "") == Period
                    ).FirstOrDefault().CoorDinate + row.ToString();

                    if (_StartPeriod != "" && _EndPeriod != "")
                    {
                        ExcelAddress formatRangeAddress = new ExcelAddress(_StartPeriod + ":" + _EndPeriod);
                        var cond1 = ws.ConditionalFormatting.AddLessThan(formatRangeAddress);
                        cond1.Style.Font.Color.Color = CSMUtils._RedColor;
                        cond1.Formula = "0";

                        formatRangeAddress = new ExcelAddress(_StartPeriod + ":" + _EndPeriod);
                        var cond2 = ws.ConditionalFormatting.AddGreaterThan(formatRangeAddress);
                        cond2.Style.Font.Color.Color = CSMUtils._GreenColor; //CSMUtils.SetRGBColor(0, 97, 0);
                        cond2.Formula = "0";
                    }
                }