博文NET导出Excel的四种方法及评测 中对比了4个库的导出性能,但对OpenXML的评价并不高,本人觉得不合理,所以我重新测试下性能
基于OpenXML的包装类 ExcelDownWorker
1 public class ExcelDownWorker 2 { 3 void NewRow();//新行 4 5 void Write(object value);//写入列,从A列开始写 6 7 void Start(Stream stream);//创建开始 8 9 void End();//创建结束 10 }
执行数据写入,数据是前面测试数据 6W行 10列
static void Excel(List<Temp> lt) { Console.WriteLine(GC.GetTotalMemory(true)); var st = new Stopwatch();st.Start();var ew = new ExcelDownWorker(); using (var fs = new FileStream(Directory.GetCurrentDirectory() + "\data.xlsx", FileMode.Create, FileAccess.ReadWrite)) { ew.Start(fs); var ps = typeof(Temp).GetProperties(); foreach (var p in ps) { ew.Write(p.Name); } ew.NewRow(); for (var i = 0; i < 1; i++)//用于测试12W,18W,102W { foreach (var n in lt) { foreach (var p in ps) { ew.Write(p.GetValue(n)); } ew.NewRow(); } } ew.End(); fs.Flush(); } st.Stop(); Console.WriteLine(GC.GetTotalMemory(true)); Console.WriteLine(st.ElapsedMilliseconds); }
class Temp { public int Id { get; set; } public int Gender { get; set; } public string FirstName { get; set; } public string LastName { get; set; } public string Email { get; set; } public DateTime BirthDate { get; set; } public string Company { get; set; } public string Phone { get; set; } public string Website { get; set; } public string SSN { get; set; } }
static void Main(string[] args) { var s = File.ReadAllText(Directory.GetCurrentDirectory() + "\test-data.json"); var lt = DeserializeObject<List<Temp>>(s); for (var i = 0; i < 5; i++) { Console.WriteLine($"___________________第{i+1}次________________________"); Excel(lt); } Console.ReadKey(); }