2015-02-23 129 views
15

我想知道是否可以使用epplus以編程方式設置單元格顏色?如何以編程方式設置單元格顏色epplus?

我從sql存儲過程加載我的數據,它運行良好,但我的用戶希望 包含單詞'年假'的單元格具有淡黃色而不是默認白色的背景顏色。有沒有辦法做到這一點?也許通過迭代數據表可能?下面是其中

public void ExportTableData(DataTable dtdata) 
{ 
    //Using EPPLUS to export Spreadsheets 
    ExcelPackage pck = new ExcelPackage(); 
    var ws = pck.Workbook.Worksheets.Add("Availability list"); 

    ws.Cells["A1"].LoadFromDataTable(dtdata, true); 

    ws.Cells["A1:G1"].Style.Font.Bold = true; 
    ws.Cells["A1:G1"].Style.Font.UnderLine = true; 

    //change cell color depending on the text input from stored proc? 
    if (dtdata.Rows[4].ToString() == "Annual Leave") 
    { 
     ws.Cells["E1"].Style.Fill.PatternType = ExcelFillStyle.Solid; 
     ws.Cells["E1"].Style.Fill.BackgroundColor.SetColor(Color.LightYellow); 
    } 

    pck.SaveAs(Response.OutputStream); 
    Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; 
    Response.AddHeader("content-disposition", "attachment; filename=Availability.xlsx"); 
    Response.End(); 
} 
+0

那麼,什麼不與你在這裏工作?抱歉沒有看到明確的問題。 – workabyte 2015-02-23 17:29:22

+0

基本上我有一列可以包含「年假」,「可用」,「生病」,「退休」等數據,並根據此文本我想以編程方式更改包含單元格的顏色。例如,如果表示「年假」,則爲淡黃色,只要包含「可用」等等,則表示綠色單元格。目前它沒有改變顏色 – wubblyjuggly 2015-02-23 17:32:11

+0

所以你有什麼^不工作?它在做什麼?完全理解期望的結果,但是阻止你到達那裏的是什麼? – workabyte 2015-02-23 17:33:24

回答

18

檢查線路:

if (dtdata.Rows[4].ToString() == "Annual Leave") 

如果它是一個標準的.NET表難道不.ToString()評估爲"System.Data.DataRow"?在循環行計數後(基本上是krillgar說的),還需要爲每個單元格調整ws.Cells["E1"]

類似的東西:

[TestMethod] 
public void Cell_Color_Background_Test() 
{ 
    //http://stackoverflow.com/questions/28679602/how-to-set-cell-color-programmatically-epplus 

    //Throw in some data 
    var dtdata = new DataTable("tblData"); 
    dtdata.Columns.Add(new DataColumn("Col1", typeof(string))); 
    dtdata.Columns.Add(new DataColumn("Col2", typeof(int))); 
    dtdata.Columns.Add(new DataColumn("Col3", typeof(int))); 

    for (var i = 0; i < 20; i++) 
    { 
     var row = dtdata.NewRow(); 
     row["Col1"] = "Available"; 
     row["Col2"] = i * 10; 
     row["Col3"] = i * 100; 
     dtdata.Rows.Add(row); 
    } 
    //throw in one cell that triggers 
    dtdata.Rows[10]["Col1"] = "Annual leave"; 

    var existingFile = new FileInfo(@"c:\temp\temp.xlsx"); 
    if (existingFile.Exists) 
     existingFile.Delete(); 

    using (var pck = new ExcelPackage(existingFile)) 
    { 
     //Using EPPLUS to export Spreadsheets 
     var ws = pck.Workbook.Worksheets.Add("Availability list"); 

     ws.Cells["A1"].LoadFromDataTable(dtdata, true); 

     ws.Cells["A1:G1"].Style.Font.Bold = true; 
     ws.Cells["A1:G1"].Style.Font.UnderLine = true; 

     //change cell color depending on the text input from stored proc? 
     //if (dtdata.Rows[4].ToString() == "Annual Leave") 
     for (var i = 0; i < dtdata.Rows.Count; i++) 
     { 
      if (dtdata.Rows[i]["Col1"].ToString() == "Annual leave") 
      { 
       ws.Cells[i + 1, 1].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; 
       ws.Cells[i + 1, 1].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightYellow); 
      } 
     } 

     pck.Save(); 
    } 
+0

差不多有!給出「範圍對樣式無效:E0」的錯誤,因爲它使用從0開始的循環。需要將樣式向下移動2個單元,這是由於excel中的標題並使用基於零的索引 – wubblyjuggly 2015-02-24 09:49:48

+0

太棒了它略見下文。乾杯! – wubblyjuggly 2015-02-24 09:58:22

+0

@wubblyjuggly酷。是的,0對1指數很容易被忽略 - 這就是'+ 1'所做的。很高興它適合你。 – Ernie 2015-02-24 13:11:48

0

你可以嘗試使用條件格式選項EPPlus

here is their sample

,這裏是與別人一SO post誰使用這個選項

一般使用links答案不是我的風格,但沒有理由重新制作這些輪子,如果EPP樣品消失了,那麼他們也是如此,如果SO樣品沒有了,那麼我就用這個答案猜測。

希望它可以幫助

2

感謝厄尼!我稍微改了一下,以便在excel中使用我的頭文件,並且還要確保代碼不會從E1開始。我用ws.cells [i + 2,5]來做到這一點。乾杯!

for (var i = 0; i < dtdata.Rows.Count; i++) 
     { 

      if (dtdata.Rows[i]["typeName"].ToString() == "Annual Leave") 
      { 
       ws.Cells[i + 2, 5].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; 
       ws.Cells[i + 2, 5].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightYellow); 
      } 

      else if (dtdata.Rows[i]["typeName"].ToString() == "Available") 
      { 
       ws.Cells[i + 2, 5].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; 
       ws.Cells[i + 2, 5].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.LightGreen); 
      } 
      else 
      { 
       ws.Cells[i + 2, 5].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; 
       ws.Cells[i + 2, 5].Style.Fill.BackgroundColor.SetColor(System.Drawing.Color.White); 
      } 
     }