USING IRONXL

How to Save A CSV File in C#

Updated August 23, 2024
Share:

Whether you're dealing with business data, scientific research, or any other domain, mastering the art of saving data to CSV in C# is an indispensable skill for software developers and data analysts alike. This article will use IronXL's market-leading data manipulating library to save spreadsheets as a CSV File and how it can be used also in writing data to CSV files.

How to Save a CSV File

  1. Install the C# library for saving spreadsheets into a CSV File.
  2. Utilize the using IronXL method to import the IronXL.
  3. Load an existing spreadsheet using WorkBook.Load method.
  4. Access the worksheet using workBook.WorkSheets.
  5. Save the Spreadsheet as a CSV file using the SaveAs method.

IronXL

IronXL stands at the forefront of simplifying the process of saving data to CSV files within C# applications, offering developers a versatile and efficient toolkit. Seamlessly integrating into C#.

Whether you're working on a data-driven application, a reporting tool, or simply need a reliable solution for exporting data, IronXL proves to be a powerful ally. IronXL comes in handy when creating CSV files, saving CSV files, and writing CSV files using C#.

This article explores the capabilities and features that make IronXL an indispensable asset for C# developers looking to streamline the task of saving data to CSV files, ensuring simplicity without compromising on flexibility or precision.

Create a New Visual Studio Project

Before you can install IronXL, you'll need to either create a new Visual Studio C# project or load an existing one. Here's a step-by-step guide to creating a new project in Visual Studio.

  1. Open Visual Studio and navigate to the "File" menu. A dropdown menu will appear; choose "New" from this menu, triggering another side menu to appear. In this side menu, select "Project".

    How to Save A CSV File in C#, Figure 1: Create a new project in Visual Studio Create a new project in Visual Studio

  2. A new window will open. In this window, use the search bar to look for "Console Application" and choose the item with the C# option. Click on the Next button.

    How to Save A CSV File in C#, Figure 2: Select Console Application Select Console Application

  3. A configuration window will pop up. Enter the project name, set the project location, and then click "Next".

    How to Save A CSV File in C#, Figure 3: Configure the new project Configure the new project

  4. The final window will appear. In this window, select the target framework and click on the Create button.

    How to Save A CSV File in C#, Figure 4: Target framework selection Target framework selection

Installing CSV Library IronXL

Now that you've set up your project, let's get the IronXL C# library installed. Follow these steps to integrate IronXL into your C#

  1. In Visual Studio, navigate to Tools. A dropdown menu will appear. From this menu, select NuGet Package Manager.
  2. In the NuGet Package Manager, access the side menu and choose Manage NuGet Packages for Solutions.

    How to Save A CSV File in C#, Figure 5: Navigate to NuGet Package Manager Navigate to NuGet Package Manager

  3. A new window will pop up. Go to the browser tab within this window and type "IronXL" in the search bar. You'll see a list of IronXL Packages. Choose the latest package from the list and click on the install button.

    How to Save A CSV File in C#, Figure 6: Search and install the IronXL package in NuGet Package Manager UI Search and install the IronXL package in NuGet Package Manager UI

Create CSV File by Saving Excel files

IronXL provides a feature for creating CSV files by saving Excel files in CSV format. This section will demonstrate how to save an Excel file as a new CSV file using the C# CSV Library IronXL. The following example code demonstrates the simple tutorial.

Input Excel File

How to Save A CSV File in C#, Figure 7: The input Excel file The input Excel file

using IronXL;

WorkBook workBook = WorkBook.Load("test.xlsx");
WorkSheet workSheet = workBook.WorkSheets[0];
workBook.SaveAs("Tesing.csv");
using IronXL;

WorkBook workBook = WorkBook.Load("test.xlsx");
WorkSheet workSheet = workBook.WorkSheets[0];
workBook.SaveAs("Tesing.csv");
Imports IronXL

Private workBook As WorkBook = WorkBook.Load("test.xlsx")
Private workSheet As WorkSheet = workBook.WorkSheets(0)
workBook.SaveAs("Tesing.csv")
VB   C#

This above code uses the IronXL library, creates a new CSV file and writes data from the Excel file in it. Let's break down the code step by step:

1. Importing IronXL

using IronXL;
using IronXL;
Imports IronXL
VB   C#

This line imports the necessary classes and functionalities from the IronXL library.

2. Loading an Excel Workbook

WorkBook workBook = WorkBook.Load("test.xlsx");
WorkBook workBook = WorkBook.Load("test.xlsx");
Dim workBook As WorkBook = WorkBook.Load("test.xlsx")
VB   C#

This line loads an Excel workbook from the file named "test.xlsx". The WorkBook class is part of the IronXL library and represents an Excel workbook. The variable "workBook" is now an instance of the WorkBook class that contains the data from the specified Excel file.

3. Accessing a Worksheet

WorkSheet workSheet = workBook.WorkSheets[0];
WorkSheet workSheet = workBook.WorkSheets[0];
Dim workSheet As WorkSheet = workBook.WorkSheets(0)
VB   C#

This line gets the first worksheet (index 0) from the loaded Excel workbook. The WorkSheet class represents an Excel worksheet, and the variable WorkSheet now holds a reference to the first worksheet in the workbook.

4. Saving as a CSV file

workBook.SaveAs("Testing.csv");
workBook.SaveAs("Testing.csv");
workBook.SaveAs("Testing.csv")
VB   C#

This line saves the entire workbook as a CSV (Comma-Separated Values) file named "Testing.csv." The SaveAs method is used to specify the file name and format for saving the workbook. In this case, the format is CSV, it's easily converted and saved all the rows of the Excel file into a CSV string.

In short, the code loads an Excel workbook from a file, accesses the first worksheet in that workbook, and then saves the entire workbook as a CSV file named "Testing.csv" using the IronXL library.

Output

How to Save A CSV File in C#, Figure 8: The output CSV file The output CSV file

Conclusion

This guide provides an overview of the process of saving data to a CSV file using C# with the assistance of the IronXL library. The ability to write data to CSV is a crucial operation in various applications, and IronXL simplifies this task by seamlessly integrating into C#

The step-by-step instructions cover the installation of IronXL through NuGet Packages, creating a new Visual Studio project, and using IronXL to load an Excel workbook, access a worksheet, and save the entire workbook as a CSV file. The provided code snippet demonstrates a practical example of how to achieve this task efficiently.

The ability to write CSV files, or create a new CSV file using C# is extremely useful for developers and by leveraging IronXL.

IronXL proves to be a valuable asset, combining simplicity with flexibility and precision, making it an indispensable tool for C# developers working on data-driven applications, reporting tools, or any project that involves exporting data to CSV files.

The ability to write CSV files, or create a new CSV file using C# is extremely useful for developers and how to create CSV files are available in this tutorial and code examples. The source code of saving CSV file can be found by visiting this code sample.

To try IronXL for yourself, opt in for the free trial offered by IronXL. This gives developers the chance to explore everything that IronXL has to offer. If you find IronXL helpful in your projects or feel that it could be beneficial for any upcoming projects, you can purchase a license, with prices starting at just $749 for the Lite version.

< PREVIOUS
How to Edit a Spreadsheet in C#
NEXT >
How to Work With Excel Files Using IronXL: A .NET Excel Library

Ready to get started? Version: 2024.11 just released

Free NuGet Download Total downloads: 1,111,773 View Licenses >