DEV Community

Cover image for Convert Excel to CSV or CSV to Excel in C#
Jeremy K.
Jeremy K.

Posted on

Convert Excel to CSV or CSV to Excel in C#

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 via the below link or install it directly via Nuget.

👇

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;
Workbook workbook = new Workbook();
workbook.LoadFromFile("input.xlsx");
Enter fullscreen mode Exit fullscreen mode
  • Get a specified worksheet by index;
Worksheet sheet = workbook.Worksheets[0];
Enter fullscreen mode Exit fullscreen mode
  • Save the worksheet as CSV.
sheet.SaveToFile("ExcelToCSV.csv", ",", Encoding.UTF8);
Enter fullscreen mode Exit fullscreen mode

Full C# code:

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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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;
Workbook workbook = new Workbook();
workbook.LoadFromFile("input.csv", ",", 1, 1);
Enter fullscreen mode Exit fullscreen mode
  • Get a specified worksheet by index and then access the used range in it;
Worksheet sheet = workbook.Worksheets[0];
CellRange usedRange = sheet.AllocatedRange;
Enter fullscreen mode Exit fullscreen mode
  • Set to Ignore errors when saving numbers in the range as text;
usedRange.IgnoreErrorOptions = IgnoreErrorType.NumberAsText;
Enter fullscreen mode Exit fullscreen mode
  • Save CSV file to Excel.
workbook.SaveToFile("CSVToExcel.xlsx", ExcelVersion.Version2016);
Enter fullscreen mode Exit fullscreen mode

Full C# code:

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);
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

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

Featured articles:
Convert Excel to PDF in C#
Convert Excel to HTML in C#
Convert Excel to Images in C#
Convert CSV to PDF in C#
Export Excel data to Word tables in C#

Top comments (0)