EPPlus 合并背景颜色?
EPPlus merges background colors?
我正在使用 EPPlus 在 Excel sheet 中填写一些数据。作为其中的一部分,我根据程序中的其他数据设置背景颜色。这似乎有效,但是颜色比我输入的颜色深。我弄乱了它,我想如果我把它们放在模板中的白色行中,它们就会正确显示,但如果我把它们在设置为灰色背景的一行中,它们显得很暗。似乎 EPPlus(或 Excel)正在将颜色合并在一起...
我希望该行整体为灰色,但希望能够为某些地方的特定单元格设置颜色覆盖。这是我的代码:
Color fillColor;
Color foreColor = Color.Black;
if (pd.BuiltInParameter == default(int))
{
fillColor = Color.Black;
foreColor = Color.White;
}
else
{
switch (pd.ParameterRevitType)
{
case "Text":
fillColor = Color.FromArgb(198, 89, 17);
break;
case "URL":
fillColor = Color.FromArgb(68, 114, 196);
break;
case "Length":
fillColor = Color.FromArgb(255, 153, 51);
break;
case "Material":
fillColor = Color.FromArgb(48, 84, 150);
foreColor = Color.White;
break;
case "YesNo":
fillColor = Color.FromArgb(112, 48, 160);
foreColor = Color.White;
break;
case "FamilyType":
fillColor = Color.FromArgb(255, 255, 0);
break;
case "Image":
fillColor = Color.FromArgb(0, 176, 240);
break;
case "Currency":
fillColor = Color.FromArgb(0, 176, 80);
break;
default:
fillColor = Color.Red;
foreColor = Color.White;
break;
}
}
ws.Cells[4, currentColumn].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells[4, currentColumn].Style.Fill.BackgroundColor.SetColor(fillColor);
if (foreColor != Color.Black)
{
ws.Cells[3, currentColumn].Style.Font.Color.SetColor(foreColor);
}
有没有办法让它只使用指定的颜色?我在这里错过了什么?
这是 VS 中的颜色预览图和输出的颜色:
作为测试,我设置了主行及其下方的行(下方为白色)。这是结果:
更新
好的,我有一个可重现的样本。这是一个小型控制台应用程序的代码:
class Program
{
static void Main(string[] args)
{
string folderPath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
string templatePath = Path.Combine(folderPath, "Template.xlsx");
string filePath = Path.Combine(folderPath, "Export.xlsx");
if (File.Exists(filePath)) File.Delete(filePath);
File.Copy(templatePath, filePath);
using (var ep = new ExcelPackage(new FileInfo(filePath)))
{
var ws = ep.Workbook.Worksheets["Families-Types"];
ws.Cells[3, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(150, 245, 125));
ep.Save();
}
}
}
Here is a link to the template that it references and Here 是 link 我从程序中得到的导出。一开始看起来几乎是正确的,但导出的是比程序中的 rgb 更深的绿色。如果您转到 Excel 中的单元格格式并输入相同的 rgb 值并按确定,您会看到这一点,它会显着变亮。
好的,我终于找到问题了。我又深入研究了 xml 并且能够在简化的示例中更好地理解它。似乎模板中设置的原始颜色具有 'tint' 的值(在本例中为 -0.499984740745262 但它因颜色而异)。这种色调修改了颜色并由于某种原因被转移到新颜色。幸运的是,EPPlus 有一个可设置的 属性 色调。我将其设置为 0,'ding! ding!' 颜色正确。
所以这是工作应用程序的更新代码,请注意设置颜色后的额外行:
class Program
{
static void Main(string[] args)
{
string folderPath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
string templatePath = Path.Combine(folderPath, "Template.xlsx");
string filePath = Path.Combine(folderPath, "Export.xlsx");
if (File.Exists(filePath)) File.Delete(filePath);
File.Copy(templatePath, filePath);
using (var ep = new ExcelPackage(new FileInfo(filePath)))
{
var ws = ep.Workbook.Worksheets["Families-Types"];
ws.Cells[3, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(150, 245, 125));
ws.Cells[3, 1].Style.Fill.BackgroundColor.Tint = 0;
ep.Save();
}
}
}
希望对其他人有所帮助。
我正在使用 EPPlus 在 Excel sheet 中填写一些数据。作为其中的一部分,我根据程序中的其他数据设置背景颜色。这似乎有效,但是颜色比我输入的颜色深。我弄乱了它,我想如果我把它们放在模板中的白色行中,它们就会正确显示,但如果我把它们在设置为灰色背景的一行中,它们显得很暗。似乎 EPPlus(或 Excel)正在将颜色合并在一起...
我希望该行整体为灰色,但希望能够为某些地方的特定单元格设置颜色覆盖。这是我的代码:
Color fillColor;
Color foreColor = Color.Black;
if (pd.BuiltInParameter == default(int))
{
fillColor = Color.Black;
foreColor = Color.White;
}
else
{
switch (pd.ParameterRevitType)
{
case "Text":
fillColor = Color.FromArgb(198, 89, 17);
break;
case "URL":
fillColor = Color.FromArgb(68, 114, 196);
break;
case "Length":
fillColor = Color.FromArgb(255, 153, 51);
break;
case "Material":
fillColor = Color.FromArgb(48, 84, 150);
foreColor = Color.White;
break;
case "YesNo":
fillColor = Color.FromArgb(112, 48, 160);
foreColor = Color.White;
break;
case "FamilyType":
fillColor = Color.FromArgb(255, 255, 0);
break;
case "Image":
fillColor = Color.FromArgb(0, 176, 240);
break;
case "Currency":
fillColor = Color.FromArgb(0, 176, 80);
break;
default:
fillColor = Color.Red;
foreColor = Color.White;
break;
}
}
ws.Cells[4, currentColumn].Style.Fill.PatternType = ExcelFillStyle.Solid;
ws.Cells[4, currentColumn].Style.Fill.BackgroundColor.SetColor(fillColor);
if (foreColor != Color.Black)
{
ws.Cells[3, currentColumn].Style.Font.Color.SetColor(foreColor);
}
有没有办法让它只使用指定的颜色?我在这里错过了什么?
这是 VS 中的颜色预览图和输出的颜色:
作为测试,我设置了主行及其下方的行(下方为白色)。这是结果:
更新
好的,我有一个可重现的样本。这是一个小型控制台应用程序的代码:
class Program
{
static void Main(string[] args)
{
string folderPath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
string templatePath = Path.Combine(folderPath, "Template.xlsx");
string filePath = Path.Combine(folderPath, "Export.xlsx");
if (File.Exists(filePath)) File.Delete(filePath);
File.Copy(templatePath, filePath);
using (var ep = new ExcelPackage(new FileInfo(filePath)))
{
var ws = ep.Workbook.Worksheets["Families-Types"];
ws.Cells[3, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(150, 245, 125));
ep.Save();
}
}
}
Here is a link to the template that it references and Here 是 link 我从程序中得到的导出。一开始看起来几乎是正确的,但导出的是比程序中的 rgb 更深的绿色。如果您转到 Excel 中的单元格格式并输入相同的 rgb 值并按确定,您会看到这一点,它会显着变亮。
好的,我终于找到问题了。我又深入研究了 xml 并且能够在简化的示例中更好地理解它。似乎模板中设置的原始颜色具有 'tint' 的值(在本例中为 -0.499984740745262 但它因颜色而异)。这种色调修改了颜色并由于某种原因被转移到新颜色。幸运的是,EPPlus 有一个可设置的 属性 色调。我将其设置为 0,'ding! ding!' 颜色正确。
所以这是工作应用程序的更新代码,请注意设置颜色后的额外行:
class Program
{
static void Main(string[] args)
{
string folderPath = Path.GetDirectoryName(typeof(Program).Assembly.Location);
string templatePath = Path.Combine(folderPath, "Template.xlsx");
string filePath = Path.Combine(folderPath, "Export.xlsx");
if (File.Exists(filePath)) File.Delete(filePath);
File.Copy(templatePath, filePath);
using (var ep = new ExcelPackage(new FileInfo(filePath)))
{
var ws = ep.Workbook.Worksheets["Families-Types"];
ws.Cells[3, 1].Style.Fill.BackgroundColor.SetColor(Color.FromArgb(150, 245, 125));
ws.Cells[3, 1].Style.Fill.BackgroundColor.Tint = 0;
ep.Save();
}
}
}
希望对其他人有所帮助。