tôi đã tìm ra giải pháp bằng bản thân mình. Hãy lấy một ví dụ mã:
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;
Trong ví dụ trên,
_formatRangeAddress
là phạm vi đó sẽ được áp dụng cho các dạng có điều kiện bởi sự biểu hiện. Ô đầu tiên trong phạm vi này sẽ được sử dụng trong công thức điều kiện. (B3).
_statement
là công thức dùng để tính toán tình trạng này, chuỗi này không bắt đầu với dấu bằng (=
) (chênh lệch điểm từ MS Excel), các tế bào được sử dụng để làm cho biểu thức là ô đầu tiên trong số _formatRangeAddress
. (B3).
Hy vọng điều này hữu ích cho những người khác cần. -Han-
bạn có thể cung cấp định dạng có điều kiện "Chứa" văn bản có điều kiện –
Đây là hàm Excel. Tôi thấy rằng chúng sử dụng 'ISNUMBER' và' SEARCH' funcions.http://office.microsoft.com/en-001/excel-help/check-if-a-cell-contains-text-HP003056106.aspx – Han
Tôi muốn sử dụng Định dạng có điều kiện trực tiếp trên Excel nhưng tôi không rõ ràng ' hiện tại' trên biểu thức của bạn. Bạn sử dụng như biểu thức 'IF (AND (ISBLANK (OFFSET (B3,0, -1)), ISBLANK (B3)), 1,0)' và, trong chú thích mã bạn nói '// điền vào màu WHITE nếu một trong ô trước đó hoặc ô hiện tại là BLANK: '. Tham chiếu tới ô 'hiện tại' ở đâu? Tôi chỉ thấy 'B3'. – jotapdiez