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
Please enter at least three characters to search
Refcards Trend Reports
Events Video Library
Refcards
Trend Reports

Events

View Events Video Library

Zones

Culture and Methodologies Agile Career Development Methodologies Team Management
Data Engineering AI/ML Big Data Data Databases IoT
Software Design and Architecture Cloud Architecture Containers Integration Microservices Performance Security
Coding Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks
Culture and Methodologies
Agile Career Development Methodologies Team Management
Data Engineering
AI/ML Big Data Data Databases IoT
Software Design and Architecture
Cloud Architecture Containers Integration Microservices Performance Security
Coding
Frameworks Java JavaScript Languages Tools
Testing, Deployment, and Maintenance
Deployment DevOps and CI/CD Maintenance Monitoring and Observability Testing, Tools, and Frameworks

Last call! Secure your stack and shape the future! Help dev teams across the globe navigate their software supply chain security challenges.

Modernize your data layer. Learn how to design cloud-native database architectures to meet the evolving demands of AI and GenAI workloads.

Releasing software shouldn't be stressful or risky. Learn how to leverage progressive delivery techniques to ensure safer deployments.

Avoid machine learning mistakes and boost model performance! Discover key ML patterns, anti-patterns, data strategies, and more.

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
  • How to Convert XLSX to CSV in Java

Trending

  • Unmasking Entity-Based Data Masking: Best Practices 2025
  • How Trustworthy Is Big Data?
  • Streamlining Event Data in Event-Driven Ansible
  • Beyond Linguistics: Real-Time Domain Event Mapping with WebSocket and Spring Boot
  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.1K 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
  • How to Convert XLSX to CSV in Java

Partner Resources

×

Comments
Oops! Something Went Wrong

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

ABOUT US

  • About DZone
  • Support and feedback
  • Community research
  • Sitemap

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 100
  • Nashville, TN 37211
  • support@dzone.com

Let's be friends:

Likes
There are no likes...yet! 👀
Be the first to like this post!
It looks like you're not logged in.
Sign in to see who liked this post!