Skip to footer content
USING IRONXL

How to Create a CSV File Using C# with IronXL

Creating a CSV file in C# is straightforward with IronXL. Simply use the WorkBook.Load() method to open your Excel file, then call SaveAsCsv() to export it as CSV format with your preferred delimiter. This approach eliminates the need for Microsoft Office Interop and provides a robust solution for converting spreadsheet file types in your .NET applications.

How Do I Create a CSV File Using C#?

  • Add IronXL Package
  • Load existing `WorkBook` and save as CSV File
How To `work related to How to Create a CSV File Using C# with IronXL

Step 1

How Do I Add the IronXL Package to My Project?

First, you'll need to install IronXL. You've got several options to choose from. 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. The great news is that IronXL works seamlessly with all .NET environments, including .NET MAUI, Blazor, and can be deployed to AWS, Azure, or Docker environments.

Which Installation Method Should I Choose?

For most developers, the NuGet Package Manager provides the simplest installation method:

  • Right-click on the Solution in the Solution Explorer
  • Click Manage NuGet Packages
  • Browse for IronXL.Excel
  • Click Install

This method ensures all dependencies are properly resolved and you're using the latest stable version. The IronXL library supports Linux and macOS in addition to Windows, making it ideal for cross-platform development.

Can I Install Via Package Manager Console?

If you prefer command-line tools or need to automate installations, the Package Manager Console offers a quick alternative:

# Install IronXL via the NuGet Package Manager Console
Install-Package IronXL.Excel
# Install IronXL via the NuGet Package Manager Console
Install-Package IronXL.Excel
SHELL

Visual Studio NuGet Package Manager showing IronXL.Excel package installation with version 2020.9.1 selected IronXL.Excel NuGet Package

After installation, don't forget to apply your license key to remove watermarks and unlock all features. You can configure licensing in your application startup or through Web.config settings.


How to Tutorial

How Do I Save an Excel Workbook as CSV?

It's really that simple!

Converting Excel files to CSV format using IronXL is refreshingly straightforward and requires minimal code. Unlike traditional approaches that need complex parsing or manual string building, IronXL handles all the formatting and delimiter considerations automatically. This makes it perfect for scenarios where you need to export Excel spreadsheets or convert XLSX to CSV as part of your data processing pipeline.

What Does the Code Look Like?

Here's the code for this simple project. This code loads an existing Excel Workbook and then saves it in CSV format using the SaveAsCsv method.

The original Excel workbook contains the following data:

Excel spreadsheet showing monthly animal population data with columns for Giraffes, Elephants, and Rhinos from January to June.

// 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

        // Alternative: Save a specific worksheet as CSV
        WorkSheet sheet = wb.GetWorkSheet("Sheet1");
        sheet.SaveAsCsv("SpecificSheet.csv", ",");

        // You can also use different delimiters
        wb.SaveAsCsv("TabDelimited.csv", "\t"); // Tab-delimited
        wb.SaveAsCsv("PipeDelimited.csv", "|"); // Pipe-delimited
    }
}
// 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

        // Alternative: Save a specific worksheet as CSV
        WorkSheet sheet = wb.GetWorkSheet("Sheet1");
        sheet.SaveAsCsv("SpecificSheet.csv", ",");

        // You can also use different delimiters
        wb.SaveAsCsv("TabDelimited.csv", "\t"); // Tab-delimited
        wb.SaveAsCsv("PipeDelimited.csv", "|"); // Pipe-delimited
    }
}
$vbLabelText   $csharpLabel

The SaveAsCsv method provides flexibility in choosing your delimiter, which is particularly useful when dealing with data that might contain commas. You can also read CSV files back into Excel format or write to CSV files directly without needing an intermediate Excel file.

For more advanced scenarios, you might want to convert a DataTable to CSV or work with Excel to SQL conversions using IronXL's comprehensive data handling capabilities.

What Additional Features Does IronXL Provide?

The IronXL Excel library also provides these powerful features:

Advanced CSV Operations

When working with CSV files, you may encounter scenarios requiring more sophisticated handling:

Working with Large Files: IronXL efficiently handles large CSV files without loading everything into memory. The library's performance optimizations ensure smooth processing even with file size limits.

Data Validation: Before converting to CSV, you can sort cells, trim cell ranges, or apply data validation to ensure data integrity.

Formula Evaluation: IronXL evaluates formulas before exporting to CSV, ensuring calculated values are properly exported. The library supports math functions and Excel formulas in C#.

Secure Export: For sensitive data, you can encrypt workbooks with passwords before processing or apply worksheet-level protection.


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 Documentation
`documentation related to Library Quick Access

Frequently 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.

Jordi Bardia
Software Engineer
Jordi is most proficient in Python, C# and C++, when he isn’t leveraging his skills at Iron Software; he’s game programming. Sharing responsibilities for product testing, product development and research, Jordi adds immense value to continual product improvement. The varied experience keeps him challenged and engaged, and he ...
Read More