1

I am trying to export an Excel file with 1,800,000+ rows of data with 9 columns using EPPlus. I used a DataSet and separate it into batch (100,000 each DataTable). Currently, it can export at max of 1,000,000 rows.

Here's my code for export below:

    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;
    }
3

2 Answers 2

1

Updated Answer:

You should have a look at this: Export large data to an exel file

This should definitely help you out.

Sign up to request clarification or add additional context in comments.

2 Comments

yes I can get my resources but the package will be closed once it executes the package.GetAsByteArray();
I tried to use your code but it still returns 'System.OutOfMemoryException' on that same line. I wasn't able to try adding await Task.Delay(5000); because it is only available in Framework 4.5 (my project is using .net Framework is 4). Thanks for helping! :)
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();

    }

}

5 Comments

@JosephLYapcengcoJr So what is your final goal? You want to convert your data to excel and save it to the server or download it on client side? or anything else?
convert it to excel then download it on client.
I don't think your code will solve my problem because you just added methods in downloading a file and my problem is when I'm converting "package = new ExcelPackage();" to byte array when the datatable's rows reach 1M+, the System.OutOfMemoryException occurs. Anyway, thanks for your help!..
@JosephLYapcengcoJr Try the following: create excel file in your project manually, say inventory.xlsx . Then write data in that file using LoadFromDataTable method and finally download the file via HttpResponse. This way you will bypass the convertion of package to the byte array which overloads your memory. I have edited my code above. Hope that helps
I tried to use your code but it still returns 'System.OutOfMemoryException' on package.Save(); So I replaced the part that using EPPlus library with an OpenXML here's the code -the ExportToOXML method only and it works!, Thanks for helping! :)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.