In this topic, we’ll show you how to export large Excel files to CSV in C#. The following steps for programmatically converting Excel files to CSV format in a C# application and the easy-to-use code will give you the solution you need.

The main problem developers face when dealing with large Excel files like XLSX or XLS is memory management. This problem can be easily solved by setting the MemorySetting property of the LoadOptions class to MemoryPreference. This will help manage memory efficiently. The default value of this property is Normal, and it should be used for regular size Excel files.

Aspose.Cells for.net is an Excel spreadsheet programming API that speeds up spreadsheet management and processing tasks, enabling you to build cross-platform applications that can generate, modify, transform, render, and print spreadsheets. It does not rely on Microsoft Excel or any Microsoft Office Interop component. You can download the Aspose.Cells For.NET Developer guide for more help.

Steps for exporting large Excel files to CSV in C#

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

Save large Excel files as C# code in 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); }}}Copy the code

The code above only saves the first worksheet in an Excel file as CSV. However, if you have multiple worksheets in a large Excel file, you can use the following code snippet. Note that in this case, we again need to use the same MemorySetting property to manage memory correctly and efficiently.

Export multiple Excel worksheets as 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); }}}}Copy the code

In the code above, we used a C# console application, but you can use the same code to export Excel files to CSV in ASP.NET or convert Excel files to CSV in Windows applications using the.net Framework. This does not require an Excel file on the system or server where the code is running.

If you have any questions or requirements, please feel free to join the Aspose Technology Exchange Group (761297826), we are happy to provide you with inquiries and consultation.