epplus保存为流_EPPlus导出两千万记录的测试代码

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 zlist = new List();

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 _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;

}

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值