c# - 如何以编程方式设置单元格颜色 epplus?

标签 c# asp.net 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();
}

最佳答案

检查你的线路:

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

如果它是一个标准的 .net 表,.ToString() 不会计算为 "System.Data.DataRow" 吗?此外,ws.Cells["E1"] 还需要在遍历行计数后针对每个单元格进行调整(基本上是 krillgar 所说的)。

类似的东西:

[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();
    }

关于c# - 如何以编程方式设置单元格颜色 epplus?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28679602/

相关文章:

c# - 如何使用 css 或 javascript 将 ASP 下拉列表设置为只读?

c# Excelpackage 将单元格范围读取到数据表中

c# - 有没有什么方法可以像在 C# 中那样使用不变区域性在 Excel 中格式化日期单元格

c# - 是否有快速且可扩展的解决方案来保存数据?

C# 两个类和两个表链接

c# - 泛型类型参数和类型

c# - 获取序列化异常 : '<>f__AnonymousType2` is not marked as serializable

c# - 创建具有特定日期格式的 Datetime 对象

c# - Entity Framework 中的导航属性是什么

excel - 如何使用 EPPlus 设置 Excel 页边距