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
This article will introduce how to write CSV files using a C# library named IronXL in a new project.
Install the C# library for writing into a CSV File.
Use WorkBook.Create
to create a new workbook.
Create a new worksheet using WorkBook.CreateWorkSheet
.
Add values to individual cells using workSheet["cell name"].Value
using var
.
SaveAs
method.IronXL emerges as a beacon of efficiency for C# developers seeking a seamless and powerful solution for writing data to CSV files as compared to the CSVHelper NuGet package. In the dynamic landscape of software development, the ability to handle and manipulate data is paramount, and IronXL steps up to the plate with its robust set of tools tailored for C#.
This article delves into the features and methodologies that make IronXL the go-to choice for C# developers looking to enhance the process of writing data to CSV files, striking the perfect balance between simplicity and precision.
To begin using the IronXL library, the initial step involves either creating a fresh Visual Studio C# project or loading an existing one. Here are the instructions for generating a new project in Visual Studio:
Open Visual Studio and navigate to the "File" menu. A drop-down menu will be displayed; within this menu, select "New". This action will reveal another side menu.
File Menu
In the side menu, locate and click on "Project". This will open a new window. Within this window, utilize the search bar to find "Console Application". Choose the option associated with C# and proceed by clicking the Next button.
New Project- Console Application
A configuration window will appear next. Enter the project name, specify the project location, and click on the Next button.
Configure Project
The final window will emerge. Here, pick the target framework and initiate the project creation process by clicking on the Create button.
Target Framework
Now that you've set up the project, it's time to incorporate the IronXL C# library. Follow these steps to install IronXL in your project:
Within the NuGet Package Manager, choose the Manage NuGet Packages for Solutions from the side menu that unfolds.
NuGet Packages
A new window will pop up. Head to the browser tab within this window, and in the search bar, type "IronXL". You'll see a list of IronXL packages; opt for the latest one and proceed to click on the Install button.
IronXL
Write data into a CSV file using the C# CSV library IronXL. In this section, we will create a new CSV file and write data in it. The following example uses the IronXL library to create a simple receipt in a CSV file. Let's break down the program code step by step.
System.Linq
using IronXL;
using System.Linq;
// This is the main class where execution starts.
public class Program {
static void Main() {
// Main method where all logic is executed
}
}
using IronXL;
using System.Linq;
// This is the main class where execution starts.
public class Program {
static void Main() {
// Main method where all logic is executed
}
}
Imports IronXL
Imports System.Linq
' This is the main class where execution starts.
Public Class Program
Shared Sub Main()
' Main method where all logic is executed
End Sub
End Class
These lines import the necessary classes and functionalities from the IronXL library for working with Excel files and the LINQ extension methods from the System.Linq
namespace.
WorkBook
and WorkSheet
// Create a new workbook. This serves as the container for all worksheets.
WorkBook workBook = WorkBook.Create();
// Create a new worksheet within the workbook named "Receipt".
WorkSheet workSheet = workBook.CreateWorkSheet("Receipt");
// Create a new workbook. This serves as the container for all worksheets.
WorkBook workBook = WorkBook.Create();
// Create a new worksheet within the workbook named "Receipt".
WorkSheet workSheet = workBook.CreateWorkSheet("Receipt");
' Create a new workbook. This serves as the container for all worksheets.
Dim workBook As WorkBook = WorkBook.Create()
' Create a new worksheet within the workbook named "Receipt".
Dim workSheet As WorkSheet = workBook.CreateWorkSheet("Receipt")
This code creates a new Excel workbook (WorkBook
) and a worksheet (WorkSheet
) within that workbook named "Receipt".
// Set the header row for columns. Headers added for better understanding of data.
workSheet["A1"].Value = "Product";
workSheet["B1"].Value = "Price";
// Set the header row for columns. Headers added for better understanding of data.
workSheet["A1"].Value = "Product";
workSheet["B1"].Value = "Price";
' Set the header row for columns. Headers added for better understanding of data.
workSheet("A1").Value = "Product"
workSheet("B1").Value = "Price"
These lines set the header row for the columns in the first row of the worksheet, labeling each column.
// Populate worksheet with product data and their respective prices.
workSheet["A2"].Value = "Item 1";
workSheet["B2"].DoubleValue = 20.10;
workSheet["A3"].Value = "Item 2";
workSheet["B3"].DoubleValue = 15.50;
workSheet["A4"].Value = "Item 3";
workSheet["B4"].DoubleValue = 10.25;
// Populate worksheet with product data and their respective prices.
workSheet["A2"].Value = "Item 1";
workSheet["B2"].DoubleValue = 20.10;
workSheet["A3"].Value = "Item 2";
workSheet["B3"].DoubleValue = 15.50;
workSheet["A4"].Value = "Item 3";
workSheet["B4"].DoubleValue = 10.25;
' Populate worksheet with product data and their respective prices.
workSheet("A2").Value = "Item 1"
workSheet("B2").DoubleValue = 20.10
workSheet("A3").Value = "Item 2"
workSheet("B3").DoubleValue = 15.50
workSheet("A4").Value = "Item 3"
workSheet("B4").DoubleValue = 10.25
These lines add information about three items, including their names and prices in the worksheet.
// Define the range for price values to be summed.
var range = workSheet["B2:B4"];
// Calculate the sum of prices.
decimal sum = range.Sum();
// Define the range for price values to be summed.
var range = workSheet["B2:B4"];
// Calculate the sum of prices.
decimal sum = range.Sum();
' Define the range for price values to be summed.
Dim range = workSheet("B2:B4")
' Calculate the sum of prices.
Dim sum As Decimal = range.Sum()
Using LINQ, this code calculates the sum of the prices from cells B2 to B4. The sum is stored in the sum
variable.
WorkSheet
// Display the sum in the console.
System.Console.WriteLine(sum);
// Update the total price in the worksheet.
workSheet["B5"].Value = sum;
// Display the sum in the console.
System.Console.WriteLine(sum);
// Update the total price in the worksheet.
workSheet["B5"].Value = sum;
' Display the sum in the console.
System.Console.WriteLine(sum)
' Update the total price in the worksheet.
workSheet("B5").Value = sum
The sum is printed to the console, and it is also updated in cell B5 of the worksheet.
// Save the workbook as a CSV file named "receipt.csv".
workBook.SaveAs("receipt.csv");
// Save the workbook as a CSV file named "receipt.csv".
workBook.SaveAs("receipt.csv");
' Save the workbook as a CSV file named "receipt.csv".
workBook.SaveAs("receipt.csv")
Finally, the entire workbook is saved as a CSV file named "receipt.csv".
In summary, this code creates a basic receipt in an Excel worksheet using IronXL, calculates the total price, prints it to the console, and then saves the workbook output as a CSV file. The receipt includes columns for "Product" and "Price", and it calculates the total price based on the individual item prices.
Receipt CSV File Output with Header
This comprehensive article underscores the significance of writing CSV files in C# and elucidates the process using the IronXL library. It emphasizes the fundamental nature of this skill in diverse data-centric applications and showcases IronXL's prowess in simplifying and optimizing data manipulation tasks within the C# ecosystem. The step-by-step approach, from project setup to utilizing IronXL to create a receipt and save it as a CSV file, provides developers with a practical understanding of the seamless integration between C#.
By offering versatility and efficiency, IronXL emerges as a valuable tool for C# developers seeking to enhance their program and ability to handle and export data in the ubiquitous CSV format, making it a crucial asset for various software development scenarios.
IronXL provides a solution for all Excel-related tasks to be done programmatically whether it be formula calculation, string sorting, trimming, finding and replacing, merging and unmerging, saving files etc. You can also set cell data formats.
For the complete tutorial on writing into a CSV file, visit this blog here. The code example for creating a CSV file can be found in the following blog.
IronXL offers a free trial, allowing you to evaluate its capabilities. If you find it useful for your projects, you can purchase a license starting from $749.
IronXL is a C# library used for handling Excel files, including writing data to CSV files. It offers a powerful and efficient solution for C# developers.
To install IronXL, open Visual Studio, navigate to the Tools menu, and select the NuGet Package Manager. Then, search for 'IronXL' in the NuGet Package Manager and install the latest version.
You can write a CSV file by creating a workbook and worksheet using IronXL, adding data to the worksheet, and then saving it as a CSV file using the SaveAs method.
Yes, IronXL can be used to create new Excel workbooks programmatically, allowing you to add worksheets and manipulate data.
IronXL offers a more comprehensive set of tools for handling Excel-related tasks, providing a seamless integration for C# developers compared to CSVHelper.
To create a new project, open Visual Studio, select 'New' from the File menu, choose 'Project', find 'Console Application' for C#, and configure the project settings.
You can add data to a worksheet by accessing individual cells using the cell name and setting their values using the Value property.
Yes, IronXL offers a free trial, allowing developers to evaluate its capabilities before purchasing a license.
IronXL provides functionalities like formula calculation, string sorting, trimming, finding and replacing, merging and unmerging cells, and saving files in various formats.
You can calculate the sum of a range of cells using LINQ in IronXL by defining the cell range and using the Sum method.