EPPlus导出两千万记录的测试代码
采用导入100w条记录一个文件,然后合并的方式
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | using System; using System.IO; using OfficeOpenXml; using System.Data; using System.Diagnostics; using System.ComponentModel; using System.Collections.Generic; namespace ConsoleApp2 { class Program { static void Main( string [] args) { Stopwatch watch = new Stopwatch(); watch.Start(); List< string > zlist = new List< string >(); using (DataTable sourceTable = new DataTable()) { sourceTable.TableName = "test" ; sourceTable.Columns.Add( "NO" , typeof ( string )); sourceTable.Columns.Add( "a" , typeof ( int )); sourceTable.Columns.Add( "b" , typeof ( float )); for ( int i = 0; i < 40000000 / 1; i++) { DataRow dr = sourceTable.NewRow(); dr[ "NO" ] = "#" + (i + 1).ToString(); ; dr[ "a" ] = i; dr[ "b" ] = i; sourceTable.Rows.Add(dr); dr = null ; } int dd = 1000000 / 1; int pages = ( int )sourceTable.Rows.Count / dd + 1; if (sourceTable.Rows.Count % dd == 0) pages = pages - 1; for ( int i = 1; i <= pages; i++) { string zfilename = Guid.NewGuid().ToString() + ".xlsx" ; zlist.Add(zfilename); FileInfo zfile = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + zfilename); using (ExcelPackage excel = new ExcelPackage(zfile)) { ExcelWorksheet ws = excel.Workbook.Worksheets.Add(sourceTable.TableName + i.ToString()); for ( int zcol = 0; zcol < sourceTable.Columns.Count; zcol++) { ws.Cells[1, zcol + 1].Value = sourceTable.Columns[zcol].ColumnName; } int zrow = 0; while (sourceTable.Rows.Count > 0) { for ( int zcol = 0; zcol < sourceTable.Columns.Count; zcol++) { ws.Cells[zrow + 2, zcol + 1].Value = sourceTable.Rows[0][zcol]; } sourceTable.Rows.RemoveAt(0); zrow++; if (zrow % dd == 0) break ; } Console.WriteLine( string .Format( "pageindex:{0}" ,i)); excel.Save(); } } sourceTable.Rows.Clear(); sourceTable.Columns.Clear(); sourceTable.Clear(); sourceTable.Reset(); } GC.Collect(); GC.WaitForFullGCComplete(); string zpath = AppDomain.CurrentDomain.BaseDirectory; zpath = zpath.Substring(0, zpath.Length - 1); MergeExcel(zpath, zlist, string .Format( "{0}.xlsx" ,Guid.NewGuid())); foreach ( string item in zlist) { File.Delete(zpath + "\\" + item); } watch.Stop(); Console.Write( "用时:" + watch.Elapsed.ToString()); Console.ReadLine(); Console.ReadLine(); } public static DataTable DtSelectTop( int from , int to, DataTable oDT) { if (oDT.Rows.Count < from ) return oDT; DataTable NewTable = oDT.Clone(); DataRow[] rows = oDT.Select( "1=1" ); for ( int i = from ; i < to; i++) { ((DataRow)rows[i])[ "NO" ] = "#" + (i + 1).ToString(); NewTable.ImportRow((DataRow)rows[i]); } return NewTable; } private static bool MergeExcel( string _stFilePath, List< string > _listFiles, string _stSaveFileName) { ExcelPackage epMergeFile = new ExcelPackage(); bool result = true ; try { string stSheetName = string .Empty; int zi = 0; foreach ( string item in _listFiles) { zi++; FileInfo newFile = new FileInfo(_stFilePath + "\\" + item); using (ExcelPackage pck = new ExcelPackage(newFile)) { ExcelWorkbook workBook = pck.Workbook; if (workBook != null ) { if (workBook.Worksheets.Count > 0) { stSheetName = workBook.Worksheets[1].Name; epMergeFile.Workbook.Worksheets.Add(stSheetName, workBook.Worksheets[1]); } } } } } catch (Exception ex) { result = false ; Debug.WriteLine( "合并文件失败:" + ex.Message); throw new Exception( "合并文件失败!" ); } if (result) { string stFile = _stFilePath + "\\" + _stSaveFileName; epMergeFile.SaveAs( new FileInfo(stFile)); epMergeFile.Dispose(); } return result; } } } |
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· TypeScript + Deepseek 打造卜卦网站:技术与玄学的结合
· 阿里巴巴 QwQ-32B真的超越了 DeepSeek R-1吗?
· 【译】Visual Studio 中新的强大生产力特性
· 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
· 【设计模式】告别冗长if-else语句:使用策略模式优化代码结构