我已经自己找到了解决方案。请举个例子的代码:
ExcelAddress _formatRangeAddress = new ExcelAddress("B3:B10,D3:D10,F3:F10,H3:H10:J3:J10");
// fill WHITE color if previous cell or current cell is BLANK:
// B3 is the current cell because the range _formatRangeAddress starts from B3.
// OFFSET(B3,0,-1) returns the previous cell's value. It's excel function.
string _statement = "IF(OR(ISBLANK(OFFSET(B3,0,-1)),ISBLANK(B3)),1,0)";
var _cond4 = sheet.ConditionalFormatting.AddExpression(_formatRangeAddress);
_cond4.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond4.Style.Fill.BackgroundColor.Color = Color.White;
_cond4.Formula = _statement;
// fill GREEN color if value of the current cell is greater than
// or equals to value of the previous cell
_statement = "IF(OFFSET(B3,0,-1)-B3<=0,1,0)";
var _cond1 = sheet.ConditionalFormatting.AddExpression(_formatRangeAddress);
_cond1.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond1.Style.Fill.BackgroundColor.Color = Color.Green;
_cond1.Formula = _statement;
// fill RED color if value of the current cell is less than
// value of the previous cell
_statement = "IF(OFFSET(B3,0,-1)-B3>0,1,0)";
var _cond3 = sheet.ConditionalFormatting.AddExpression(_formatRangeAddress);
_cond3.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
_cond3.Style.Fill.BackgroundColor.Color = Color.Red;
_cond3.Formula = _statement;
在上述例子中,
_formatRangeAddress
是将被应用于由所述表达 条件格式的范围内。条件公式中将使用此 范围内的第一个单元格。 (B3)。
_statement
是 用于计算条件式,这个字符串确实与等号(=
)(从MS Excel的差异点)不 开始,它是用来使表达 小区是所述第一小区在 _formatRangeAddress
。 (B3)。
希望这有助于其他需要的人。 -Han-
来源
2012-11-05 10:08:56
Han
可以提供“包含”文本条件格式 –
这是Excel函数,我看到他们使用'ISNUMBER'和'SEARCH'函数。http://office.microsoft.com/en-001/excel-help/check-if-a-cell-contains-text-HP003056106.aspx – Han
我想直接在Excel上使用条件格式,但我没有清楚的'当前'在您的表达。你用作表达式IF(AND(ISBLANK(OFFSET(B3,0,-1)),ISBLANK(B3)),1,0)',并且在代码注释中你说'//如果其中一个先前的单元格或当前单元格是空白:'。 “当前”单元格的引用在哪里?我只看到'B3'。 – jotapdiez