编程语言
首页 > 编程语言> > C#-EPPlus Excel更改单元格颜色

C#-EPPlus Excel更改单元格颜色

作者:互联网

我正在尝试将给定单元格的颜色设置为另一个单元格的颜色(模板中已着色).
但是worksheet.Cells [row,col] .Style.Fill.BackgroundColor似乎没有getproperty.
是否可以这样做,还是我必须在互联网上找到确切的颜色十六进制代码?

编辑

使用答案中的代码,我得到了那个错误(它是用法语编写的,但会与我在第一条评论中写的一样翻译)

解决方法:

这样的事情怎么样?

//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);

更新:
看来,如果您从系统颜色或调色板中选择一种颜色作为填充色,则效果很好.如果您在填充下拉菜单中选择“主题颜色”之一,则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
}

我不确定它是否受支持…根据EPPlus Faqs

What is NOT supported by the library (these are the most obvious features)?
[…]
* Themes

标签:epplus,c
来源: https://codeday.me/bug/20191028/1950385.html