2016-12-23 38 views
0

在我的工具中,用戶可以選擇一種配置(通過combobox-> multiple datatable),並且相應的表格將按照以下內容反映在Excel表格中。列(行中的數據將不同),對於所有配置將保持不變的是產品名稱,序列名稱和長度1和總長度。不同的配置會添加諸如長度2,長度3,長度4(用戶將在這些行中添加數據)等列。在Excel中EPPLUS多列配置和條件格式C#

我想在「總長度」列中添加條件格式公式,如果在範圍內(最小值到最大值),背景單元格將變爲綠色,超出範圍時變爲紅色。我堅持使用我的代碼沒有solution.It沒有改變任何顏色時,用戶在Excel中添加數據。幫幫我。謝謝!

enter image description here

 private void ManualFormatExcelandAddRules(ExcelWorksheet WS, DataTable DTtoFormat, int ColStartAddOfDT, int RowStartAddOfDT) 
     { 
      int colCountofDT = DTtoFormat.Columns.Count; 
      int rowCountofDT = DTtoFormat.Rows.Count; 
      double minval = 0; 
      double maxval = 0; 
      int flag = 0; 
      for (int Colno = ColStartAddOfDT; Colno < ColStartAddOfDT + colCountofDT; Colno++) 
      { 
       WS.Cells[RowStartAddOfDT, Colno].Style.Border.BorderAround(ExcelBorderStyle.Thin); 
       for (int RowNo = RowStartAddOfDT + 1; RowNo <= RowStartAddOfDT + rowCountofDT; RowNo++) 
       { if (WS.Cells[RowNo, Colno].Text.Contains("to") && WS.Cells[RowNo, ColStartAddOfDT].Text.Contains("DRAM")) 
         { 
          string[] GuidelineVal = WS.Cells[RowNo, Colno].Text.Split("to".ToArray(), StringSplitOptions.RemoveEmptyEntries).ToArray(); 
          if (GuidelineVal[0].Trim() != "NA" && GuidelineVal[1].Trim() != "NA") 
          { 
           minval = Convert.ToDouble(GuidelineVal[0].Trim()); 
           maxval = Convert.ToDouble(GuidelineVal[1].Trim()); 
           flag = 0; 
          } 
          else 
           flag = 1; 
         } 

         else if (WS.Cells[RowNo, Colno].Text == "" && WS.Cells[RowStartAddOfDT + 1, Colno].Text.Contains("to")) 
         { 


          if (flag == 0) 
          { 

           string _statement = "AND(Convert.ToDouble(WS.Cells[RowNo, Colno].Text) >= minval,Convert.ToDouble(WS.Cells[RowNo, Colno].Text) <= maxval)"; 
           var _cond = WS.ConditionalFormatting.AddExpression(WS.Cells[RowNo, Colno]); 
           _cond.Formula = _statement; 
           _cond.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; 
           _cond.Style.Fill.BackgroundColor.Color = Color.Green;        

          } 
          else 
           WS.Cells[RowNo, Colno].Style.Fill.BackgroundColor.SetColor(Color.Red); 
           WS.Cells[RowNo, Colno].Style.Border.BorderAround(ExcelBorderStyle.Thin); 
        } 
    } 
} 

回答

1

您使用條件格式表達是錯誤/包含語法錯誤/使用不存在的功能,這讓那個,因爲它不Excel將忽略它瞭解它需要做什麼。

看着你的代碼中有4個變量組成的表達:

  • RowNoColNo指示細胞條件formattig適用於
  • minvalmaxval是下限和上限的條件

下面的代碼使用這些變量來建立正確的表達式:

string _statement = string.Format(
    CultureInfo.InvariantCulture, 
    "AND({0}>={1},{0}<={2})", 
    new OfficeOpenXml.ExcelCellAddress(RowNo, ColNo).Address, 
    minval, 
    maxval); 
var _cond = WS.ConditionalFormatting.AddExpression(WS.Cells[RowNo, ColNo]); 
_cond.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; 
_cond.Style.Fill.BackgroundColor.Color = Color.Green; 

_cond.Formula = _statement; 

請注意,該表達式僅使用有效的Excel函數。你不能混入.Net語句,如Convert.ToDouble。使用InvariantCulture進行數字轉換也很重要,否則分隔符可能會被解釋爲函數中的額外參數。

當你調試這個_statement將包含這個:AND(A2>=40.2,A2<=44.5),並且當應用到A2單元時,它的工作原理與所宣稱的一樣。

+0

謝謝!有用!:) – juitanis