How to Convert Multiple CSV Files to Excel XLSX using C# .NET Core

Cloudmersive
3 min readMay 29, 2024

--

We can easily convert multiple CSV files to a single Excel document using just a few lines of code.

To handle our conversion, we’ll use the ready-to-run C#/.NET Core code examples provided below to structure a quick API call. This way, we can make consistent, reliable conversions without writing a ton of new code ourselves.

This will allow us to convert up to 10 CSV files into a single Excel document. Each CSV file will become a new worksheet in the resulting XLSX document, and the original CSV file names will be retained as worksheet names.

To authorize our API calls, we’ll just need a free Cloudmersive API key. This will allow us to make up to 800 conversions per month with zero additional commitments (our total will simply reset the following month once we reach our limit).

First, let’s run the following command in our Package Manager console to install the client SDK via NuGet:

Install-Package Cloudmersive.APIClient.NETCore.DocumentAndDataConvert -Version 2.2.1

Next, let’s use the following examples to add in our namespace imports and call the conversion function. We can replace the “YOUR_API_KEY” placeholder text with our own API key string:

using System;
using System.Diagnostics;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Api;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Client;
using Cloudmersive.APIClient.NETCore.DocumentAndDataConvert.Model;

namespace Example
{
public class ConvertDocumentCsvMultiToXlsxExample
{
public void main()
{
// Configure API key authorization: Apikey
Configuration.Default.AddApiKey("Apikey", "YOUR_API_KEY");

var apiInstance = new ConvertDocumentApi();
var inputFile1 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | First input file to perform the operation on.
var inputFile2 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Second input file to perform the operation on.
var inputFile3 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Third input file to perform the operation on. (optional)
var inputFile4 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Fourth input file to perform the operation on. (optional)
var inputFile5 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Fifth input file to perform the operation on. (optional)
var inputFile6 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Sixth input file to perform the operation on. (optional)
var inputFile7 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Seventh input file to perform the operation on. (optional)
var inputFile8 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Eighth input file to perform the operation on. (optional)
var inputFile9 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Ninth input file to perform the operation on. (optional)
var inputFile10 = new System.IO.FileStream("C:\\temp\\inputfile", System.IO.FileMode.Open); // System.IO.Stream | Tenth input file to perform the operation on. (optional)
var worksheetNames = worksheetNames_example; // string | Optional; Specify the name of each CSV's worksheet in order, separated with commas (e.g. \"worksheet1,worksheet2,worksheet3\"). Defaults to the names of the input CSV files. Recommended when inputting the files directly, without file names. (optional)

try
{
// Convert Multiple CSV Files into a Single XLSX Spreadsheet
byte[] result = apiInstance.ConvertDocumentCsvMultiToXlsx(inputFile1, inputFile2, inputFile3, inputFile4, inputFile5, inputFile6, inputFile7, inputFile8, inputFile9, inputFile10, worksheetNames);
Debug.WriteLine(result);
}
catch (Exception e)
{
Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentCsvMultiToXlsx: " + e.Message );
}
}
}
}

Now we can load each of our CSV files into the file input variables and carry out our conversion.

If we aren’t using our original CSV file names in our final document, we can set var worksheetNames to specify the names we’d like to use.

--

--

Cloudmersive
Cloudmersive

Written by Cloudmersive

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

No responses yet