Convert XLSX to CSV - C# .NET 6

This tutorial shows how to convert an XLSX file to CSV in a C# .NET 6 Console application using LEADTOOLS SDK.

Summary This tutorial shows how to convert an XLSX file to CSV using LEADTOOLS SDK in a C# .NET 6 application.
Completion Time 20 minutes
Project Download tutorial project (1 KB)
Platform C# .NET 6 Console Application
IDE Visual Studio 2022
Runtime Target .NET 6 or Higher
Runtime License Download LEADTOOLS

Required Knowledge

Get familiar with the basic steps of creating a project by reviewing the Add References and Set a License tutorial, before working on this tutorial.

Create the Project and Add the LEADTOOLS References

Start with a copy of the project created in the Add References and Set a License tutorial. If you do not have that project, follow the steps in that tutorial to create it.

The references needed depend upon the purpose of the project. References can be added by one or the other of the following two methods (but not both).

If using NuGet references, this tutorial requires the following NuGet package:

If using local DLL references, the following DLLs are needed.

The DLLs are located at <INSTALL_DIR>\LEADTOOLS23\Bin\net:

For a complete list of which DLL files are required for your application, refer to Files to be Included With Your Application.

Set the License File

The License unlocks the features needed for the project. It must be set before any toolkit function is called. For details, including tutorials for different platforms, refer to Setting a Runtime License.

There are two types of runtime licenses:

Add the Convert to CSV Code

Inside the Solution Explorer, open Program.cs. Add the following to the using block at the top of the Program.cs file:

using Leadtools; 
using Leadtools.Document.LEADOffice.Sheet; 

Add the method ConvertToCSV(string inFile, string outFile) and call it inside of the Main method under the InitLEAD method. This method will be used to parse the existing XLSX spreadsheet and generate a CSV file using the information.

static void ConvertToCSV(string inFile, string outFile) 
   // Load workbook 
   LEADWorkbook workbook = LEADWorkbookFactory.LoadFromFile(inFile, new LoadWorkbookOptions()); 
   // Get first sheet 
   Sheet sheet = workbook.GetSheetAt(0); 
   // Set a variable for the length of the header row 
   int lastHeaderCol = sheet.GetRowAt(0).LastCellIndex; 
   StringBuilder sb = new(); 
   // Parse each row to the length of the header row 
   for (int i = 0; i < sheet.LastRowIndex + 1; i++) 
      Row row = sheet.GetRowAt(i); 
      for (int j = 0; j < lastHeaderCol; j++) 
         var cell = row.GetCellAt(j); 
         string value = ""; 
         if (cell != null) 
            // Check for type of cell and pull data from it to add to the string builder 
            switch (cell.CellType) 
               case CellType.Bool: 
                  value = cell.BoolCellValue.ToString(); 
               case CellType.String: 
                  value = cell.StringCellValue; 
               case CellType.Formula: 
                  value = "=" + cell.CellFormula; 
               case CellType.Numeric: 
                  value = cell.NumericCellValue.ToString(); 
                  value = ""; 
         sb.Append($"{value}{(j != row.LastCellIndex - 1 ? "," : "")}"); 
   // Write the string builder to the output file 
   Console.WriteLine( sb.ToString() ); 
   File.WriteAllText(outFile, sb.ToString()); 


Download a sample file to use in this tutorial.

Add the following code to the main method below the InitLEAD() call in order to convert the desired file into CSV.

static void Main(string[] args) 
   ConvertToCSV(inFile, outFile); 

Run the Project

Run the project by pressing F5, or by selecting Debug -> Start Debugging.

If the steps were followed correctly, the application loads the XLSX file, parses its data, and saves the data as a CSV file to the specified location. The original Excel Spreadsheet can be seen below.

Sample Excel Sheet

Example output from the console can be seen below.

Console Output


This tutorial showed how to create a new LEADTOOLS Workbook based off of a 2D array with text, numbers, and formulas, then save it as a CSV file.

See Also

Help Version 23.0.2024.5.22
Products | Support | Contact Us | Intellectual Property Notices
© 1991-2024 LEAD Technologies, Inc. All Rights Reserved.

Products | Support | Contact Us | Intellectual Property Notices
© 1991-2023 LEAD Technologies, Inc. All Rights Reserved.