How to Convert XLSX to CSV using C# in .NET Framework

Spreadsheet software like Excel provides a simple and user-friendly way to organize and store data strings in a way that can be instantly sorted or parsed. However, the formatting and design of Excel spreadsheets may not always be compatible with different database software. In these cases, it is smart to transfer your data strings to CSV so that the database can quickly and easily analyze the plain text input. This API will show you how to convert any XLSX file to CSV, as well as how to convert multiple spreadsheets at once.

Image for post
Image for post

First, we need to install our SDK. This first code block will remain the same for both functions:

Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 1.3.4

Then, for our first function, we can call ConvertDocumentXlsxToCsv. This will only convert the first worksheet in your XLSX file:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class ConvertDocumentXlsxToCsvExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
// Uncomment below to setup prefix (e.g. Bearer) for API key, if needed
// Configuration.Default.AddApiKeyPrefix("Apikey", "Bearer");
var apiInstance = new ConvertDocumentApi();
var inputFile = new System.IO.Stream(); // System.IO.Stream | Input file to perform the operation on.
try
{
// Excel XLSX to CSV
byte[] result = apiInstance.ConvertDocumentXlsxToCsv(inputFile);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentXlsxToCsv: " + e.Message );
}
}
}
}

The second function, though, will allow you to convert multiple Excel worksheets to separate CSV files at once. To perform this action, call our function, ConvertDocumentXlsxToCsvMulti:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NET.DocumentAndDataConvert.Model;
namespace Example
{
public class ConvertDocumentXlsxToCsvMultiExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");
var apiInstance = new ConvertDocumentApi();
var inputFile = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Input file to perform the operation on.
var outputEncoding = outputEncoding_example; // string | Optional, set the output text encoding for the result; possible values are UTF-8, ASCII and UTF-32. Default is UTF-8. (optional)
try
{
// Convert Excel XLSX Spreadsheet to CSV, Multiple Worksheets
CsvCollection result = apiInstance.ConvertDocumentXlsxToCsvMulti(inputFile, outputEncoding);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentXlsxToCsvMulti: " + e.Message );
}
}
}
}

Now, you can instantly convert any XSLX file to CSV for streamlined use in any non-Excel-compatible application. To retrieve your free API Key, visit the Cloudmersive website. This will give you access to 800 monthly calls across our library of APIs.

Written by

There’s an API for that. Cloudmersive is a leader in Highly Scalable Cloud APIs.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store