【问题标题】:Customize excel sheet's cell background color of open xml in asp.net在asp.net中自定义打开xml的excel表的单元格背景颜色
【发布时间】:2015-03-11 10:37:18
【问题描述】:

我想使用 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)

【问题讨论】:

    标签: c# asp.net export-to-excel closedxml


    【解决方案1】:

    您所描述的在 Excel 和 ClosedXml (see the ClosedXml documentation) 中称为 条件格式。代码如下所示:

    worksheet.Range("B2:B100").AddConditionalFormat()
        .WhenGreaterThan(20000).Fill.SetBackgroundColor(XLColor.Red);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-09-21
      • 1970-01-01
      • 2017-09-17
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多