通过表达式EPPlus格式化条件

时间:2014-09-02 11:08:33

标签: c# asp.net visual-studio-2010 epplus

我正在使用带有条件格式的EPPlus创建excel。我使用C#代码进行条件格式化,但它无法正常工作。

请检查我的以下代码并告诉我错误的地方:

ExcelPackage pck = new ExcelPackage();
var ws = pck.Workbook.Worksheets.Add("Sample1");
var _formatRangeAddress = new ExcelAddress("H16:K31,H33:K44,H46:K57,H59:K69,H71:K73");
string _statement = "=AND(COUNTA(H16:H16)<2,COUNTA(H16:K16)>0)";
var _cond4 = ws.ConditionalFormatting.AddExpression(_formatRangeAddress);
_cond4.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond4.Style.Fill.BackgroundColor.Color = Color.Green;
_cond4.Formula = _statement;
pck.SaveAs(Response.OutputStream);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;  filename=Sample1.xlsx");

2 个答案:

答案 0 :(得分:7)

在开头设置不带=的公式字符串:

string _statement = "AND(COUNTA(H16:H16)<2,COUNTA(H16:K16)>0)";
[...]
_cond4.Formula = _statement;

此处提到了解决方案:Conditional Formatting by Expression using EPPlus

答案 1 :(得分:1)

请试试这个:

ExcelPackage pck = new ExcelPackage();
var ws = pck.Workbook.Worksheets.Add("Sample1");
var _formatRangeAddress = new ExcelAddress("H16:K31,H33:K44,H46:K57,H59:K69,H71:K73");
string _statement = "AND(COUNTA(H16:H16)<2,COUNTA(H16:K16)>0)";
var _cond4 = ws.ConditionalFormatting.AddExpression(_formatRangeAddress);
_cond4.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond4.Style.Fill.BackgroundColor.Color = Color.Green;
_cond4.Formula = _statement;
pck.SaveAs(Response.OutputStream);
Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
Response.AddHeader("content-disposition", "attachment;  filename=Sample1.xlsx");