如何在EPPlus中为字体分配颜色

如何在EPPlus中为字体分配颜色

本文介绍了如何在EPPlus中为字体分配颜色?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我可以像这样设置一个单元格或单元格范围的背景色:

I can set the background color of a cell or range of cells like so:

rowRngprogramParamsRange.Style.Fill.PatternType = ExcelFillStyle.Solid;
rowRngprogramParamsRange.Style.Fill.BackgroundColor.SetColor(Color.DarkRed);

不过我无法设置字体颜色。我尝试过:

I have not been able to set the font color, though. I tried this:

rowRngprogramParamsRange.Style.Font.Color = Color.Red;

...这无法通过两个错误消息进行编译:第一个,我无法分配系统。将Drawing.Color转换为OfficeOpenXml.Style.ExcelColor,第二个属性仍然是只读的。

...which failed to compile with two err msgs: the first, that I cannot assign System.Drawing.Color to OfficeOpenXml.Style.ExcelColor, and the second that the property is readonly anyway.

只是咧着嘴笑,我忍受了,我尝试转换值:

Just for grin and bear its, I tried casting the value:

rowRngprogramParamsRange.Style.Font.Color = (OfficeOpenXml.Style.ExcelColor)Color.Red;

...然后我得到, 无法转换类型'System.Drawing.Color '到'OfficeOpenXml.Style.ExcelColor'

...and I now get, "Cannot convert type 'System.Drawing.Color' to 'OfficeOpenXml.Style.ExcelColor'"

EPPlus中的大多数功能都非常简单,当然比Excel Interop更容易,但这让我感到困惑。

Most everything in EPPlus is pretty easy, certainly easier than Excel Interop, but this one has me baffled. How does one assign a color to a font for a range in EPPlus?

推荐答案

可以安全地 为EPPlus中的某个范围的字体分配颜色吗?假设 Style.Fill.BackgroundColor Style.Font.Color 都属于 ExcelColor ,因此只需使用与设置背景色相同的 SetColor()方法即可。

It's safe to assume Style.Fill.BackgroundColor and Style.Font.Color are both of type ExcelColor, so just use the same SetColor() method you used to set the background color.

rowRngprogramParamsRange.Style.Font.Color.SetColor(Color.Red);

这篇关于如何在EPPlus中为字体分配颜色?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-21 14:44