I have a data table for Oracle stored procedure. It is generating more than 1 million rows. While converting into .xls, the size is almost 36 mb and it is showing an "out-of-memory" exception.
I am using C# and Crystal Reports libraries.
Is there any better way to create a .xlsx file (with all the data and smaller size)?

Amit MohantyRank #112 · 20K reputationPosted May 15, 2023, 8:02 AM EDT
When dealing with large datasets, generating Excel files (.xlsx) can be memory-intensive and may lead to out-of-memory exceptions. To overcome this issue and create a smaller-sized .xlsx file, you can consider using a library like EPPlus, which provides efficient memory usage and compression capabilities.
Install the EPPlus NuGet package in your project.
using OfficeOpenXml; // Create a new Excel package using (var package = new ExcelPackage()) { // Add a new worksheet ExcelWorksheet worksheet = package.Workbook.Worksheets.Add("Sheet1"); // Get your data from the Oracle stored procedure and populate the worksheet // Assuming you have the data in a DataTable called 'dataTable' worksheet.Cells["A1"].LoadFromDataTable(dataTable, true); // Auto-fit columns (optional) worksheet.Cells.AutoFitColumns(); // Save the package to a file string filePath = "path/to/your/file.xlsx"; package.SaveAs(new FileInfo(filePath)); }Tuhin PaulRank #7 · 71.9K reputationPosted May 15, 2023, 1:09 PM EDT
Few things you can do to create a .xlsx file with all the data and a smaller size:
Here are some additional tips for creating a smaller .xlsx file: