DZone
Thanks for visiting DZone today,
Edit Profile
  • Manage Email Subscriptions
  • How to Post to DZone
  • Article Submission Guidelines
Sign Out View Profile
  • Post an Article
  • Manage My Drafts
Over 2 million developers have joined DZone.
Log In / Join
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Related

  • How to Convert Excel and CSV Documents to HTML in Java
  • Simple Method to Convert OLM to CSV Manually
  • How to Convert CSV to XML in Java
  • The Middleware Gap in AI Agent Frameworks

Trending

  • Building a High-Throughput Distributed Sequence Generator Using the Hi-Lo Algorithm
  • Optimizing High-Volume REST APIs Using Redis Caching and Spring Boot (With Load Testing Code)
  • How SaaS Architectures Break at Scale — and the Engineering Decisions That Prevent It
  • Build a GitHub Slack Bot With AWS Bedrock and MCP, Part 2
  1. DZone
  2. Coding
  3. Frameworks
  4. Convert CSV Files to Excel in .NET Framework

Convert CSV Files to Excel in .NET Framework

Learn how to easily convert CSV files to XLSX using an API in .NET Framework.

By 
Brian O'Neill user avatar
Brian O'Neill
DZone Core CORE ·
Jul. 27, 21 · Tutorial
Likes (2)
Comment
Save
Tweet
Share
6.3K Views

Join the DZone community and get the full member experience.

Join For Free

When it comes to file formats for data storage and manipulation, the two formats at the top of the list are CSV and Excel; each brings their own benefits to the table, but which format you should use is dependent on the project at hand. Let’s take a look at the Comma-Separated Values (CSV) format to start; it’s an excellent option for data storage due to its simple design, which allows for creation in almost any text editor. On top of this, it takes up very little memory and stores your data as is without any manipulation; this straightforward nature is why CSV is often the initial choice for developers.

Now, we will move on to the Excel format. This is the more widely used of the two formats, largely due to its compatibility and support across multiple programs and platforms. The compatible structure of Excel, paired with its ability to display charts, graphs, and images makes it the ideal choice when sharing a data file with partners or clients. In addition, while the simplicity of the CSV file may be an advantage at times, it will not allow you to perform the operations or formulas on the data sets that Excel is known for. 

If your project needs to lean more towards Excel’s features, then this article is for you. In this brief tutorial, we will demonstrate how you can use APIs in .NET Framework to automatically convert single or multiple CSV files to XLSX; no additional code-writing needed.

Let’s begin the process by running this command to install the SDK:

C#
 
Install-Package Cloudmersive.APIClient.NET.DocumentAndDataConvert -Version 3.4.2


Once the client installation is complete, we are ready to call the first function. This API function focuses on converting a single CSV file to a single XLSX spreadsheet and can be called with the below example code:

C#
 
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 ConvertDocumentCsvToXlsxExample
    {
        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.

            try
            {
                // Convert CSV to Excel XLSX Spreadsheet
                byte[] result = apiInstance.ConvertDocumentCsvToXlsx(inputFile);
                Debug.WriteLine(result);
            }
            catch (Exception e)
            {
                Debug.Print("Exception when calling ConvertDocumentApi.ConvertDocumentCsvToXlsx: " + e.Message );
            }
        }
    }
}


For a successful operation, you will need to ensure the following parameters are included:

  • Input file – the file to perform the operation on.
  • API Key – your personal API key. To retrieve your personal API key, visit the Cloudmersive website to register for a free account; this provides 800 monthly calls across our entire library of APIs.

Now for the next function, we will be converting multiple CSV files (up to 10) to a single XLSX spreadsheet; each CSV file will be represented as a single worksheet within the new Excel file. This operation provides an option to specify the worksheet names as well; if you choose to not provide worksheet names, they will default to the names of the input CSV files. To call the function, simply input the target CSV files (and worksheet names, optionally) into the below code:

C#
 
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 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 );
            }
        }
    }
}


By using these APIs, you will be able to continue to create files in CSV, with the knowledge that you have a quick and easy solution for converting them to Excel when the need arises.  

Data file CSV Convert (command) Framework

Opinions expressed by DZone contributors are their own.

Related

  • How to Convert Excel and CSV Documents to HTML in Java
  • Simple Method to Convert OLM to CSV Manually
  • How to Convert CSV to XML in Java
  • The Middleware Gap in AI Agent Frameworks

Partner Resources

×

Comments

The likes didn't load as expected. Please refresh the page and try again.

  • RSS
  • X
  • Facebook

ABOUT US

  • About DZone
  • Support and feedback
  • Community research

ADVERTISE

  • Advertise with DZone

CONTRIBUTE ON DZONE

  • Article Submission Guidelines
  • Become a Contributor
  • Core Program
  • Visit the Writers' Zone

LEGAL

  • Terms of Service
  • Privacy Policy

CONTACT US

  • 3343 Perimeter Hill Drive
  • Suite 215
  • Nashville, TN 37211
  • [email protected]

Let's be friends:

  • RSS
  • X
  • Facebook