2015-02-09 125 views
0

如何使用epplus條件格式將三個圖標集添加到Excel中的每個單元格中。我用下面的代碼添加三個圖標集:基於規則的Epplus條件格式化

using (ExcelRange scoreRange = workSheet.Cells[1, 2, 1, 10]) 
      { 
       ExcelAddress rangeAddress = new ExcelAddress(scoreRange.Address); 
       var ruleIconSet = workSheet.ConditionalFormatting.AddThreeIconSet(rangeAddress, eExcelconditionalFormatting3IconsSetType.Arrows); // This calculates based on the value in the range 
      } 

我想創建一個規則一樣,如果在一個單元格的值小於0,應該會顯示綠色圖標,如果值大於0 ,應顯示紅色圖標。

什麼應該是可以執行這些東西的規則聲明?

回答

0
for(int j =2; j <=9; j++) //Loop through columns 
{ 
    for(int i = 3; i <= 12; i++) // Loop through rows 
    { 
    // gets only the current cell as range 
    ExcelRange rng = worksheet.Cells[i, j, i, j]; 
    ExcelAddress address = new ExcelAddress(rng.Address); 
    // Get the value of the current cell 
    if(Convert.ToDouble(worksheet.Cells[i, j].Value) >= 4.0) 
    { 
     var v = worksheet.ConditionalFormatting.AddThreeIconSet(address, eExcelconditionalFormatting3IconSetType.Arrows); 
     v.Reverse = true; 
     v.Icon1.Type = eExcelConditionalFormattingValueObjectType.Num; 
    } 
    else if(Convert.ToDouble(workSheet.Cells[i, j].Value) > 1.0 && Convert.ToDouble(workSheet.Cells[i, j].Value) < 4.0) 
    { 

     var v = worksheet.ConditionalFormatting.AddThreeIconSet(address , eExcelconditionalFormatting3IconsSetType.Arrows); 
     v.Icon3.Type = eExcelConditionalFormattingValueObjectType.Num; 

    } 
    else (Convert.ToDouble(workSheet.Cells[i, j].Value) < 1.0) 
    { 
     var v = worksheet.ConditionalFormatting.AddThreeIconSet(address , eExcelconditionalFormatting3IconsSetType.Arrows); 
     v.Icon2.Type = eExcelConditionalFormattingValueObjectType.Num; 
    } 
} 
} 

這適用於我。