【问题标题】:Set row color with closedXML based on column value根据列值使用 closedXML 设置行颜色
【发布时间】:2019-03-08 08:54:23
【问题描述】:

我正在尝试使用 VB.NET 从数据表中编写 Excel 文件。我也能够实现一些 excel 操作。现在我想根据特定列的单元格值(例如 Col-4)为一行着色。这是我的 Excel 文件 -

Here is my Excel file

I want to achieve like this -

寻找一些指导来实现这一目标。

【问题讨论】:

    标签: closedxml


    【解决方案1】:

    我建议您添加条件格式。例如:

    var workbook = new XLWorkbook();
    var ws = workbook.AddWorksheet("Sheet1");
    
    ws.FirstCell().SetValue(1)
      .CellBelow().SetValue(1)
      .CellBelow().SetValue(2)
      .CellBelow().SetValue(3)
      .CellBelow().SetValue(4);
    
    ws.RangeUsed().AddConditionalFormat().WhenBetween(2, 3)
      .Fill.SetBackgroundColor(XLColor.Red);
    

    参考:https://github.com/ClosedXML/ClosedXML/wiki/Conditional-Formatting

    【讨论】:

    • 谢谢。我通过添加以下代码来满足要求 - Dim nonEmptyDataRows2 = ws1.RowsUsed For Each dataRow1 In nonEmptyDataRows2 'for row number check Dim cell1 As String = dataRow1.Cell(29).Value If cell1 = "0" Then dataRow1.Style.Fill .BackgroundColor = XLColor.Yellow ElseIf cell1 = "1" Then dataRow1.Style.Fill.BackgroundColor = XLColor.Green End If Next
    • @RituparnoBhattacharya 是 C# 吗?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-07
    • 1970-01-01
    • 1970-01-01
    • 2018-10-21
    • 2014-03-11
    相关资源
    最近更新 更多