2015-02-09 66 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; 
    } 
} 
} 

这适用于我。