我正在尝试实现以下目标:我有一个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;
任何帮助将不胜感激!!
答案 0 :(得分:3)
使用fs.Write
的方法只会使用epplus生成的文件覆盖复制的文件,因为您在字节/流级别执行此操作。所以这不会得到你想要的。 (@MatthewD在他的帖子中向你展示了这一点。)
至于应用格式本身,你应该有什么工作但是如果你遇到那种错误我怀疑你正在混合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);
}
}
答案 1 :(得分:0)
要扩展@Ernie代码示例,这是一个工作示例,根据单元格的值为范围着色。该范围的每个单元格可以具有三种颜色中的任何一种,这取决于单元格的值(&lt; .01,&lt; .05,&lt; .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";