经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 程序设计 » ASP.net » 查看文章
2023版:深度比较几种.NET Excel导出库的性能差异
来源:cnblogs  作者:百宝门园地  时间:2023/9/25 16:50:31  对本文有异议

2023版:深度比较几种.NET Excel导出库的性能差异

引言

背景和目的

本文介绍了几个常用的电子表格处理库,包括EPPlus、NPOI、Aspose.Cells和DocumentFormat.OpenXml,我们将对这些库进行性能测评,以便为开发人员提供实际的性能指标和数据。

下表将功能/特点、开源/许可证这两列分开,以满足需求:

功能 / 特点 EPPlus NPOI Aspose.Cells DocumentFormat.OpenXml
开源
许可证 MIT Apache 商业 MIT
支持的 Excel 版本 Excel 2007 及更高版本 Excel 97-2003 Excel 2003 及更高版本 Excel 2007 及更高版本

测评电脑配置

组件 规格
CPU 11th Gen Intel(R) Core(TM) i5-11320H @ 3.20GHz,2496 Mhz,4 个内核,8 个逻辑处理器
内存 40 GB DDR4 3200MHz
操作系统 Microsoft Windows 10 专业版
电源选项 已设置为高性能
软件 LINQPad 7.8.5 Beta
运行时 .NET 6.0.21

准备工作

使用Bogus库生成6万条标准化的测试数据。

  1. void Main()
  2. {
  3. string path = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.Desktop), "test-data.json");
  4. using var file = File.Create(path);
  5. using var writer = new Utf8JsonWriter(file, new JsonWriterOptions { Indented = true });
  6. var data = new Bogus.Faker<Data>()
  7. .RuleFor(x => x.Id, x => x.IndexFaker + 1)
  8. .RuleFor(x => x.Gender, x => x.Person.Gender)
  9. .RuleFor(x => x.FirstName, (x, u) => x.Name.FirstName(u.Gender))
  10. .RuleFor(x => x.LastName, (x, u) => x.Name.LastName(u.Gender))
  11. .RuleFor(x => x.Email, (x, u) => x.Internet.Email(u.FirstName, u.LastName))
  12. .RuleFor(x => x.BirthDate, x => x.Person.DateOfBirth)
  13. .RuleFor(x => x.Company, x => x.Person.Company.Name)
  14. .RuleFor(x => x.Phone, x => x.Person.Phone)
  15. .RuleFor(x => x.Website, x => x.Person.Website)
  16. .RuleFor(x => x.SSN, x => x.Person.Ssn())
  17. .GenerateForever().Take(6_0000)
  18. .Dump();
  19. JsonSerializer.Serialize(writer, data);
  20. Process.Start("explorer", @$"/select, ""{path}""".Dump());
  21. }

Bogus输出结果

Id Gender FirstName LastName Email BirthDate Company Phone Website SSN
1 Male Antonio Paucek Antonio.Paucek@gmail.com 1987/10/31 5:46:50 Moen, Willms and Maggio (898) 283-1583 x88626 pamela.name 850-06-4706
2 Male Kurt Gerhold Kurt.Gerhold40@yahoo.com 1985/11/1 18:41:01 Wilkinson and Sons (698) 637-0181 x49124 cordelia.net 014-86-1757
3 Male Howard Hegmann Howard2@hotmail.com 1979/7/20 22:35:40 Kassulke, Murphy and Volkman (544) 464-9818 x98381 kari.com 360-23-1669
4 Female Rosemarie Powlowski Rosemarie.Powlowski48@hotmail.com 1964/5/18 1:35:45 Will Group 1-740-705-6482 laurence.net 236-10-9925
5 Female Eunice Rogahn Eunice84@gmail.com 1979/11/25 11:53:14 Rippin - Rowe (691) 491-2282 x3466 yvette.net 219-75-6886
……

创建公共类方便正式测评使用

  1. void Main()
  2. {
  3. string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test-data.json";
  4. LoadUsers(path).Dump();
  5. }
  6. List<User> LoadUsers(string jsonfile)
  7. {
  8. string path = jsonfile;
  9. byte[] bytes = File.ReadAllBytes(path);
  10. return JsonSerializer.Deserialize<List<User>>(bytes);
  11. }
  12. IObservable<object> Measure(Action action, int times = 5)
  13. {
  14. return Enumerable.Range(1, times).Select(i =>
  15. {
  16. var sw = Stopwatch.StartNew();
  17. long memory1 = GC.GetTotalMemory(true);
  18. long allocate1 = GC.GetTotalAllocatedBytes(true);
  19. {
  20. action();
  21. }
  22. long allocate2 = GC.GetTotalAllocatedBytes(true);
  23. long memory2 = GC.GetTotalMemory(true);
  24. sw.Stop();
  25. return new
  26. {
  27. 次数 = i,
  28. 分配内存 = (allocate2 - allocate1).ToString("N0"),
  29. 内存提高 = (memory2 - memory1).ToString("N0"),
  30. 耗时 = sw.ElapsedMilliseconds,
  31. };
  32. }).ToObservable();
  33. }
  34. class User
  35. {
  36. public int Id { get; set; }
  37. public int Gender { get; set; }
  38. public string FirstName { get; set; }
  39. public string LastName { get; set; }
  40. public string Email { get; set; }
  41. public DateTime BirthDate { get; set; }
  42. public string Company { get; set; }
  43. public string Phone { get; set; }
  44. public string Website { get; set; }
  45. public string SSN { get; set; }
  46. }

代码解释

1、上面的代码单位是字节 (bytes)

2 、其中IObservable(System.IObservable)是用于处理事件流的接口,它实现了观察者模式。它表示一个可观察的序列,可以产生一系列的事件,并允许其他对象(观察者)来订阅和接收这些事件。IObservable 适用于动态的、实时的事件流处理,允许观察者以异步方式接收事件,可以用于响应式编程、事件驱动的编程模型等。

3、GC.GetTotalAllocatedBytes(true) 获取分配内存大小
GC.GetTotalMemory(true) 获取占用内存大小

性能测评

EPPlus

  1. string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test-data.json";
  2. List<User> users = LoadUsers(path);
  3. Measure(() =>
  4. {
  5. Export(users, Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\export.epplus.xlsx");
  6. }).Dump("EPPlus");
  7. void Export<T>(List<T> data, string path)
  8. {
  9. using var stream = File.Create(path);
  10. using var excel = new ExcelPackage(stream);
  11. ExcelWorksheet sheet = excel.Workbook.Worksheets.Add("Sheet1");
  12. PropertyInfo[] props = typeof(User).GetProperties();
  13. for (var i = 0; i < props.Length; ++i)
  14. {
  15. sheet.Cells[1, i + 1].Value = props[i].Name;
  16. }
  17. for (var i = 0; i < data.Count; ++i)
  18. {
  19. for (var j = 0; j < props.Length; ++j)
  20. {
  21. sheet.Cells[i + 2, j + 1].Value = props[j].GetValue(data[i]);
  22. }
  23. }
  24. excel.Save();
  25. }

输出结果

EPPlus (6.2.8) (2023/8/15)输出结果

次数ΞΞ 分配内存ΞΞ 内存提高ΞΞ 耗时ΞΞ
1 454,869,176 970,160 2447
2 440,353,488 176 1776
3 440,062,264 0 1716
4 440,283,584 0 1750
5 440,653,264 0 1813

EPPlus (4.5.3.2)(2019/6/16)输出结果

次数ΞΞ 分配内存ΞΞ 内存提高ΞΞ 耗时ΞΞ
1 963,850,944 192,048 2765
2 509,450,792 600 1897
3 509,872,160 424 1920
4 509,858,576 424 1989
5 509,651,512 424 2076

由此看出 相比2019,到了2023年EPPlus的性能得到了略微的提升

NPOI

示例代码一:XSSFWorkbook

  1. List<User> users = LoadUsers(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test-data.json");
  2. Measure(() =>
  3. {
  4. Export(users, Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\export.npoi.xlsx");
  5. }).Dump("NPOI");
  6. void Export<T>(List<T> data, string path)
  7. {
  8. IWorkbook workbook = new XSSFWorkbook();
  9. ISheet sheet = workbook.CreateSheet("Sheet1");
  10. var headRow = sheet.CreateRow(0);
  11. PropertyInfo[] props = typeof(User).GetProperties();
  12. for (var i = 0; i < props.Length; ++i)
  13. {
  14. headRow.CreateCell(i).SetCellValue(props[i].Name);
  15. }
  16. for (var i = 0; i < data.Count; ++i)
  17. {
  18. var row = sheet.CreateRow(i + 1);
  19. for (var j = 0; j < props.Length; ++j)
  20. {
  21. row.CreateCell(j).SetCellValue(props[j].GetValue(data[i]).ToString());
  22. }
  23. }
  24. using var file = File.Create(path);
  25. workbook.Write(file);
  26. workbook.Close();
  27. }

输出结果

NPOI (2.6.1)(2023/7/12)输出结果

次数ΞΞ 分配内存 内存提高 耗时ΞΞ
1 1,589,285,792 567,272 5549
2 1,577,028,664 96 7043
3 1,577,398,488 48 8107
4 1,576,360,696 -90,512 9336
5 1,576,226,688 -3,120 8289

NPOI (2.4.1)(2018/12/18)输出结果

次数ΞΞ 分配内存 内存提高 耗时ΞΞ
1 1,648,548,696 526,824 6947
2 1,633,685,136 120 7921
3 1,634,033,296 24 8864
4 1,634,660,176 -90,200 8945
5 1,634,205,368 -2,584 8078

示例代码二:SXSSFWorkbook

  1. List<User> users = LoadUsers(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test-data.json");
  2. Measure(() =>
  3. {
  4. Export(users, Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\export.npoi.xlsx");
  5. }).Dump("NPOI");
  6. void Export<T>(List<T> data, string path)
  7. {
  8. IWorkbook workbook = new SXSSFWorkbook();
  9. ISheet sheet = workbook.CreateSheet("Sheet1");
  10. var headRow = sheet.CreateRow(0);
  11. PropertyInfo[] props = typeof(User).GetProperties();
  12. for (var i = 0; i < props.Length; ++i)
  13. {
  14. headRow.CreateCell(i).SetCellValue(props[i].Name);
  15. }
  16. for (var i = 0; i < data.Count; ++i)
  17. {
  18. var row = sheet.CreateRow(i + 1);
  19. for (var j = 0; j < props.Length; ++j)
  20. {
  21. row.CreateCell(j).SetCellValue(props[j].GetValue(data[i]).ToString());
  22. }
  23. }
  24. using var file = File.Create(path);
  25. workbook.Write(file);
  26. workbook.Close();
  27. }

输出结果

NPOI (2.6.1)(2023/7/12)输出结果

次数 分配内存 内存提高 耗时
1 571,769,144 11,495,488 2542
2 482,573,584 96 5106
3 481,139,296 24 1463
4 481,524,384 48 1510
5 481,466,616 48 1493

NPOI (2.4.1)(2018/12/18)输出结果

次数 分配内存 内存提高 耗时
1 660,709,472 537,512 7808
2 650,060,376 8,128 8649
3 649,006,952 4,136 7064
4 649,267,920 -89,776 6973
5 649,955,024 48 6538

经过测试 发现SXSSFWorkbook 确实比XSSFWorkbook 性能好,有显著提升
由此看出 相比2018,到了2023年NPOI的性能得到了略微的提升

Aspose.Cells

  1. Util.NewProcess = true;
  2. List<User> users = LoadUsers(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test-data.json");
  3. SetLicense();
  4. Measure(() =>
  5. {
  6. Export(users, Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\export.aspose2.xlsx");
  7. }, 5).Dump("Aspose");
  8. void Export<T>(List<T> data, string path)
  9. {
  10. using var excel = new Workbook();
  11. excel.Settings.MemorySetting = MemorySetting.Normal;
  12. excel.Settings.CheckExcelRestriction = false;
  13. Worksheet sheet = excel.Worksheets["Sheet1"];
  14. sheet.Cells.ImportCustomObjects(data, 0, 0, new ImportTableOptions
  15. {
  16. IsFieldNameShown = true,
  17. DateFormat = "MM/DD/YYYY hh:mm:ss AM/PM",
  18. ConvertNumericData = false,
  19. });
  20. excel.Save(path);
  21. }
  22. void SetLicense()
  23. {
  24. Stream stream = new MemoryStream(Convert.FromBase64String(@"密钥"));
  25. stream.Seek(0, SeekOrigin.Begin);
  26. new Aspose.Cells.License().SetLicense(stream);
  27. }

输出结果

Aspose.Cells (23.8.0)(2023/8/9)输出结果

次数 分配内存 内存提高 耗时
1 443,025,112 3,471,984 2889
2 392,090,304 30,208 1863
3 391,419,072 -8 1716
4 392,041,144 24 1797
5 392,078,992 24 1689

Aspose.Cells (19.8.0)(2019/8/20)输出结果

次数 分配内存 内存提高 耗时
1 552,862,056 2,987,000 2913
2 508,337,872 49,776 1750
3 507,922,728 24 1933
4 507,949,584 24 1781
5 508,368,208 24 1773

由此看出 相比2019,到了2023年Aspose.Cells的性能还是一样差不多,只是内存占用减少了

DocumentFormat.OpenXml

  1. List<User> users = LoadUsers(Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\test-data.json");
  2. Measure(() =>
  3. {
  4. Export(users, Environment.GetFolderPath(Environment.SpecialFolder.Desktop) + @"\export.openXml.xlsx");
  5. }).Dump("OpenXML");
  6. void Export<T>(List<T> data, string path)
  7. {
  8. using SpreadsheetDocument excel = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook);
  9. WorkbookPart workbookPart = excel.AddWorkbookPart();
  10. workbookPart.Workbook = new Workbook();
  11. WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>();
  12. worksheetPart.Worksheet = new Worksheet(new SheetData());
  13. Sheets sheets = excel.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets());
  14. Sheet sheet = new Sheet
  15. {
  16. Id = excel.WorkbookPart.GetIdOfPart(worksheetPart),
  17. SheetId = 1,
  18. Name = "Sheet1"
  19. };
  20. sheets.Append(sheet);
  21. SheetData sheetData = worksheetPart.Worksheet.GetFirstChild<SheetData>();
  22. PropertyInfo[] props = typeof(User).GetProperties();
  23. { // header
  24. var row = new Row() { RowIndex = 1 };
  25. sheetData.Append(row);
  26. row.Append(props.Select((prop, i) => new Cell
  27. {
  28. CellReference = ('A' + i - 1) + row.RowIndex.Value.ToString(),
  29. CellValue = new CellValue(props[i].Name),
  30. DataType = new EnumValue<CellValues>(CellValues.String),
  31. }));
  32. }
  33. sheetData.Append(data.Select((item, i) =>
  34. {
  35. var row = new Row { RowIndex = (uint)(i + 2) };
  36. row.Append(props.Select((prop, j) => new Cell
  37. {
  38. CellReference = ('A' + j - 1) + row.RowIndex.Value.ToString(),
  39. CellValue = new CellValue(props[j].GetValue(data[i]).ToString()),
  40. DataType = new EnumValue<CellValues>(CellValues.String),
  41. }));
  42. return row;
  43. }));
  44. excel.Save();
  45. }

输出结果

DocumentFormat.OpenXml (2.20.0)(2023/4/7)输出结果

次数ΞΞ 分配内存 内存提高 耗时ΞΞ
1 614,013,080 421,552 3909
2 613,007,112 96 3487
3 613,831,672 104 3465
4 613,058,344 24 3650
5 613,161,096 24 3521

DocumentFormat.OpenXml (2.9.1)(2019/3/14)输出结果

次数ΞΞ 分配内存 内存提高 耗时ΞΞ
1 542,724,752 139,080 3504
2 542,478,208 96 2897
3 543,030,904 24 2826
4 542,247,544 24 2957
5 542,763,312 24 2941

由此看出 相比2019,到了2023年DocumentFormat.OpenXml的性能反而越差啦

结论和总结

结论一:如果你想找开源,(旧版本免费),(最新版收费)EPPlus 依旧是最佳选择

次数ΞΞ 分配内存ΞΞ 内存提高ΞΞ 耗时ΞΞ
1 454,869,176 970,160 2447
2 440,353,488 176 1776
3 440,062,264 0 1716
4 440,283,584 0 1750
5 440,653,264 0 1813

结论二:如果你想找速度快,很稳定,但收费的,Aspose.Cells 依旧是最佳选择

次数 分配内存 内存提高 耗时
1 443,025,112 3,471,984 2889
2 392,090,304 30,208 1863
3 391,419,072 -8 1716
4 392,041,144 24 1797
5 392,078,992 24 1689

总结:
1、EPPlus表现不错,内存和耗时在开源组中表现最佳
2、收费的Aspose.Cells表现最佳,内存占用最低,用时也最短

作者 => 百宝门瞿佑明

此文章是对此前《.NET骚操作》2019年写的文章的更新和扩展
https://www.cnblogs.com/sdflysha/p/20190824-dotnet-excel-compare.html

原文地址:https://blog.baibaomen.com/2023版:深度比较几种-net-excel导出库的性能差异/

原文链接:https://www.cnblogs.com/baibaomen-org/p/17721587.html

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号