How to Export Large Excel File to CSV in C#

In this topic, we’ll answer the question how to export large Excel file to CSV in C#. The steps to convert Excel file to CSV format programmatically in C# applications given below along with the simple and easy code will provide you the required solution.

The main problem developers face when processing large Excel file like XLSX or XLS is memory management. This problem can be easily resolved by setting MemorySetting property of LoadOptions class to MemoryPreference. This will help manage memory efficiently. The default value of this property is Normal which should be used in case of a regular size Excel files.

Steps to Export Large Excel File to CSV in C#

  1. Install Aspose.Cells for .NET package from NuGet.org
  2. Add Using directive for Aspose.Cells namespace
  3. Set Aspose license using SetLicense method
  4. Set MemorySetting property to MemoryPreference option
  5. Create an instance of Workbook Class and pass the LoadOptions object created in previous step
  6. Finally, save the exported output CSV file

C# Code to Save Large Excel File as CSV Format

using System;
//Add reference to Aspose.Cells for .NET API
//Use following namespaces to Export excel file to CSV
using Aspose.Cells;
namespace ExportLargeExcelFiletoCSV
{
class Program
{
static void Main(string[] args)
{
//Set Aspose license before exporting Excel file to CSV file format
//using Aspose.Cells for .NET
Aspose.Cells.License AsposeCellsLicense = new Aspose.Cells.License();
AsposeCellsLicense.SetLicense(@"c:\asposelicense\license.lic");
//For optimized memory usage for large excel file use
//MemoryPreference MemorySetting option
LoadOptions OptionsLoadingLargeExcelFile = new LoadOptions();
OptionsLoadingLargeExcelFile.MemorySetting = MemorySetting.MemoryPreference;
//Create an instance of Workbook class to load input large excel file
//Also pass the MemoryPreference load options to the constructor
Workbook ExportExcelToCSVWorkBook = new Workbook("Large_Excel_To_Export.xlsx", OptionsLoadingLargeExcelFile);
//Save the exported output file as CSV format
ExportExcelToCSVWorkBook.Save("Exported_Output_CSV.csv", SaveFormat.Csv);
}
}
}

The above code saves only the first sheet in the Excel file as CSV. However, if you have multiple sheets in the large excel file then you can use the following code snippet. Please note, in this case we again need to use the same MemorySetting property to properly and efficiently manage the memory.

Export Multiple Excel Sheets to Separate CSV Files

using System;
//Add reference to Aspose.Cells for .NET API
//Use following namespaces to Export excel file to CSV
using Aspose.Cells;
namespace ExportLargeExcelFiletoCSV
{
class Program
{
static void Main(string[] args)
{
//Set Aspose license before exporting Excel file to CSV file format
//using Aspose.Cells for .NET
Aspose.Cells.License AsposeCellsLicense = new Aspose.Cells.License();
AsposeCellsLicense.SetLicense(@"c:\asposelicense\license.lic");
//For optimized memory usage for large excel file use
//MemoryPreference MemorySetting option
LoadOptions OptionsLoadingLargeExcelFile = new LoadOptions();
OptionsLoadingLargeExcelFile.MemorySetting = MemorySetting.MemoryPreference;
//Create an instance of Workbook class to load input large excel file
//Also pass the MemoryPreference load options to the constructor
Workbook ExportExcelToCSVWorkBook = new Workbook("Large_Excel_To_Export.xlsx", OptionsLoadingLargeExcelFile);
//To save multiple sheets in a workbook use following code
for (int SheetIndex = 0; SheetIndex < ExportExcelToCSVWorkBook.Worksheets.Count; SheetIndex++)
{
ExportExcelToCSVWorkBook.Worksheets.ActiveSheetIndex = SheetIndex;
ExportExcelToCSVWorkBook.Save("Exported_CSV_" + SheetIndex + ".csv", SaveFormat.Csv);
}
}
}
}

In the above code, we used the C# Console Application, but you can use the same code to export Excel file to CSV in ASP.NET or convert Excel file format to CSV in Windows applications with .NET Framework. This does not require Excel file on the system or server where your code is running.

 English