我想使用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)
答案 0 :(得分:0)
您所描述的内容在Excel和ClosedXml(see the ClosedXml documentation)中称为条件格式。代码看起来像这样:
worksheet.Range("B2:B100").AddConditionalFormat()
.WhenGreaterThan(20000).Fill.SetBackgroundColor(XLColor.Red);