[英]EPPlus Excel Change cell color
I am trying to set the color of a given cell with the color of another cell (that is already colored in the template. But worksheet.Cells[row, col].Style.Fill.BackgroundColor
doesn't seem to have a get
property. Is it possible to do that or do I have to find the exact hexdecimal code of the color on the internet ?我正在尝试使用另一个单元格的颜色设置给定单元格的颜色(模板中已经着色。但是
worksheet.Cells[row, col].Style.Fill.BackgroundColor
似乎没有get
属性. 是否有可能做到这一点,或者我必须在互联网上找到颜色的确切十六进制代码?
EDIT编辑
using the code in the answer, I get that error (it is written in French but it translate with what I wrote in my first comment)使用答案中的代码,我得到了那个错误(它是用法语写的,但它翻译了我在第一条评论中写的内容)
How about something like this?这样的事情怎么样?
//get color from this cell
var rgb = ws.Cells[1, 2].Style.Fill.BackgroundColor.Rgb;
//Convert to system.drawing.color
var color = System.Drawing.ColorTranslator.FromHtml("#" + rgb);
//set color in this cell
ws.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(color);
Update: Seems that if you select a color from system colors or the palette as your fill color it works fine.更新:似乎如果您从系统颜色或调色板中选择一种颜色作为填充颜色,它可以正常工作。 If you select one of the 'Theme colors' in the fill drop down
.Rgb
returns an empty string如果您在填充下拉列表中选择“主题颜色”之一,则
.Rgb
将返回一个空字符串
//get style
var style = ws.Cells[400, 1].Style;
//If color from System colors or palette
if (!string.IsNullOrEmpty(style.Fill.BackgroundColor.Rgb))
{
//Convert to system.drawing.colow
var color = System.Drawing.ColorTranslator.FromHtml("#" + style.Fill.BackgroundColor.Rgb);
//set color in this cell
ws.Cells[1, 1].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells[1, 1].Style.Fill.BackgroundColor.SetColor(color);
}
else if (!string.IsNullOrEmpty(style.Fill.BackgroundColor.Theme))
{
//No idea how to get color from Theme
}
I'm not sure it's supported... according to EPPlus Faqs :我不确定它是否受支持......根据EPPlus Faqs :
What is NOT supported by the library (these are the most obvious features)?
库不支持什么(这些是最明显的功能)? [...] * Themes
[...] * 主题
For anyone getting here, since I had my round of fun with EPPlus and I wasn't satisfied with the answers above:对于到达这里的任何人,因为我在 EPPlus 上玩得很开心,而且我对上面的答案并不满意:
cell.Style.Fill.BackgroundColor.LookupColor()
returns #AARRGGBB color (IE red is #FFFF0000).返回 #AARRGGBB 颜色(即红色是 #FFFF0000)。 The part you're interesdted in is probably the last 6 digits.
您感兴趣的部分可能是最后 6 位数字。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.