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