programing

EPLUS를 사용한 식에 의한 조건부 포맷

telecom 2023. 6. 9. 21:45
반응형

EPLUS를 사용한 식에 의한 조건부 포맷

EPLUS의 조건부 포맷 기능을 사용하여 범위를 포맷하려고 합니다.많은 문서를 읽었지만 조건부 서식 표현식에 대한 언급은 없습니다.

저는 매우 혼란스럽습니다.그 기능을 사용하는 방법을 모릅니다.몇 가지 질문이 있습니다.

  1. 여러 범위를 사용하여 매개 변수 ExcelAddress(예: "H1:H17,L1:L17,AA1:AA17")
  2. 수식이 포뮬러 속성에 입력되는 것은 어떻게든 인터럽트 엑셀과 같은 것입니까, 아닌가요?(interop excel에서 포맷을 위해 현재 셀에 대해 "A1"을 사용하는 것처럼)
  3. 조건부 서식 표현식을 사용하는 작은 데모 코드 레그를 줄 수 있습니까?

감사해요!

(제가 쓴 영어가 서툴러서 죄송합니다)

저는 혼자서 해결책을 찾았습니다.예제 코드를 사용하십시오.

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).

이것이 필요한 사람들에게 도움이 되길 바랍니다.

EPLUS 3.1 베타 버전에서는 조건부 포맷이 지원됩니다.

여기서 소스 코드를 확인하십시오. http://epplus.codeplex.com/discussions/348196/

여러 달이 지난 후 LINQ와 EP Plus를 사용하여 훨씬 더 유연하고 빠른 접근 방식을 찾았습니다.목록에 추가 속성을 추가하여 Excel 행 번호를 저장한 다음 LINQ를 사용하여 셀 주소를 검색하기만 하면 됩니다.이 경우 다음과 같습니다.

string sRng = string.Join(",", YourModel.Where(f => f.YourField == null)
    .Select(a => "H" + a.iRow + ",L" + a.iRow + ",AA" + a.iRow)); // this address could be many pages and it works

if (sRng.Length > 0) {
    ws.Cells[sRng].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.Green); 
}

다음은 전체 기사입니다.

https://www.codeproject.com/Tips/1231992/Conditional-Formatting-in-Excel-with-LINQ-and-EPPl

또 다른 예를 보십시오. https://stackoverflow.com/a/49022692/8216122 는 이것이 미래의 누군가에게 도움이 되기를 바랍니다.

언급URL : https://stackoverflow.com/questions/13196762/conditional-formatting-by-expression-using-epplus

반응형