Create a CSV file using C#
Create and output a CSV file easily with IronXL's free for development. The following code shows you how.
How to Create a CSV file using C#
- Add IronXL Package
- Load existing WorkBook and save as CSV File
Step 1
1. Add IronXL Package
First, you must have IronXL installed. There are various options. You can download it directly: https://ironsoftware.com/csharp/excel/docs/
Or you can import the IronXL NuGet package by following the next few steps
- Right-click on the Solution in the Solution Explorer
- Click Manage NuGet Packages
- Browse for IronXL.Excel
- Click Install
# Install IronXL via the NuGet Package Manager Console
Install-Package IronXL.Excel
# Install IronXL via the NuGet Package Manager Console
Install-Package IronXL.Excel
IronXL.Excel NuGet Package
How to Tutorial
2. Save Workbook as CSV
Easy as that!
Here is the code for this little project. This code loads an existing Excel Workbook, and then simply saves it in the CSV format by using the SaveAsCsv
method.
The Normal Excel workbook contains the following data:
// Import the IronXL library
using IronXL;
class Program
{
static void Main()
{
// Load an existing workbook
WorkBook wb = WorkBook.Load("Normal_Excel_File.xlsx"); // You can import .xls, .csv, or .tsv files
// Save the workbook to a CSV file. This will save the first worksheet as a CSV.
wb.SaveAsCsv("SaveAsCSV.csv", ","); // This will output: SaveAsCSV.Sheet1.csv
}
}
// Import the IronXL library
using IronXL;
class Program
{
static void Main()
{
// Load an existing workbook
WorkBook wb = WorkBook.Load("Normal_Excel_File.xlsx"); // You can import .xls, .csv, or .tsv files
// Save the workbook to a CSV file. This will save the first worksheet as a CSV.
wb.SaveAsCsv("SaveAsCSV.csv", ","); // This will output: SaveAsCSV.Sheet1.csv
}
}
' Import the IronXL library
Imports IronXL
Friend Class Program
Shared Sub Main()
' Load an existing workbook
Dim wb As WorkBook = WorkBook.Load("Normal_Excel_File.xlsx") ' You can import .xls, .csv, or .tsv files
' Save the workbook to a CSV file. This will save the first worksheet as a CSV.
wb.SaveAsCsv("SaveAsCSV.csv", ",") ' This will output: SaveAsCSV.Sheet1.csv
End Sub
End Class
Additionally, the IronXL Excel library provides the following features:
- A broad set of functions, including data manipulation, data export, data import and importing from other sources.
- Support for chart management that is fully compatible with Excel.
- Support for Excel's most popular file formats (.xlsx) and other file formats.
- Support for cell formatting such as text alignment, font size, color, etc.
- Ability to control the way that cells are displayed in an Excel workbook (e.g. gridlines).
Library Quick Access
IronXL API Reference Documentation
Learn more and share how to merge, unmerge, and work with cells in Excel spreadsheets using the handy IronXL API Reference Documentation.
IronXL API Reference DocumentationFrequently Asked Questions
How do I create a CSV file using C#?
You can create a CSV file in C# by using the IronXL library. After installing the IronXL package, load an existing Excel workbook and use the SaveAsCsv
method to save it as a CSV file.
What steps are involved in installing the IronXL package for C#?
To install IronXL, you can either download it directly from the Iron Software website or use the NuGet Package Manager in Visual Studio. In the NuGet Package Manager, browse for IronXL.Excel and click 'Install', or use the command Install-Package IronXL.Excel
in the console.
Can I export data from an Excel file to CSV using C#?
Yes, using IronXL, you can export data from an Excel file to a CSV format. Load the Excel workbook and use the SaveAsCsv
method to export the data.
What are the file formats supported by the IronXL library?
IronXL supports various file formats including .xlsx, .xls, .csv, and .tsv, allowing you to work with a wide range of Excel file types.
How can I manipulate Excel data using a C# library?
With IronXL, you can manipulate Excel data by using its comprehensive functions for data import, data export, and data formatting, giving you full control over Excel files in C#.
Does IronXL support chart management in Excel files?
Yes, IronXL supports chart management, enabling you to work with and manage charts in Excel files seamlessly.
What cell formatting options are available in the IronXL library?
IronXL provides various cell formatting options, including text alignment, font size, and color, as well as gridline customization, helping you to format Excel documents as needed.
Where can I find more detailed API documentation for IronXL?
You can access the detailed API reference documentation for IronXL on the Iron Software website under the API Reference Documentation section, which provides additional resources for working with Excel spreadsheets.