方法一
private void button5_Click(object sender, EventArgs e)
{
Stopwatch watch = new Stopwatch();
BackgroundWorker bw = new BackgroundWorker();
bw.DoWork += new DoWorkEventHandler(delegate
{
watch.Start();
using (SqlConnection conn = new SqlConnection("server=192.168.255.1; database=db1;uid=sa;pwd=sa;"))
{
conn.Open();
SqlCommand cmd = new SqlCommand("select * from tblMRP", conn);
SqlDataAdapter da = new SqlDataAdapter(cmd);
DataSet ds = new DataSet();
da.Fill(ds);
var col = ds.Tables[0].Columns.Add("NO", typeof(string));
col.SetOrdinal(0);
using (ExcelPackage p = new ExcelPackage(new FileInfo(AppDomain.CurrentDomain.BaseDirectory + Guid.NewGuid().ToString() + ".xlsx")))
{
int dd = 1000000;
int pages = (int)ds.Tables[0].Rows.Count / dd + 1;
if (ds.Tables[0].Rows.Count % dd == 0)
pages = pages - 1;
for (int i = 1; i <= pages; i++)
{
DataTable dt = DtSelectTop((i - 1) * dd, Math.Min(i * dd, ds.Tables[0].Rows.Count), ds.Tables[0]);
var ws = p.Workbook.Worksheets.Add(ds.Tables[0].TableName + i.ToString());
ws.Cells["A1"].LoadFromDataTable(dt, true);
}
p.Save();
}
}
watch.Stop();
});
bw.RunWorkerCompleted += new RunWorkerCompletedEventHandler(delegate
{
MessageBox.Show("用时:" + watch.Elapsed.ToString());
});
bw.RunWorkerAsync();
}
public 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;
}
方法二
public static void ExportByEPPlus(DataTable sourceTable)
{
Stopwatch watch = new Stopwatch();
watch.Start();
var col = sourceTable.Columns.Add("NO", typeof(string));
col.SetOrdinal(0);
FileInfo zfile = new FileInfo(AppDomain.CurrentDomain.BaseDirectory + Guid.NewGuid().ToString() + ".xlsx");
int dd = 1000000 / 1;
int pages = (int)sourceTable.Rows.Count / dd + 1;
if (sourceTable.Rows.Count % dd == 0)
pages = pages - 1;
using (ExcelPackage p = new ExcelPackage(zfile))
{
using (sourceTable)
{
for (int i = 1; i <= pages; i++)
{
ExcelWorksheet ws = p.Workbook.Worksheets.Add(sourceTable.TableName + i.ToString());
int zstart = (i - 1) * dd;
int zend = Math.Min(i * dd, sourceTable.Rows.Count);
for (int zrow = zstart; zrow < zend; zrow++)
{
for (int zcol = 0; zcol < sourceTable.Columns.Count; zcol++)
{
ws.Cells[zrow + 1 - zstart, zcol + 1].Value = sourceTable.Rows[zrow][zcol];
}
//Console.WriteLine($"pageindex:{i} ,rows:{zrow}");
}
}
}
GC.Collect(0, GCCollectionMode.Forced);
GC.WaitForPendingFinalizers();
p.Save();
}
watch.Stop();
Console.Write("用时:" + watch.Elapsed.ToString());
Console.Read();
}
private static bool MergeExcel(string _stFilePath, List _listFiles, string _stSaveFileName)
{
ExcelPackage epMergeFile = new ExcelPackage();
bool result = true;
try
{
string stSheetName = string.Empty;
foreach (string item in _listFiles)
{
if (!FileIsHave(_stFilePath + "\\" + item)) continue;
FileInfo newFile = new FileInfo(_stFilePath + "\\" + item);
//开启
using (ExcelPackage pck = new ExcelPackage(newFile))
{
//设定ExcelWorkBook
ExcelWorkbook workBook = pck.Workbook;
if (workBook != null)
{
if (workBook.Worksheets.Count > 0)
{
//设置excel表格的页边距
//workBook.Worksheets[1].PrinterSettings.TopMargin = 10M / 2.54M;
stSheetName = item.Substring(0, item.LastIndexOf('.'));
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;
}
采用导入100w条记录一个文件,然后合并的方式
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;
}
}
}