Set custom BackgroundColor of a Excel sheet cell using epplus c#

后端 未结 4 1009
情话喂你
情话喂你 2021-02-01 00:20

The problem:

I am using EEPlus.

I am stuck at applying a hex color code, e.g. #B7DEE8, for a cell in my Excel sheet.

I got the following (wo

相关标签:
4条回答
  • 2021-02-01 00:41

    This is working well.

    Dim objExcel As New ExcelPackage
    Dim Sheet As ExcelWorksheet = objExcel.Workbook.Worksheets.Add("SheetName")
    Sheet.Cells["A1"].Style.Fill.PatternType = Style.ExcelFillStyle.Solid
    Sheet.Cells["A1"].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(170, 170, 170))
    
    0 讨论(0)
  • 2021-02-01 00:42

    This worked for me.

    //fill column A with solid red color from hex

    worksheet.Column(1).Style.Fill.PatternType = ExcelFillStyle.Solid; worksheet.Column(1).Style.Fill.BackgroundColor.SetColor(ColorTranslator.FromHtml("#FF0000"));`

    //fill row 4 with striped orange background

    worksheet.Row(4).Style.Fill.PatternType = ExcelFillStyle.DarkHorizontal; worksheet.Row(4).Style.Fill.BackgroundColor.SetColor(Color.Orange);

    0 讨论(0)
  • 2021-02-01 00:44

    Try this

    Color colFromHex = System.Drawing.ColorTranslator.FromHtml("#B7DEE8");
    ws.Cells["A1:B1"].Style.Fill.PatternType = ExcelFillStyle.Solid;
    ws.Cells["A1:B1"].Style.Fill.BackgroundColor.SetColor(colFromHex);
    
    0 讨论(0)
  • 2021-02-01 01:03

    You are not obliged to translate a hexadecimal CSS color formula: You can simply put "0X" as a header of that number, which makes it an integer expression:

        var couleur = System.Drawing.Color.FromArgb(OXB7DEF8);
        Sheet.Cells["A1"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid;
        Sheet.Cells["A1"].Style.Fill.BackgroundColor.SetColor(couleur);
    
    0 讨论(0)
提交回复
热议问题