{ using var stream = File.Create(path); using var excel = new ExcelPackage(stream); ExcelWorksheet sheet = excel.Workbook.Worksheets.Add("Sheet1"); PropertyInfo props = typeof(User).GetProperties; for (var i = 0; i < props.Length; ++i) { sheet.Cells[1, i + 1].Value = https://www.isolves.com/it/cxkf/yy/net/2019-09-02/props[i].Name; } for (var i = 0; i < data.Count; ++i) { for (var j = 0; j < props.Length; ++j) { sheet.Cells[i + 2, j + 1].Value = https://www.isolves.com/it/cxkf/yy/net/2019-09-02/props[j].GetValue(data[i]); } } excel.Save;}注意,不同于NPOI/Aspose.Cells,EPPlus的下标是基于1的(而不是0) 。
次数分配内存内存提高耗时1534,970,328156,04832482533,610,23214,89628073533,595,9367,64828534533,590,7764,40827425533,598,44011,2802759 分配内存约508MB,耗时首次稍长,约3.2秒,后面稳定在2.7-2.8秒 。
OpenXMLvoid Export<T>(List<T> data, string path){ using SpreadsheetDocument excel = SpreadsheetDocument.Create(path, SpreadsheetDocumentType.Workbook); WorkbookPart workbookPart = excel.AddWorkbookPart; workbookPart.Workbook = new Workbook; WorksheetPart worksheetPart = workbookPart.AddNewPart<WorksheetPart>; worksheetPart.Worksheet = new Worksheet(new SheetData); Sheets sheets = excel.WorkbookPart.Workbook.AppendChild<Sheets>(new Sheets); Sheet sheet = new Sheet { Id = excel.WorkbookPart.GetIdOfPart(worksheetPart), SheetId = 1, Name = "Sheet1" }; sheets.Append(sheet); SheetData sheetData = https://www.isolves.com/it/cxkf/yy/net/2019-09-02/worksheetPart.Worksheet.GetFirstChild PropertyInfo props = typeof(User).GetProperties; { // header var row = new Row { RowIndex = 1 }; sheetData.Append(row); row.Append(props.Select((prop, i) => new Cell { CellReference = ('A' + i - 1) + row.RowIndex.Value.ToString, CellValue = https://www.isolves.com/it/cxkf/yy/net/2019-09-02/new CellValue(props[i].Name), DataType = new EnumValue<CellValues>(CellValues.String), })); } sheetData.Append(data.Select((item, i) => { var row = new Row { RowIndex = (uint)(i + 2) }; row.Append(props.Select((prop, j) => new Cell { CellReference = ('A' + j - 1) + row.RowIndex.Value.ToString, CellValue = https://www.isolves.com/it/cxkf/yy/net/2019-09-02/new CellValue(props[j].GetValue(data[i]).ToString), DataType = new EnumValue<CellValues>(CellValues.String), })); return row; })); excel.Save;}注意,因为`OpenXML`比较偏低层,东西比较复杂,所以我们慢慢说:
- 对于一些对象,它需要创建相应的Part,如WorksheetPart;
- Excel可以使用SharedStringTable来共享变量值,适合相同字符串非常多的场景 。
但此示例共享变量值收益很低,但会极大地增加代码复杂性(普通用户可能很难写出),因此本示例未使用SharedStringTable;
- 它基于单元格位置标识,如B3(第三行第二列),因此索引方式比EPPlus/NPOI都要复杂;
- 代码示例中使用'A' + i - 1来计算位置标识,因此这个示例不能用于超过26列(字母数)的数据;
- 代码使用LINQ(而不是循环)来枚举所有行/列,可以让代码在已经非常复杂的情况下,更简洁一点;
经测试,将LINQ改成for循环对性能结果变化影响极其微小 。
次数分配内存内存提高耗时1556,937,896145,83240092555,981,21631237833555,985,9362,76038844555,984,3841,87238695555,989,1203,8803704 内存占用约530MB左右,第一次比后面多1MB的样子,耗时3.7-4.0秒之间 。
Aspose.Cells
void Export<T>(List<T> data, string path){ using var excel = new Workbook; Worksheet sheet = excel.Worksheets["Sheet1"]; PropertyInfo props = typeof(User).GetProperties; for (var i = 0; i < props.Length; ++i) { sheet.Cells[0, i].Value = https://www.isolves.com/it/cxkf/yy/net/2019-09-02/props[i].Name; } for (var i = 0; i < data.Count; ++i) { for (var j = 0; j < props.Length; ++j)
推荐阅读
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
- .NET如何生成大量随机数据
- Excel中怎么计算出某个日期是星期几
- 使用频率很高的3个Excel函数公式
- Excel新手常见的几种坑,亲测踩中很伤
- 在Excel里面制作一个二维码 做二维码
- .net开源框架简介和通用技术选型建议
- 一组Excel专用小技巧,新手请拿好
- 微信聊天记录怎么导出word?不可错过的微信技巧
- 怎样冻结excel表格的某个区域?
- Excel数据校验有办法
