使用EPPlus进行条件格式化

我试图达到以下目的:我有一个C#应用程序,它执行一些数据处理,然后使用EPPlus输出到.xlsx 。 我想添加一些条件格式到Excel,并尝试了下面的方法,首先我做了一个模板空白excel的所有条件格式设置规则,然后尝试转储数据。 下面的代码片段是我的方法。 p是一个Excel包。 目前这不起作用,数据写入正确,但是我设置的格式化规则丢失了。 我猜是因为它在写作之前基本上清除了所有的东西。 任何帮助将不胜感激!

 Byte[] bin = p.GetAsByteArray(); File.Copy("C:\\template.xlsx", "C:\\result.xlsx"); using (FileStream fs = File.OpenWrite("C:\\result.xlsx")) { fs.Write(bin, 0, bin.Length); } 

注意::我也尝试了以下以避免整个外部模板情况..检查下面的代码片段。 这个问题是,.xlsx生成后,我打开它,它说,该文件有不可读或不可显示的内容,它需要修复它,并在我这样做后,一切都很好,条件格式也工作。 我不知道为什么要这样做,或者我怎样才能摆脱文件打开时的错误。

  string _statement = "$E1=\"3\""; var _cond = ws.ConditionalFormatting.AddExpression(_formatRangeAddress); _cond.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; _cond.Style.Fill.BackgroundColor.Color = Color.LightCyan; _cond.Formula = _statement; 

任何帮助将不胜感激!!

使用fs.Write的方法将简单地用epplus生成的文件覆盖复制的文件,因为您在字节/stream级别执行此操作。 所以这不会得到你想要的。 (@MatthewD在他的post中向你展示了这个)。

至于应用格式本身,你有什么应该工作,但如果你正在得到这样的错误,我怀疑你是混合Excel文件的epplus和非epplus操纵。 这就是你应该这样做的粗略方法:

 [TestMethod] public void Conditional_Format_Test() { //http://stackoverflow.com/questions/31296039/conditional-formatting-using-epplus var existingFile = new FileInfo(@"c:\temp\temp.xlsx"); if (existingFile.Exists) existingFile.Delete(); //Throw in some data var datatable = new DataTable("tblData"); datatable.Columns.Add(new DataColumn("Col1", typeof(int))); datatable.Columns.Add(new DataColumn("Col2", typeof(int))); datatable.Columns.Add(new DataColumn("Col3", typeof(int))); for (var i = 0; i < 20; i++) { var row = datatable.NewRow(); row["Col1"] = i; row["Col2"] = i * 10; row["Col3"] = i * 100; datatable.Rows.Add(row); } using (var pack = new ExcelPackage(existingFile)) { var ws = pack.Workbook.Worksheets.Add("Content"); ws.Cells["E1"].LoadFromDataTable(datatable, true); //Override E1 ws.Cells["E1"].Value = "3"; string _statement = "$E1=\"3\""; var _cond = ws.ConditionalFormatting.AddExpression(new ExcelAddress(ws.Dimension.Address)); _cond.Style.Fill.PatternType = ExcelFillStyle.Solid; _cond.Style.Fill.BackgroundColor.Color = Color.LightCyan; _cond.Formula = _statement; pack.SaveAs(existingFile); } } 

要展开@Ernie代码示例,下面是一个根据单元格的值为范围着色的示例。 根据单元格的值(<.01,<.05,<.1),范围中的每个单元格可以具有三种颜色中的任意一种。

 ExcelRange rng = ws.Cells[statsTableRowStart, 10, statsTableRowStart + gud.levels.level.Count() - 1, 10]; OfficeOpenXml.ConditionalFormatting.Contracts.IExcelConditionalFormattingExpression _condp01 = ws.ConditionalFormatting.AddExpression(rng); _condp01.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; _condp01.Style.Fill.BackgroundColor.Color = System.Drawing.Color.OrangeRed; _condp01.Formula = new ExcelFormulaAddress(rng.Address) + "<.01"; OfficeOpenXml.ConditionalFormatting.Contracts.IExcelConditionalFormattingExpression _condp05 = ws.ConditionalFormatting.AddExpression(rng); _condp05.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; _condp05.Style.Fill.BackgroundColor.Color = System.Drawing.Color.OliveDrab; _condp05.Formula = new ExcelFormulaAddress(rng.Address) + "<.05"; OfficeOpenXml.ConditionalFormatting.Contracts.IExcelConditionalFormattingExpression _condp1 = ws.ConditionalFormatting.AddExpression(rng); _condp1.Style.Fill.PatternType = OfficeOpenXml.Style.ExcelFillStyle.Solid; _condp1.Style.Fill.BackgroundColor.Color = System.Drawing.Color.LightCyan; _condp1.Formula = new ExcelFormulaAddress(rng.Address) + "<.1";