USING IRONXL

C# CSV Library (Developer Tutorial)

This article will delve into the world of C# exploring the features, methods, and techniques to efficiently work with any CSV file, enhancing your data-driven projects and simplifying the handling of structured data using the one on the most advanced CSV Library named IronXL.

How to Create, Read, and Export a CSV File

  1. Install the C# library for creating, reading, and exporting CSV files.
  2. Utilize the WorkBook.Create method to create a new workbook.
  3. Create CSV file using workBook.SaveAs("test.csv") method.
  4. Read CSV parsing Using WorkBook.Load Method.
  5. Export CSV to XLSX format using the SaveAs method.

IronXL

IronXL stands as a formidable ally for C# developers venturing into the intricate realm of CSV file manipulation. As a dedicated CSV library, IronXL offers a streamlined and efficient approach to reading, writing, and handling CSV data, providing developers with a robust toolkit to navigate the complexities of comma-separated values.

Whether you're dealing with large datasets, implementing data import/export functionality, or striving for precision in data parsing, IronXL emerges as a reliable companion. This article delves into the features and functionalities that make IronXL an indispensable asset for C# developers seeking a seamless and powerful solution for CSV file processing within their applications.

IronXL offers the perfect solution for spreadsheet styling using code styling of a workbook.

Create a New Visual Studio Project

To begin using IronXL, the initial step involves the creation of a fresh Visual Studio C# project or the loading of an existing one. The following steps outline the process of establishing a new project within Visual Studio.

Step 1

Launch Visual Studio and navigate to the "File" menu. A dropdown menu will unfold; within this menu, opt for "New". Subsequently, a side menu will materialize, prompting you to choose "Project".

Step 2

A new window will emerge. Within this window, access the search bar and type "Console Application". Identify the first program as one with the C# option and proceed by clicking "Next".

Step 3

Another configuration window will present itself. Here, input the project name, specify the project location, and then proceed by clicking "Next".

Step 4

The final window will surface, allowing you to select the target framework. Conclude the process by clicking "Create".

Installing CSV Library IronXL

Now that your project is set up, let's add the IronXL C# library. Follow these steps to install IronXL in your C# project.

Step 1

Open Visual Studio and navigate to Tools. A dropdown menu will appear. From this menu, select the NuGet Package Manager.

Step 2

In the NuGet Package Manager, choose the option for Solutions from the side menu that appears.

C# CSV Library (Developer Tutorial), Figure 1: Navigate to NuGet Package Manager Navigate to NuGet Package Manager

Step 3

A new window will pop up. Within this window, navigate to the browser tab. In the search bar, type "IronXL". A list of IronXL packages will be displayed. Choose the latest package from the list and click on the Install button.

C# CSV Library (Developer Tutorial), Figure 2: Search and install the IronXL package in NuGet Package Manager UI Search and install the IronXL package in NuGet Package Manager UI

You can also use the Package Manager Console to install the IronXL.

Creating a CSV File Using IronXL

This section will create a CSV file using the .NET library IronXL. Below is the source code for creating a CSV file using C# .NET Core.

using IronXL;

// Create a new workbook
WorkBook workBook = WorkBook.Create();

// Add a new worksheet named "new_sheet" to the workbook
WorkSheet workSheet = workBook.CreateWorkSheet("new_sheet");

// Populate the first row with data
workSheet["A1"].Value = "Hello World";
workSheet["B1"].Value = "500";
workSheet["C1"].Value = "CSV";
workSheet["D1"].Value = "Files";

// Save the workbook as a CSV file
workBook.SaveAs("test.csv");
using IronXL;

// Create a new workbook
WorkBook workBook = WorkBook.Create();

// Add a new worksheet named "new_sheet" to the workbook
WorkSheet workSheet = workBook.CreateWorkSheet("new_sheet");

// Populate the first row with data
workSheet["A1"].Value = "Hello World";
workSheet["B1"].Value = "500";
workSheet["C1"].Value = "CSV";
workSheet["D1"].Value = "Files";

// Save the workbook as a CSV file
workBook.SaveAs("test.csv");
Imports IronXL

' Create a new workbook
Private workBook As WorkBook = WorkBook.Create()

' Add a new worksheet named "new_sheet" to the workbook
Private workSheet As WorkSheet = workBook.CreateWorkSheet("new_sheet")

' Populate the first row with data
Private workSheet("A1").Value = "Hello World"
Private workSheet("B1").Value = "500"
Private workSheet("C1").Value = "CSV"
Private workSheet("D1").Value = "Files"

' Save the workbook as a CSV file
workBook.SaveAs("test.csv")
$vbLabelText   $csharpLabel

This C# code utilizes the IronXL library to create a new Excel WorkBook and adds a worksheet named "new_sheet" to it. The code then populates cells A1, B1, C1, and D1 with the values "Hello World", "500", "CSV", and "Files" respectively. Finally, it saves the workbook as a CSV file named "test.csv". The IronXL library simplifies Excel-related tasks in C#.

> OUTPUT:  
> Hello World,500,CSV,Files

Reading and Writing CSV Files

IronXL supports reading and writing CSV files in C# code. This section will read a CSV file and print the file in the console.

using IronXL;

// Load an existing CSV file into a workbook
WorkBook workBook = WorkBook.Load("test.new_sheet.csv");

// Access the first worksheet in the workbook
WorkSheet workSheet = workBook.WorkSheets[0];

// Print the worksheet to the console
System.Console.WriteLine(workSheet);
using IronXL;

// Load an existing CSV file into a workbook
WorkBook workBook = WorkBook.Load("test.new_sheet.csv");

// Access the first worksheet in the workbook
WorkSheet workSheet = workBook.WorkSheets[0];

// Print the worksheet to the console
System.Console.WriteLine(workSheet);
Imports IronXL

' Load an existing CSV file into a workbook
Private workBook As WorkBook = WorkBook.Load("test.new_sheet.csv")

' Access the first worksheet in the workbook
Private workSheet As WorkSheet = workBook.WorkSheets(0)

' Print the worksheet to the console
System.Console.WriteLine(workSheet)
$vbLabelText   $csharpLabel

This C# code uses the IronXL library to load an existing CSV file named "test.new_sheet.csv" as an Excel WorkBook using its filename. It then accesses the first WorkSheet in the workbook. Finally, it prints information about the worksheet to the console using System.Console.WriteLine(). The WorkBook.Load method is used to load an existing Excel file, and the workBook.WorkSheets[0] expression retrieves the first worksheet from the workbook.

C# CSV Library (Developer Tutorial), Figure 3: The Console output The Console output

Exporting a CSV Document Using IronXL

Using IronXL, developers can export a CSV file to other Excel formats. This code will export a CSV file into an XLSX file.

using IronXL;

// Load an existing CSV file into a workbook
WorkBook workBook = WorkBook.Load("test.new_sheet.csv");

// Access the first worksheet in the workbook
WorkSheet workSheet = workBook.WorkSheets[0];

// Save the workbook as an Excel file in XLSX format
workBook.SaveAs("sample.xlsx");
using IronXL;

// Load an existing CSV file into a workbook
WorkBook workBook = WorkBook.Load("test.new_sheet.csv");

// Access the first worksheet in the workbook
WorkSheet workSheet = workBook.WorkSheets[0];

// Save the workbook as an Excel file in XLSX format
workBook.SaveAs("sample.xlsx");
Imports IronXL

' Load an existing CSV file into a workbook
Private workBook As WorkBook = WorkBook.Load("test.new_sheet.csv")

' Access the first worksheet in the workbook
Private workSheet As WorkSheet = workBook.WorkSheets(0)

' Save the workbook as an Excel file in XLSX format
workBook.SaveAs("sample.xlsx")
$vbLabelText   $csharpLabel

This C# code utilizes the IronXL library to load an existing CSV file named "test.new_sheet.csv" as an Excel WorkBook. It then accesses the first worksheet in the WorkSheet. Subsequently, it saves the entire workbook, including the loaded worksheet, as an Excel file named "sample.xlsx" using the workBook.SaveAs("sample.xlsx") statement.

This code effectively converts an object from a CSV file to an Excel file, taking advantage of IronXL's functionality to handle different spreadsheet formats.

C# CSV Library (Developer Tutorial), Figure 4: The output Excel file The output Excel file

Conclusion

In conclusion, this comprehensive guide explores the realm of C# CSV file manipulation, emphasizing the significance of efficient libraries for handling structured data. CSV files play a vital role in data exchange and C#.

The step-by-step guide provides insights into setting up a Visual Studio project, installing the IronXL library, and demonstrates practical examples of creating, reading, and exporting CSV files. Ultimately, IronXL emerges as a powerful solution, enhancing the efficiency and ease of handling CSV data within C# applications.

The complete article on how to read a CSV file can be found in this blog. Also, the tutorial and code examples for creating CSV files are available.

Furthermore, IronXL also offers a wide range of features to interact with Excel WorkBook, WorkSheet, and Cells level, such as converting between popular formats, cell data formatting, and even managing charts.

IronXL offers a free trial, providing users with an opportunity to test out its complete functionality. To continue to benefit from using IronXL, developers can purchase a commercial license.

Frequently Asked Questions

What is IronXL?

IronXL is a powerful C# library specifically designed for reading, writing, and handling CSV and Excel files within C# applications.

How can I create a CSV file using IronXL in C#?

To create a CSV file using IronXL in C#, you can utilize the WorkBook.Create method to generate a new workbook, add a worksheet, populate it with data, and then save it as a CSV using the workBook.SaveAs method.

How do I install IronXL in a Visual Studio C# project?

To install IronXL in a Visual Studio C# project, navigate to the NuGet Package Manager within Visual Studio, search for IronXL, and install the latest package available.

Can IronXL convert CSV files to other formats?

Yes, IronXL can convert CSV files to other formats such as XLSX by loading the CSV file as a workbook and saving it in the desired format using the SaveAs method.

What are the benefits of using IronXL for CSV file handling?

IronXL provides a streamlined and efficient approach to handling CSV files, offering features like reading, writing, and converting files, making it a robust tool for managing structured data in C#.

Does IronXL offer a free trial?

Yes, IronXL offers a free trial that allows users to test its functionality before purchasing a commercial license.

How do I read a CSV file using IronXL?

To read a CSV file using IronXL, load the CSV file into a WorkBook using the WorkBook.Load method, access the desired worksheet, and process the data as needed.

What is required to start using IronXL in a new project?

To start using IronXL in a new project, you need to create a new Visual Studio C# project, install the IronXL library via NuGet Package Manager, and reference the IronXL namespace in your code.

Can IronXL manage Excel file features beyond CSV handling?

Yes, IronXL can manage various Excel file features, including converting between popular formats, cell data formatting, and chart management.

What makes IronXL a reliable tool for C# developers?

IronXL is considered reliable due to its comprehensive feature set for handling Excel and CSV files, ease of integration into existing C# projects, and efficient data processing capabilities.

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Export to CSV in .NET Core
NEXT >
How to Write A CSV File in C#