2015-02-23 130 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); 
      } 
     }