private int ExportDataBulk(Func <int, int, ICollection> getDataFunc) { int totalCount = 0; int bulkCount = 20000; string templateFile = System.Web.HttpContext.Current.Server.MapPath(@"~\Excel\Template\ExcelTemplate.xlsx"); using (FileStream fileStream = new FileStream(templateFile, FileMode.Open)) { WorkbookDesigner designer = new WorkbookDesigner(new Workbook(fileStream)); Cells cells = designer.Workbook.Worksheets[0].Cells; object[,] templateValueArray = cells.ExportArray(1, 0, 1, cells.Columns.Count); while (totalCount <= 1000000) { ICollection bulkData = getDataFunc(totalCount, bulkCount); if (bulkData == null || bulkData.Count <= 0) { break; } if (totalCount > 0) { cells.ImportTwoDimensionArray(templateValueArray, totalCount + 1, 0); } designer.SetDataSource("Product", bulkData); designer.Process(); designer.ClearDataSource(); totalCount += bulkData.Count; if (bulkData.Count < bulkCount) { break; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } string fileName = DateTime.Now.Ticks.ToString(); fileName = System.Web.HttpContext.Current.Server.MapPath(@"~\Excel\Export\" + fileName + ".xlsx"); designer.Workbook.Save(fileName); } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); return(totalCount); }
private int ExportDataBulk(Func <int, int, ICollection> getDataFunc) { int totalCount = 0; int bulkCount = 20000; string templateFile = System.Web.HttpContext.Current.Server.MapPath(@"~\Excel\Template\ExcelTemplate.xlsx"); string exportFileName = DateTime.Now.Ticks.ToString(); exportFileName = System.Web.HttpContext.Current.Server.MapPath(@"~\Excel\Export\" + exportFileName + ".xlsx"); object[,] templateValueArray = null; using (FileStream fileStream = new FileStream(templateFile, FileMode.Open)) { WorkbookDesigner designer = new WorkbookDesigner(new Workbook(fileStream)); Cells cells = designer.Workbook.Worksheets[0].Cells; templateValueArray = cells.ExportArray(1, 0, 1, cells.Columns.Count); } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); while (totalCount <= 1000000) { ICollection bulkData = getDataFunc(totalCount, bulkCount); if (bulkData == null || bulkData.Count <= 0) { break; } string loadFileName = (totalCount <= 0 ? templateFile : exportFileName); using (FileStream fileStream = new FileStream(loadFileName, FileMode.Open, FileAccess.ReadWrite, FileShare.ReadWrite)) { using (Workbook workbook = new Workbook(fileStream)) { WorkbookDesigner designer = new WorkbookDesigner(workbook); using (Cells cells = workbook.Worksheets[0].Cells) { if (totalCount > 0) { cells.ImportTwoDimensionArray(templateValueArray, totalCount + 1, 0); } designer.SetDataSource("Product", bulkData); designer.Process(); designer.ClearDataSource(); XlsSaveOptions xlsSaveOptions = new XlsSaveOptions() { ClearData = true }; workbook.Save(exportFileName, xlsSaveOptions); cells.Dispose(); } workbook.Dispose(); } fileStream.Close(); fileStream.Dispose(); } totalCount += bulkData.Count; if (bulkData.Count < bulkCount) { break; } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); } GC.Collect(); GC.WaitForPendingFinalizers(); GC.Collect(); GC.WaitForPendingFinalizers(); return(totalCount); }