2012-11-02 3 views
13

EPPlus의 조건부 서식 기능을 사용하여 일부 범위의 서식을 지정하려고합니다. 나는 많은 문서를 읽었지만 조건부 표현식에 대해서는 언급하지 않았다.EPPlus를 사용한 표현식에 의한 조건부 서식

매우 혼란 스럽습니다. 해당 기능을 사용하는 방법을 모르십시오. 여기 내 몇 가지 질문은 다음과 같습니다

  1. 우리가 매개 변수로 ExcelAddress을 넣어 여러 범위를 사용할 수 있습니다 (같은 "H1 : H17, L1 : L17,"AA1 : AA17 ")
  2. 는 공식은 공식 속성에 투입 어떻게 든 Interop를 Excel과 같은 아닌지?
  3. 당신이 나에게 조건부 서식 표현을 사용하여 작은 데모 코드 다리를 줄 수 (엑셀 우리는 상호 운용성에 서식의 현재 셀 에 대한 표현하기 위해 "A1"을 사용하여 같은).

고맙습니다.

(불쌍한 영어 죄송합니다.)

답변

28

:

여기에 소스 코드를 살펴 보자. 예 코드 제발 : 위 예에서

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 엑셀에서 차이 포인트)하지 시작 않고 표현하는 데 사용되는 셀은 첫 번째 셀은 _formatRangeAddress. (B3).

희망은 다른 사람에게 도움이되기를 바랍니다. -Han-

+0

"포함"텍스트 조건부 서식 지정을 제공 할 수 있습니까? –

+1

Excel 기능입니다. 'ISNUMBER'및 'SEARCH'기능을 사용하고 있습니다.http://office.microsoft.com/en-kr/excel-help/check-if-a-cell-contains-text-HP003056106.aspx – Han

+0

Excel에서 직접 조건부 서식을 사용하고 싶지만 ' 현재 '를 표현합니다. ISBLANK (OFFSET (B3,0, -1), ISBLANK (B3)), 1,0)'식으로 사용하고 코드 주석에서'// WHITE color 이전 셀 또는 현재 셀은 공백입니다. '현재'셀에 대한 참조는 어디에 있습니까? 나는 단지'B3' 만 본다. – jotapdiez

0

많은 달 후에 LINQ와 EPPlus를 사용하여 더 유연하고 빠른 접근 방식을 찾았습니다. 목록에 추가 속성을 추가하여 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 희망이 미래에 누군가를 도와줍니다.