如何以编程方式设置单元格颜色 epplus?
How to set cell color programmatically epplus?
我想知道是否可以使用 epplus 以编程方式设置单元格颜色?
我从 sql 存储过程加载我的数据并且它运行良好,但我的用户想要
包含单词 'Annual Leave' 的单元格的背景颜色为浅黄色,而不是默认的白色。有没有办法做到这一点?也许通过遍历数据表?下面是哪里
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();
}
您可以尝试使用 EPPlus 中的条件格式选项
这里是 SO post 与使用此选项的其他人
通常使用链接作为答案不是我的风格,但没有理由重新制作这些轮子,如果 EPP 样本消失了,我猜他们也是,如果 SO 样本消失了,那么我猜这个答案也是如此。
希望对您有所帮助
检查你的线路:
if (dtdata.Rows[4].ToString() == "Annual Leave")
如果它是标准的 .net table 不会 .ToString()
评估为 "System.Data.DataRow"
?此外 ws.Cells["E1"]
需要在遍历行计数后针对每个单元格进行调整(基本上是 krillgar 所说的)。
类似的东西:
[TestMethod]
public void Cell_Color_Background_Test()
{
//
//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();
}
谢谢厄尼!我稍微更改了它以允许 excel 中的 header 并确保代码不从 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);
}
}
我想知道是否可以使用 epplus 以编程方式设置单元格颜色?
我从 sql 存储过程加载我的数据并且它运行良好,但我的用户想要 包含单词 'Annual Leave' 的单元格的背景颜色为浅黄色,而不是默认的白色。有没有办法做到这一点?也许通过遍历数据表?下面是哪里
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();
}
您可以尝试使用 EPPlus 中的条件格式选项
这里是 SO post 与使用此选项的其他人
通常使用链接作为答案不是我的风格,但没有理由重新制作这些轮子,如果 EPP 样本消失了,我猜他们也是,如果 SO 样本消失了,那么我猜这个答案也是如此。
希望对您有所帮助
检查你的线路:
if (dtdata.Rows[4].ToString() == "Annual Leave")
如果它是标准的 .net table 不会 .ToString()
评估为 "System.Data.DataRow"
?此外 ws.Cells["E1"]
需要在遍历行计数后针对每个单元格进行调整(基本上是 krillgar 所说的)。
类似的东西:
[TestMethod]
public void Cell_Color_Background_Test()
{
//
//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();
}
谢谢厄尼!我稍微更改了它以允许 excel 中的 header 并确保代码不从 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);
}
}