Convert Excel to CSV or CSV to Excel in C#
Tags: csharp, excel, csv, dotnet
Published: 2023-12-15 07:34:51

Excel provides advanced functionality for data processing and analysis, while CSV offers a lightweight, platform-independent format for data exchange and integration with a wide range of software tools and programming languages. Converting between the two formats provides flexibility and compatibility depending on the specific needs of data processing and sharing. This article will share how to convert Excel XLS or XLSX to CSV and CSV to Excel formats in C# using a free library.

### Free .NET library for conversion between Excel and CSV formats
Before getting started, we need to download the free library ([Free Spire.XLS for .NET](https://www.e-iceblue.com/Download/download-excel-for-net-free.html) via the below link or install it directly via [Nuget](http://www.nuget.org/packages/FreeSpire.XLS/).

👇

### Convert Excel to CSV in C#.
The main steps to convert Excel XLS or XLSX to CSV format are:

- Create an instance of Workbook class and load a .xls or .xlsx file;
```c#
Workbook workbook = new Workbook();
workbook.LoadFromFile("input.xlsx");
```

- Get a specified worksheet by index;
```c#
Worksheet sheet = workbook.Worksheets[0];
```

- Save the worksheet as CSV.
```c#
sheet.SaveToFile("ExcelToCSV.csv", ",", Encoding.UTF8);
```

**Full C# code:**
```C#
using Spire.Xls;
using System.Text;

namespace ConvertAWorksheetToCsv
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of Workbook class
            Workbook workbook = new Workbook();

            //Load an Excel file
            workbook.LoadFromFile("Test.xlsx");

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Save the worksheet as CSV
            sheet.SaveToFile("ExcelToCSV.csv", ",", Encoding.UTF8);
        }
    }
}
```

### Convert CSV to Excel in C#.
To convert a CSV file to Excel format, follow the below steps:

- Create an instance of Workbook class and load a CSV file;
```C#
Workbook workbook = new Workbook();
workbook.LoadFromFile("input.csv", ",", 1, 1);
```
- Get a specified worksheet by index and then access the used range in it;
```C#
Worksheet sheet = workbook.Worksheets[0];
CellRange usedRange = sheet.AllocatedRange;
```
- Set to Ignore errors when saving numbers in the range as text;
```C#
usedRange.IgnoreErrorOptions = IgnoreErrorType.NumberAsText;
```
- Save CSV file to Excel.
```C#
workbook.SaveToFile("CSVToExcel.xlsx", ExcelVersion.Version2016);
```

**Full C# code:**
```C#
using Spire.Xls;

namespace ConvertCsvToExcel
{
    class Program
    {
        static void Main(string[] args)
        {
            //Create an instance of Workbook class
            Workbook workbook = new Workbook();

            //Load a CSV file
            workbook.LoadFromFile("ExcelToCSV.csv", ",", 1, 1);

            //Get the first worksheet
            Worksheet sheet = workbook.Worksheets[0];

            //Access the used range in the worksheet
            CellRange usedRange = sheet.AllocatedRange;
            //Ignore errors when saving numbers in the range as text
            usedRange.IgnoreErrorOptions = IgnoreErrorType.NumberAsText;
            //Autofit columns and rows
            usedRange.AutoFitColumns();
            usedRange.AutoFitRows();

            //Save the result file
            workbook.SaveToFile("CSVToExcel.xlsx", ExcelVersion.Version2013);
        }
    }
}
```

✨ In conclude, the free .NET Excel library allows you to convert Excel XLS & XLS to CSV as well as convert  CSV to Excel with C# through the **SaveToFile()** method. Also be aware that converting to CSV may result in the loss of complex formatting, macros, and other advanced features unique to Excel.

<u>   </u>

> _**Featured articles:**_
> [Convert Excel to PDF in C# ](https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Excel-Conversion/NET-Excel-New-method-of-Convert-Excel-to-PDF.html)
> [Convert Excel to HTML in C#](https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Convert-Excel-to-HTML-Excel-to-HTML-in-C-VB.NET.html) 
> [Convert Excel to Images in C# ](https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Convert-Excel-to-Image-Worksheet-to-Image-in-C-VB.NET.html)
> [Convert CSV to PDF in C# ](https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Conversion/How-to-Convert-CSV-to-PDF-in-C-VB.NET.html)
> [Export Excel data to Word tables in C# ](https://www.e-iceblue.com/Tutorials/Spire.XLS/Spire.XLS-Program-Guide/Document-Operation/How-to-Export-Excel-Data-to-Word-Table-Maintaining-Formatting-in-C.html)