We occasionally need to export data from one format to another for a variety of reasons. In this post, we’ll show you how to convert Excel to CSV in C# and provide some sample code. This article explains how to set up the functionality and perform simple document transformation to CSV format. In just a few minutes, you can convert Excel to CSV using C# by following the steps and code below.
Steps to Convert Excel to CSV in C#
- Install GroupDocs.Conversion for .NET package from the NuGet.org
- Use GroupDocs.Conversion namespace
- Initialize Converter class and input the source Excel file
- Create an object of the SpreadsheetConvertOptions class and set properties for the converted document
- Finally, save the converted file to disc by invoking the Converter class’s Convert method with the converted file name and an instance of the SpreadsheetConvertOptions class
You should now have a good understanding of how simple it is to follow these steps and develop the c# convert Excel to CSV feature. You can begin by downloading the needed package from the NuGet website and invoking the Converter class with the source Excel file. Then set the format for the rendering file by initializing the SpreadsheetConvertOptions class. Finally, pass the name of the output file to the Convert method, along with the object produced in the previous step, so that the document can be saved to a disc.
Code to Convert Excel to CSV in C#
using System; | |
using GroupDocs.Conversion.Options.Convert; | |
namespace ConvertExcelToCsvInCSharp | |
{ | |
class Program | |
{ | |
public static void Main(string[] args) // Main function to convert Excel to CSV using C# | |
{ | |
// Remove the watermark in output CSV document by adding license | |
string licensePath = "GroupDocs.Conversion.lic"; | |
GroupDocs.Conversion.License lic = new GroupDocs.Conversion.License(); | |
lic.SetLicense(licensePath); | |
// Load the source Excel file for conversion to CSV | |
var converter = new GroupDocs.Conversion.Converter("sample.xlsx"); | |
// Set the starting sheet number and consecutive sheet count | |
var convertOptions = new SpreadsheetConvertOptions | |
{ | |
Format = GroupDocs.Conversion.FileTypes.SpreadsheetFileType.Csv, | |
PageNumber = 1, | |
PagesCount = 1 | |
}; | |
// Convert and save the Excel in CSV format | |
converter.Convert("converted.csv", convertOptions); | |
Console.WriteLine("Done"); | |
} | |
} | |
} |
We have written some sample code for basic document conversion that demonstrates how to use the C# convert XLSX to CSV capability. In addition to specifying the file type for the output document, we have also specified two properties for the converted document: the list of page indexes and the number of pages. You can also add many other attributes to the rendering document, such as a Password, the starting page number, the zoom level, and a Watermark.
The feature of converting a PDF to HTML in C# was discussed in our previous post. Check out the how to convert PDF to HTML using C# guide if you want to learn more.