【发布时间】:2015-04-25 02:52:58
【问题描述】:
我想知道是否可以使用 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();
}
【问题讨论】:
-
那么,您在这里拥有的东西有什么不适合的呢?很抱歉没有看到明确的问题。
-
基本上我有一列可以包含诸如“年假”、“可用”、“病假”、“退休”等数据,并且根据此文本,我想以编程方式更改包含单元格。例如,如果它显示“年假”,则为浅黄色,如果它包含“可用”,则为绿色单元格,依此类推。目前它没有改变颜色
-
那么你所拥有的 ^ 不起作用吗?它在做什么?完全了解想要的结果,但是是什么阻止了您到达那里?
-
我认为它基本上是从数据表中获取价值的。当行包含“年假”值时,我希望它更改单元格颜色。它符合要求,但实际上并没有做任何事情
-
您给它的单元格中的值是第一行中的第五个单元格。您需要迭代查找该值的行,并在找到它时更改单元格地址。