Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
Have you ever been stuck trying to parse CSV files and convert them to XLSX or XLSX files to CSV files for some important task in .NET, but couldn't figure out how to do so without writing tons of code?
Many CSV libraries exist to solve this issue. However, the IronXL C# Excel Library will be used in this blog to perform these tasks with just two lines of code.
To get started, all you need is Visual Studio, and follow the instructions to install in detail below.
Open the Visual Studio editor.
Go to the File menu in Visual Studio. Choose "New Project", then select Console Application.
Type the project name and choose the project location. Then, click the Create button to create the project. Select the required .NET Framework, as shown in the screenshot below:
Create a new C#. NET Console Application
The program.cs
file will open so you can enter the logic and create/run the application.
The IronXL library can be downloaded and installed in many different ways. Today, we will focus on two of these:
The NuGet Package Manager UI is available in Visual Studio to install the package directly into the project. The below screenshot shows how to open it.
Installing IronXL using Visual Studio NuGet Package Manager GUI
The Package Manager UI provides a Browse feature that displays a list of the package libraries that are offered on the NuGet website. Enter the "IronXL" keyword, as in the below screenshot, to find the IronXL package.
Locate the IronXL library in NuGet Package Manager by searching for it in the Browse section
Select the IronXL.Excel
package and click the Install button to add it to the project.
In the Visual Studio menu, go to Tools > NuGet Package Manager > click on Package Manager Console.
Access the NuGet Package Manager Console within Visual Studio from the Tools menu
The Package Manager Console will appear at the bottom of the screen. Just type the following command then press enter, and IronXL will be installed.
Install-Package IronXL.Excel
Install the IronXL library through Command-Line
Parsing CSVs manually requires writing a lot of precise code to get the job done, but with IronXL, it requires just a few lines of code.
By using only conventional C# code to parse CSV formatted files, you will have to use a lot of bulky code. Here is an example of code to achieve this.
using FileHelpers;
using System;
namespace parse_csv
{
[DelimitedRecord(",")]
public class Record
{
public string Name;
public string Age;
}
class Program
{
static void Main(string[] args)
{
// Create a FileHelperEngine for the Record class
var fileHelperEngine = new FileHelperEngine<Record>();
// Read records from the CSV file into an array
var records = fileHelperEngine.ReadFile(@"C:\File\records.csv");
// Print each record's Name and Age
foreach (var record in records)
{
Console.WriteLine(record.Name);
Console.WriteLine(record.Age);
}
}
}
}
using FileHelpers;
using System;
namespace parse_csv
{
[DelimitedRecord(",")]
public class Record
{
public string Name;
public string Age;
}
class Program
{
static void Main(string[] args)
{
// Create a FileHelperEngine for the Record class
var fileHelperEngine = new FileHelperEngine<Record>();
// Read records from the CSV file into an array
var records = fileHelperEngine.ReadFile(@"C:\File\records.csv");
// Print each record's Name and Age
foreach (var record in records)
{
Console.WriteLine(record.Name);
Console.WriteLine(record.Age);
}
}
}
}
Imports FileHelpers
Imports System
Namespace parse_csv
<DelimitedRecord(",")>
Public Class Record
Public Name As String
Public Age As String
End Class
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Create a FileHelperEngine for the Record class
Dim fileHelperEngine As New FileHelperEngine(Of Record)()
' Read records from the CSV file into an array
Dim records = fileHelperEngine.ReadFile("C:\File\records.csv")
' Print each record's Name and Age
For Each record In records
Console.WriteLine(record.Name)
Console.WriteLine(record.Age)
Next record
End Sub
End Class
End Namespace
But using IronXL this can be achieved in just a few lines of code.
Using IronXL you can parse CSV files from XLSX, XLS, TSV, and more. In this tutorial, we'll explore the following conversions:
Open Microsoft Excel and create a new XLSX file. Populate its rows and columns with some mock data. The below image shows the file used for all conversions in this tutorial.
Sample Excel data
Once you have your file ready, write the following example code and execute the program.
using IronXL;
class Program
{
static void Main()
{
// Load the XLSX file into a WorkBook object
WorkBook wb = WorkBook.Load("test.xlsx");
// Save the WorkBook as a CSV file
wb.SaveAsCsv("Parsed CSV.csv");
}
}
using IronXL;
class Program
{
static void Main()
{
// Load the XLSX file into a WorkBook object
WorkBook wb = WorkBook.Load("test.xlsx");
// Save the WorkBook as a CSV file
wb.SaveAsCsv("Parsed CSV.csv");
}
}
Imports IronXL
Friend Class Program
Shared Sub Main()
' Load the XLSX file into a WorkBook object
Dim wb As WorkBook = WorkBook.Load("test.xlsx")
' Save the WorkBook as a CSV file
wb.SaveAsCsv("Parsed CSV.csv")
End Sub
End Class
After the execution is completed a new file named Parsed CSV.csv will be created. Reading CSV files can be done in any editor or reader you like. The below image shows the output of the above command - our generated CSV data. In the output file, double quotation marks represent bold values.
The result of invoking WorkBook.SaveAsCsv method on the sample Excel Workbook
In this example, we will see how to convert XLS files into CSV format.
First, let's create an example XLS file that we can convert to CSV format.
A sample XLS file
Next, we will execute the block of code below to convert the sample XLS file into a CSV file.
using IronXL;
class Program
{
static void Main()
{
// Load the XLS file into a WorkBook object
WorkBook wb = WorkBook.Load("XLS.xls");
// Save the WorkBook as a CSV file
wb.SaveAsCsv("Example2.csv");
}
}
using IronXL;
class Program
{
static void Main()
{
// Load the XLS file into a WorkBook object
WorkBook wb = WorkBook.Load("XLS.xls");
// Save the WorkBook as a CSV file
wb.SaveAsCsv("Example2.csv");
}
}
Imports IronXL
Friend Class Program
Shared Sub Main()
' Load the XLS file into a WorkBook object
Dim wb As WorkBook = WorkBook.Load("XLS.xls")
' Save the WorkBook as a CSV file
wb.SaveAsCsv("Example2.csv")
End Sub
End Class
After the execution of the above code is finished, you will have a newly generated CSV file.
The result CSV file from saving using the code above
Spreadsheet applications frequently employ TSV files, or Tab-Separated Values files, to transfer data across databases. It saves a data table with tabs separating the data columns and each record being on a different line.
IronXL offers a CSV parser to parse CSV files from TSV format for better data management.
Let's get started with the example.
A sample TSV spreadsheet
using IronXL;
class Program
{
static void Main()
{
// Load the TSV file into a WorkBook object
WorkBook wb = WorkBook.Load("TSV.tsv");
// Save the WorkBook as a CSV file
wb.SaveAsCsv("Example3.csv");
}
}
using IronXL;
class Program
{
static void Main()
{
// Load the TSV file into a WorkBook object
WorkBook wb = WorkBook.Load("TSV.tsv");
// Save the WorkBook as a CSV file
wb.SaveAsCsv("Example3.csv");
}
}
Imports IronXL
Friend Class Program
Shared Sub Main()
' Load the TSV file into a WorkBook object
Dim wb As WorkBook = WorkBook.Load("TSV.tsv")
' Save the WorkBook as a CSV file
wb.SaveAsCsv("Example3.csv")
End Sub
End Class
Below is the output in CSV format. The output CSV file
This tutorial uses IronXL to parse varying file formats to CSV in C#.
Additionally, the IronXL library also provides the following features:
Check out IronXL's features, Code Examples and documentation content for more information about how IronXL works. Download IronXL and try out free for 30 Days with a trial license key. Visit the Licensing Page for more information about licensing terms and conditions.
Purchase the complete Iron Suite to retrieve licenses for all five Iron Software libraries for the price of two IronXL library licenses!
Thanks for reading!
IronXL is a C# Excel Library that simplifies the process of parsing CSV files and converting them to XLSX or XLS files (and vice versa) with minimal code. It is particularly useful for developers looking to efficiently handle Excel and CSV file manipulations in .NET applications.
You can install IronXL in a Visual Studio project using the NuGet Package Manager UI or the Visual Studio Command-Line. In the Package Manager UI, search for 'IronXL' and install it. Alternatively, use the command 'Install-Package IronXL.Excel' in the Package Manager Console.
Yes, IronXL can convert an XLSX file to a CSV file. You load the XLSX file into a WorkBook object and then use the SaveAsCsv method to create a CSV file.
Yes, IronXL supports parsing CSV files from TSV files. You load the TSV file into a WorkBook object and then save it as a CSV file using the SaveAsCsv method.
IronXL provides a wide range of features including data manipulation, data export, chart management, cell formatting, and compatibility with Excel encryption. It also supports custom Excel methods like freeze panels, formulas, and conditional formatting.
To use IronXL in a C# project, you need Visual Studio and the .NET Framework. The library can be installed via NuGet Package Manager in Visual Studio.
Yes, IronXL offers a free 30-day trial that can be downloaded from the NuGet website. This allows developers to test its features before committing to a purchase.
Yes, IronXL can handle conversions between various file formats such as XLSX, XLS, and TSV. It provides flexible solutions for managing different spreadsheet formats within .NET applications.