Convert Excel to CSV and CSV to Excel Formats in C#

Convert XLS XLSX to CSV in C#

XLS and XLSX are the most used and well-known formats of MS Excel spreadsheets. You must be well aware of the enhanced capabilities and countless formatting options of Microsoft Office for these formats during this century. On the other hand, CSV files are the comma-separated values, normally used to store tabular data without formatting. These files can be viewed in any text editor and also in MS Excel for tabular format. This article guides to convert Excel spreadsheets of XLS/XLSX format into CSV format and CSV to XLS/XLSX format programmatically using C#.

The following topics are covered below:

.NET API for Excel Files and CSV Conversion

GroupDocs.Conversion provides .NET API that allows automating the conversion of various documents and image file formats into one another. I will be using this API to convert XLSX into CSV and then CSV into XLS or XLSX using C#. Along with the spreadsheet formats, the API supports back and forth conversion of many other document and image formats like word-processing documents, presentations, eBooks, JPG, PNG, WebP, and many more.

You can download the DLLs or MSI installer from the downloads section or install the API in your .NET application via NuGet.

PM> Install-Package GroupDocs.Conversion

Convert Excel (XLS/XLSX) to CSV in C#

Let’s start with the tabular and well-formatted data in XLS or XLSX format, and convert it into unformatted comma-separated CSV format. The following steps allow converting the XLS or XLSX format to CSV within the .NET applications.

  • Load the Excel file (XLS or XLSX) using the Converter class.
  • Set the starting worksheet number and sheet count. (Optional)
  • Set the conversion format of the output file as CSV using SpreadsheetConvertOptions.
  • Call the Convert method to transform the spreadsheet data or and specific pages into CSV format.

The following code shows how to convert XLS or XLSX into CSV format in C#.

Convert CSV to Excel (XLS/XLSX) in C#

On the contrary, if you have the comma-separated data and you want to convert it into a well-formatted tabular format, you need to convert that CSV data into XLS or XLSX format. The following steps show how to convert the CSV file into MS Excel XLSX format using C#.

  • Prepare the loading options for CSV file and define separator.
  • Load the CSV using the Converter class.
  • Set the conversion format to XLSX using SpreadsheetConvertOptions.
  • Use the Convert method to get the CSV data transformed into XLSX format.

The following code shows how to convert your CSV file into XLSX format in C#.

Just set the conversion format accordingly and provide the appropriate file name with the extension for the XLS or any other file format.

Get a Free API License

You can get a free temporary license in order to use the API without the evaluation limitations.

Conclusion

To sum up the article, you learned back and forth conversion of MS Excel spreadsheets XLS/XLSX and CSV files using C#. You can learn more about the .NET Conversion Automation API using the documentation, or by experiencing the examples available on GitHub. Reach us for any query via the forum.

Related Article

See Also