使用 epplus c# 设置 Excel 工作表单元格的自定义背景颜色
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16998447/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Set custom BackgroundColor of a Excel sheet cell using epplus c#
提问by Hakuna Matata
The problem:
问题:
I am using EEPlus.
我正在使用 EEPlus。
I am stuck at applying a hex color code, e.g. #B7DEE8, for a cell in my Excel sheet.
我坚持应用十六进制颜色代码,例如#B7DEE8,用于 Excel 工作表中的单元格。
I got the following (working) code:
我得到以下(工作)代码:
ws.Cells["A1:B1"].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells["A1:B1"].Style.Fill.BackgroundColor.SetColor(Color.Gray);
But I need something like the following:
但我需要类似以下内容:
ws.Cells["A1:B1"].Style.Fill.BackgroundColor.SetColor("#B7DEE8");
So my question is: is it possible to use hex color codes with EEPlus? If so, how can I do that?
所以我的问题是:EEPlus 是否可以使用十六进制颜色代码?如果是这样,我该怎么做?
采纳答案by Yograj Gupta
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);
回答by Jhonny Nina
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))
回答by BBBreiz
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:
您不必翻译十六进制 CSS 颜色公式:您可以简单地将“0X”作为该数字的标题,这使其成为整数表达式:
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);

