EPPlus Excel更改单元格颜色 [英] EPPlus Excel Change cell color

查看:368
本文介绍了EPPlus Excel更改单元格颜色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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 getproperty. Is it possible to do that or do I have to find the exact hexdecimal code of the color on the internet ?

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)

推荐答案

这样的东西怎么样?

//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常见问题解答

I'm not sure it's supported... according to EPPlus Faqs :


什么不是库支持(这些是最明显的功能)?
[...]
*主题

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

这篇关于EPPlus Excel更改单元格颜色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆