具有现有背景颜色的单元格中的SetColor问题

背景

我们的业务团队最近将用于从Web应用程序导出数据的空白Excel文件的背景颜色从“无填充”更改为灰色(RGB:217,217,217)颜色。 我们的目标是让所有数据不是写入数据的单元格都是灰色的,而不是白色/不填充。 奇怪的是,这种改变作为导出过程的一部分分配给各个单元的颜色不再反映分配的实际颜色。 导出过程使用EPPLUS 4.1.0(与版本4.0.5,4.0.4相同的结果)和以下代码来设置单元格颜色:

worksheet.Cells["A1:H10"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; worksheet.Cells["A1:H10"].Style.Fill.BackgroundColor.SetColor( System.Drawing.ColorTranslator.FromHtml("#DDEBF7")); 

当我将灰色背景上的HTML颜色设置为#DDEBF7(RGB:221,235,247)时,得到的颜色是#A4CAEA(RGB:164,202,234)。 在没有填充背景的情况下,颜色被正确地改变(到RGB:221,235,247)。

为什么会发生? 这灰色的背景是不知何故地修改了“setcolor”的结果,这似乎确实如此,我该如何避免/避免这种行为?

其他说明

  1. 作为导出过程的一部分,我们可以将单元格块设置为灰色背景颜色,但这会对导出的性能产生负面影响。

  2. 我们可以在源文件中保留NO FILL背景,但是我们希望允许业务团队控制外观和感觉。

下面是重现该问题所需的代码(假定您的网站目录中有一个名为EPPlusColorTest.xlsx的Excel文件,并带有两个名为“NoFillSheet”和“GraySheet”的空白表格,GraySheet工作表的背景颜色应设置为RGB :217,217,217。

EPPLUS_ColorTest.aspx

 <%@ Page Language="C#" AutoEventWireup="true" CodeFile="EPPLUS_ColorTest.aspx.cs" Inherits="EPPLUS_ColorTest" %> <!DOCTYPE html> <html xmlns="http://www.w3.org/1999/xhtml"> <head runat="server"> <title></title> </head> <body> <form id="form1" runat="server"> <div> <p>Testing color issues with XLSX Files verses XLSM Files</p> <p>Input the HTML Color to test (eg #DDEBF7) <asp:TextBox runat="server" id="tbxHTMLColor">#DDEBF7</asp:TextBox> </p> <asp:Button runat="server" id="btnXLSXTest" text="XLSX Test" OnClick="btnXLSXTest_OnClick"/> </div> </form> </body> </html> 

EPPLUS_ColorTest.aspx.cs

 using System; using System.Web; using OfficeOpenXml; using System.IO; public partial class EPPLUS_ColorTest : System.Web.UI.Page { protected void btnXLSXTest_OnClick(object sender, EventArgs e) { string fileName = "EPPlusColorTest.xlsx"; string filePathAndName = HttpContext.Current.Request.MapPath(HttpContext.Current.Request.ApplicationPath + "\\" + fileName); FileInfo fiNew = new FileInfo(filePathAndName); using (ExcelPackage xlPackage = new ExcelPackage(fiNew)) { ExcelWorksheet worksheet = null; //No Fill Sheet - set background color worksheet = xlPackage.Workbook.Worksheets.Count < 1 ? xlPackage.Workbook.Worksheets.Add("NoFillSheet") : xlPackage.Workbook.Worksheets["NoFillSheet"]; worksheet.Cells["A1:H10"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; worksheet.Cells["A1:H10"].Style.Fill.BackgroundColor.SetColor( System.Drawing.ColorTranslator.FromHtml(tbxHTMLColor.Text)); //Gray Sheet - set background color worksheet = xlPackage.Workbook.Worksheets.Count < 1 ? xlPackage.Workbook.Worksheets.Add("GraySheet") : xlPackage.Workbook.Worksheets["GraySheet"]; worksheet.Cells["A1:H10"].Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; worksheet.Cells["A1:H10"].Style.Fill.BackgroundColor.SetColor(System.Drawing.ColorTranslator.FromHtml(tbxHTMLColor.Text)); xlPackage.Save(); } Response.Clear(); Response.ContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"; Response.AppendHeader("Content-Disposition", "attachment; filename=" + fileName); Response.TransmitFile(filePathAndName); Response.Flush(); Response.End(); } }