如何在.NET Core项目中使用Excel为数据表格的单元格着色?

huangapple go评论109阅读模式
英文:

How can I color the cell of datagrid with excel in .net core project

问题

在我的.NET Core项目中,我有一个数据表格。我想根据内容对某些列进行着色。我创建了数据表格然后解析了数据。

  1. DataTable dt = new();
  2. dt.Columns.Add("Status");
  3. dt.Columns.Add("Base");
  4. dt.Columns.Add("First Date");
  5. dt.Columns.Add("Second Date");
  6. dt.Columns.Add("Failure Mode");
  7. dataAll.ForEach(row =>
  8. {
  9. dt.Rows.Add(
  10. row.status,
  11. row.partBase,
  12. row.pcaDate,
  13. row.icaDate,
  14. row.failureMode
  15. );
  16. });

我想要更改状态列的颜色。例如,如果状态等于R,我想将该单元格颜色设置为红色。如何着色数据表格单元格。以下是如何导出到Excel的方式:

  1. using XLWorkbook wb = new();
  2. // 创建数据表格
  3. DataTable dt = MapDataToExcel(response.Result.ToList());
  4. // 添加为工作表
  5. wb.Worksheets.Add(dt, "Rapor");
  6. // 将其保存为文件,然后将其流式传输给用户
  7. using MemoryStream stream = new MemoryStream();
  8. wb.SaveAs(stream);
  9. return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Rapor.xlsx");

请注意,如何着色数据表格单元格并不在您提供的代码中。要实现此功能,您需要使用Excel库或工具,例如EPPlus,来打开Excel文件并根据条件着色单元格。这部分的代码不包含在您的示例中。

英文:

I have a datatable in my .net core project. I want to color certain column according to the content. I created the datatable and then parsed the data.

  1. DataTable dt = new();
  2. dt.Columns.Add("Status");
  3. dt.Columns.Add("Base");
  4. dt.Columns.Add("First Date");
  5. dt.Columns.Add("Second Date");
  6. dt.Columns.Add("Failure Mode");
  7. dataAll.ForEach(row =>
  8. {
  9. dt.Rows.Add(
  10. row.status,
  11. row.partBase,
  12. row.pcaDate,
  13. row.icaDate,
  14. row.failureMode
  15. );
  16. });

I want to change the color of the status column. For example to color this cell red if the status is equal to R. How can I color the Datatable cell.This is how I export with Excel;

  1. using XLWorkbook wb = new();
  2. // create data table
  3. DataTable dt = MapDataToExcel(response.Result.ToList());
  4. // add as worksheet
  5. wb.Worksheets.Add(dt, "Rapor");
  6. // save it as a file then stream it to the user
  7. using MemoryStream stream = new MemoryStream();
  8. wb.SaveAs(stream);
  9. return File(stream.ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Rapor.xlsx");

答案1

得分: 1

我已经假设状态字段是一个布尔值。您可以将此方法包含在您的代码片段中,并根据您自己的定义更改条件。

  1. private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  2. {
  3. bool status = Convert.ToBoolean(e.Row.Cells["Status"].Value);
  4. if (status)
  5. {
  6. e.CellStyle.BackColor = Color.Green;
  7. }
  8. else
  9. {
  10. e.CellStyle.BackColor = Color.Red;
  11. }
  12. }
英文:

I have assumed that the status field is a boolen value. You can include this method in your code fragment and change the condition according to your own definition.

  1. private void DataGridView1_CellFormatting(object sender, DataGridViewCellFormattingEventArgs e)
  2. {
  3. bool status = Convert.ToBoolean(e.Row.Cells["Status"].Value);
  4. if (status)
  5. {
  6. e.CellStyle.BackColor = Color.Green;
  7. }
  8. else
  9. {
  10. e.CellStyle.BackColor = Color.Red;
  11. }
  12. }

答案2

得分: 1

  1. for (int i = 2; i < dataCount; i++)
  2. {
  3. foreach (var item in wb.Worksheets)
  4. {
  5. var objPage2 = item.Cell(i, 1).GetString();
  6. if (objPage2 == "R") { item.Cell(i, 1).Style.Fill.BackgroundColor = XLColor.Red; }
  7. if (objPage2 == "G") { item.Cell(i, 1).Style.Fill.BackgroundColor = XLColor.Green; }
  8. if (objPage2 == "Y") { item.Cell(i, 1).Style.Fill.BackgroundColor = XLColor.Yellow; }
  9. }
  10. }

根据特定列的值检查并根据其值进行着色。

英文:
  1. for (int i = 2; i &lt; dataCount; i++)
  2. {
  3. foreach (var item in wb.Worksheets)
  4. {
  5. var objPage2 = item.Cell(i, 1).GetString();
  6. if (objPage2 == &quot;R&quot;) { item.Cell(i, 1).Style.Fill.BackgroundColor = XLColor.Red; }
  7. if (objPage2 == &quot;G&quot;) { item.Cell(i, 1).Style.Fill.BackgroundColor = XLColor.Green; }
  8. if (objPage2 == &quot;Y&quot;) { item.Cell(i, 1).Style.Fill.BackgroundColor = XLColor.Yellow; }
  9. }
  10. }

Check the value of the specific column and color them depending on value

huangapple
  • 本文由 发表于 2023年7月20日 19:21:49
  • 转载请务必保留本文链接:https://go.coder-hub.com/76729340.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定