0

我想使用 Asp.Net 设置某些单元格的颜色。假设,如果我的薪水金额大于 20000,则特定单元格的颜色将为红色,其中薪水 > 20000 。其他单元格也是如此。我正在从数据库中获取完整数据,最后使用 closedXML dll 将数据导出到 excel 文件中。我的所有数据都存储在 excel 文件中,但要求已满。

它是我生成 excel 的代码(在 Vb.Net 中)

Public Sub GenerateExcel(ByVal DTExcel As DataTable, ByVal sFolder As String, ByVal email As String)
    Dim filepathemail As String = "", cntCol As Integer = 0, cntrow As Integer = 0, k As Integer = 0

    If DTExcel.Rows.Count > 0 Then
        ' string Sallary= ds.Tables[0].Rows[0]["Sallary"].ToString();

        Dim Excel As New XLWorkbook()

   If Sallary > 20000 Then
                //Here need to change the color of that cell (How to find cell)
  End if

        Dim worksheet = Excel.Worksheets.Add("Longcode SMS Log Report")
        worksheet.Cell(1, 1).InsertTable(DTExcel, True)

        worksheet.Tables.First().InsertRowsAbove(3)
        worksheet.Cell("A1").SetValue("Longcode SMS Log Report").Style.Font.FontName = "Calibri"
        worksheet.Cell("A1").Style.Font.Bold = True
        worksheet.Cell("A1").Style.Font.FontSize = 14
        worksheet.Range("A1:L1").Row(1).Merge()


        worksheet.Cell("A2").SetValue("Report Date : " & Format(ReportDate, "dd/MM/yyyy ")).Style.Font.FontName = "Calibri"
        worksheet.Cell("A2").Style.Font.Bold = True
        worksheet.Cell("A2").Style.Font.FontSize = 13
        worksheet.Range("A2:L2").Row(1).Merge()

        worksheet.Range("A3:L3").Row(1).Merge()

        Dim dttime As DateTime = DateTime.Now
        dttime = dttime.AddDays(-1)
        Dim hr As String = dttime.Hour
        Dim day As String = dttime.Day
        Dim mon As String = dttime.Month
        Dim yr As String = dttime.Year
        Dim dirPath As String = Application.StartupPath & "\Reports\" & sFolder
        If Not Directory.Exists(dirPath) Then
            Directory.CreateDirectory(dirPath)
        End If
        filepathemail = IO.Path.Combine(dirPath, "Longcode SMS Log Report_" & day & mon & yr & "_" + ".xlsx")
        Dim filepath As String = filepathemail
        Excel.SaveAs(filepath)
4

1 回答 1

0

您所描述的内容称为Excel 和 ClosedXml 中的条件格式(请参阅 ClosedXml 文档)。代码看起来像这样:

worksheet.Range("B2:B100").AddConditionalFormat()
    .WhenGreaterThan(20000).Fill.SetBackgroundColor(XLColor.Red);
于 2015-03-12T10:48:43.077 回答