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.

Overview  
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:

C#
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.

C#
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(); 
                  break; 
               case CellType.String: 
                  value = cell.StringCellValue; 
                  break; 
               case CellType.Formula: 
                  value = "=" + cell.CellFormula; 
                  break; 
               case CellType.Numeric: 
                  value = cell.NumericCellValue.ToString(); 
                  break; 
               default: 
                  value = ""; 
                  break; 
            } 
         } 
         sb.Append($"{value}{(j != row.LastCellIndex - 1 ? "," : "")}"); 
      } 
      sb.Append('\n'); 
   } 
   // Write the string builder to the output file 
   Console.WriteLine( sb.ToString() ); 
   File.WriteAllText(outFile, sb.ToString()); 
} 

Note

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.

C#
static void Main(string[] args) 
{ 
   InitLEAD(); 
   string inFile = @"CHANGE PATH TO LOCATION OF SAMPLE FILE"; 
   string outFile = @"CHANGE PATH TO DESIRED OUTPUT LOCATION"; 
   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

Wrap-up

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.4.23
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.