如何使EPPlus中的大型工作表的单元格格式更快

我有一个C#数据处理应用程序,它使用EPPlus将最终结果写入Excel表单。 行的背景颜色根据该行上的数据表示而改变。 时间从来都不是问题,因为我只处理之前<100MB的文件。 但是,随着我的要求改变,文件变大,我注意到,只是着色使得我的应用程序慢了60%。 消除着色使应用程序显着更快。 下面的代码片段是我用来对数据进行着色以使其可视化区分的代码示例。 我不是EPPlus的专家,但有没有办法,这可以优化,使我的应用程序更快? 或者,有没有更好的方法让我把这些行视觉上截然不同的人谁最终将看数据? 任何帮助将不胜感激!

if (data[4] == "3") { // color the type 3 messages here var fill1 = cell1.Style.Fill; fill1.PatternType = ExcelFillStyle.Solid; fill1.BackgroundColor.SetColor(Color.LightGray); } if (data[4] == "4") { var fill1 = cell1.Style.Fill; fill1.PatternType = ExcelFillStyle.Solid; fill1.BackgroundColor.SetColor(Color.BlanchedAlmond); } 

编辑:

这是我用来复制模板并将excel数据写入新工作表的代码。 p是一个Excel包,在写入excel文件之前,我将其转换为字节数组。

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

这里是打开现有文件的代码。

  FileInfo AddressList = new FileInfo("c:\test\test.xlsx"); // Open and read the XlSX file. try { using (ExcelPackage package = new ExcelPackage(AddressList)) { // Get the work book in the file ExcelWorkbook workBook = package.Workbook; if (workBook != null) { if (workBook.Worksheets.Count > 0) { // Get the first worksheet //ExcelWorksheet Worksheet = workBook.Worksheets.First(); var worksheet = package.Workbook.Worksheets[1]; 

如果使用命名样式,则EPPlus中的样式和大多数Excel API会更快。 像这样分配和使用EPPlus中的单元格样式…

 internal static string YourStyleName = "MyStyle"; ExcelNamedStyleXml yourStyle = excel.Workbook.Styles.CreateNamedStyle(YourStyleName); yourStyle.Style.Font.Color.SetColor(Color.DarkRed); yourStyle.Style.Fill.PatternType = ExcelFillStyle.Solid; yourStyle.Style.Fill.BackgroundColor.SetColor(Color.LemonChiffon); // ... sheet.Cells[sourceRange].StyleName = YourStyleStyleName