C# export generic data via Microsoft.Office.Interop.Excel vs NPOI,the latter is much faster 40 times than the former
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Threading.Tasks; using Excel = Microsoft.Office.Interop.Excel; using System.Reflection; using Microsoft.Office.Interop.Excel; using System.Windows.Forms; using System.IO; using NPOI.XSSF.UserModel; namespace Framework.Utility { public class ExportHelper<T> where T : class, new() { public static void ExportDataViaOfficeInteropExcel(IEnumerable<T> dataList, ref string fileName, bool isAutoDownload = false) { int loopCount = 1; try { if (dataList == null || !dataList.Any()) { MessageBox.Show("The data source is null"); return; } var excelApp = new Excel.Application(); var workBook = excelApp.Workbooks.Add(); var workSheet = (Worksheet)workBook.Worksheets[1]; workSheet.Columns.AutoFit(); var firstRowData = dataList.FirstOrDefault(); var pis = firstRowData.GetType().GetProperties().Where(x => !x.GetMethod.IsVirtual).ToArray(); if (pis != null && pis.Any()) { for (int i = 0; i < pis.Count(); i++) { workSheet.Cells[1, i + 1] = pis[i].Name; } int row = 1; foreach (var data in dataList) { row++; for (int i = 0; i < pis.Count(); i++) { var objValue = pis[i].GetValue(data); loopCount++; workSheet.Cells[row, i + 1] = objValue?.ToString(); } } } //SaveFileDialog sfd = new SaveFileDialog(); //sfd.Filter = "Excel files (*.xlsx)|*.xlsx|All files (*.*)|*.*"; //sfd.FileName = $"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}"; //if (sfd.ShowDialog()==DialogResult.OK) //{ // workBook.SaveCopyAs(sfd.FileName); //} //string fileName= $"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}.xlsx"; fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), $"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}InteropExcel.xlsx"); workBook.SaveCopyAs(fileName); Console.WriteLine(loopCount); } catch (Exception ex) { MessageBox.Show(ex.StackTrace); } } public static void ExportDataViaNPOI(List<T> dataList,ref string fileName) { if (dataList == null || !dataList.Any()) { MessageBox.Show("The data source is null"); return; } XSSFWorkbook book = new XSSFWorkbook(); fileName = Path.Combine(Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location), $"{DateTime.Now.ToString("yyyyMMddHHmmssffff")}NPOI.xlsx"); var firstRowData = dataList.FirstOrDefault(); var sheet = book.CreateSheet("Sheet1"); var firstRow = sheet.CreateRow(0); var propertiesArr = firstRowData.GetType().GetProperties().Where(x => !x.GetMethod.IsVirtual).ToArray(); for (int i = 0; i < propertiesArr.Length; i++) { var column = firstRow.CreateCell(i); column.SetCellValue(propertiesArr[i].Name); } for (int i = 1; i <= dataList.Count; i++) { var indexRow = sheet.CreateRow(i); for (int j = 0; j < propertiesArr.Length; j++) { var indexColumn = indexRow.CreateCell(j); var indexColumnName = propertiesArr[j]; var columnValue = indexColumnName.GetValue(dataList[i - 1]); if (columnValue != null) { indexColumn.SetCellValue(columnValue.ToString()); } } } using (FileStream stream = File.OpenWrite(fileName)) { book.Write(stream); stream.Close(); } dataList = null; } } }
InteropExcel,202011302115526893,Count:1000,time cost 38261 milliseconds
NPOI,202011302116405231,Count:1000,time cost 823 milliseconds
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· AI与.NET技术实操系列:基于图像分类模型对图像进行分类
· go语言实现终端里的倒计时
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 分享一个免费、快速、无限量使用的满血 DeepSeek R1 模型,支持深度思考和联网搜索!
· 基于 Docker 搭建 FRP 内网穿透开源项目(很简单哒)
· ollama系列01:轻松3步本地部署deepseek,普通电脑可用
· 25岁的心里话
· 按钮权限的设计及实现
2016-11-30 WPF Command
2016-11-30 visa