This post describes to you how to convert CSV to Excel in C# without the use of third-party software. This tutorial will show you how to convert documents between spreadsheet file types in a simple and basic manner. Furthermore, the convert CSV to XLSX C# sample code can be used in the project as is, or you can improve it by changing the properties of the converted document.
Steps to Convert CSV to Excel in C#
- Configure GroupDocs.Conversion for .NET from the NuGet package manager into the application
- Use GroupDocs.Conversion namespace in the code
- Initialize Converter class and pass the source CSV file
- Instantiate SpreadsheetConvertOptions class for defining the properties for the converted Excel file
- Save the output Excel file to the disk by utilizing the Convert method of the Converter class and provide an instance of the SpreadsheetConvertOptions as well as the name of the output Excel file
The guidelines above provide step-by-step instructions for implementing C# convert CSV to Excel capability with just a few lines of code. The basic document transformation from CSV to Excel format can be accomplished by following the steps outlined above. If you wish to personalize the converted Excel file to meet your specific requirements, you must use the SpreadsheetConvertOptions class to define parameters for the output file.
Code to Convert CSV to Excel in C#
using System; | |
using GroupDocs.Conversion.Options.Convert; | |
namespace ConvertCsvToExcelInCSharp | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) // Main function to convert CSV to Excel using C# | |
{ | |
// Remove the watermark in output Excel document by adding license | |
string licensePath = "GroupDocs.Conversion.lic"; | |
GroupDocs.Conversion.License lic = new GroupDocs.Conversion.License(); | |
lic.SetLicense(licensePath); | |
// Load the source CSV file for conversion to Excel | |
var converter = new GroupDocs.Conversion.Converter("sample.csv"); | |
// Set the convert options for the Excel file | |
var convertOptions = new SpreadsheetConvertOptions(); | |
// Convert and save the CSV in XLSX format | |
converter.Convert("converted.xlsx", convertOptions); | |
Console.WriteLine("Done"); | |
} | |
} | |
} |
In the following example, we’ve built a sample code for the C# convert CSV to XLSX capability. This code can be used as a starting point for document conversion and you may alter it to fulfill your requirements. Additionally, numerous parameters can be applied to the output Excel file to modify it, such as the starting page number, the list of page indexes, the number of pages, the password, the zoom, and many others.
In our last article, we discussed how to convert an HTML document to a PDF using C#. If you want to learn more about it, check out the tutorial on how to convert HTML to PDF using C#.