【问题标题】:How to set cell color programmatically epplus?如何以编程方式设置单元格颜色 epplus?
【发布时间】: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();
}

【问题讨论】:

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

标签: c# asp.net epplus


【解决方案1】:

您可以尝试使用 EPPlus 中的条件格式选项

here is their sample

这里是SO post 与使用此选项的其他人的联系

通常使用链接作为答案不是我的风格,但没有理由重新制作这些轮子,如果 EPP 样本消失了,我猜他们也是如此,如果 SO 样本消失了,那么我猜测这个答案也是如此。

希望对你有帮助

【讨论】:

  • 链接的问题是它们停止工作。喜欢你的。
  • 这就是为什么我链接到使用代码但链接本身仍然有效的 SO 答案
【解决方案2】:

检查你的线路:

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

【讨论】:

  • 快到了!给出“范围对样式无效:E0”的错误,因为使用循环它从 0 开始。由于 excel 中的标题和使用从零开始的索引,需要将样式向下移动 2 个单元格
  • @wubblyjuggly 酷。是的,0 vs 1 索引很容易忘记——这就是上面的“+ 1”所做的。很高兴它对你有用。
【解决方案3】:

谢谢厄尼!我稍微改变了它,以允许我在 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);
            }
        }

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-04-02
    相关资源
    最近更新 更多