导出巨大的Excel文件

时间:2017-07-11 10:27:26

标签: c# excel asp.net-mvc-3 epplus

我正在尝试使用EPPlus导出包含1,800,000多行数据和9列的Excel文件。我使用了一个DataSet并将其分成批处理(每个DataTable 100,000个)。目前,它最多可以导出1,000,000行。

以下是我的导出代码:

    public FileContentResult ExportInventory()
    {
         DataSet dsInventory = Session["Live_Inventory"] as DataSet;

         var filename = "Excel Inventory.xlsx";
         var excelContentType = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";
         byte[] excelContent = ExportExcel(dsInventory);

         return File(excelContent, excelContentType, filename);
    }

    public byte[] ExportExcel(DataSet dsInventory)
    {
         byte[] result = null;
         ExcelWorksheet workSheet = null;

         var package = new ExcelPackage();
         for (int tbl = 1; tbl <= dsInventory.Table.Count; tbl++)
         {
              workSheet = package.Workbook.Worksheets.Add(String.Format("Sheet " + tbl));
              workSheet.Cells["A1"].LoadFromDataTable(dsInventory.Tables[tbl], true);                  
         }

         result = package.GetAsByteArray();//Exception occurs here when table count > 10 
         return result;
    }

2 个答案:

答案 0 :(得分:1)

更新答案:

你应该看一下:useful page

这绝对可以帮到你。

答案 1 :(得分:0)

public void DataToExcel(DataTable dataToConvert, HttpResponse response)
{
    FileInfo myFile = new FileInfo("~/MyProject/Inventory.xlsx");
    using (ExcelPackage package = new ExcelPackage(myFile))
    {
        ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1");

        worksheet.Cells["A1"].LoadFromDataTable(dataToConvert, true, OfficeOpenXml.Table.TableStyles.Light1);

        package.Save();


        response.Clear();
        response.ContentType = "application/vnd.openxmlformats-officedocument.wordprocessingml.document";
        response.AddHeader("Content-Disposition", string.Format("attachment;filename=\"{0}\"", "Inventory.xlsx"));
        response.WriteFile("~/MyProject/Inventory.xlsx");
        response.Flush();
        response.End();

    }

}