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
In this article, one of the most popular libraries, IronXL, an Excel library will be used to compare and contrast how to interact with Microsoft Excel documents programmatically in .NET technologies and it will create an Excel spreadsheet environment to read Excel worksheets, write Excel tables, and export spreadsheet files to CSV.
Documents created in Microsoft Excel can be read and converted to CSV files using the C# IronXL, .NET Excel library. IronXL is a standalone .NET Excel software library that doesn't need Microsoft Office or Microsoft.Office.Interop.Excel or Excel Interop to be installed. It can read several spreadsheet formats and is not just a static library.
IronXL's simple C# API makes it easy to read multiple worksheets, modify, and generate Excel spreadsheets in a .NET environment. It fully supports Xamarin, Linux, macOS, Azure, .NET Core applications, and .NET Framework.
The next sections of this article will show you how simple it is to create/read an Excel file with the IronXL library.
Step 1: Launching a new endeavor to create Excel files.
After starting Visual Studio, select "New Project" from the "File" menu.
In the resulting dialogue box, select the "Console App" .NET Project templates, then click "Next."
New Project
Once the new project's location has been entered in the Location field, you may enter any project name you choose for the Project. Click the Next button to proceed.
Project Configuration
A .NET Framework can be selected using the Framework drop-down option. Here, we're using .NET 6.0, the long-term supported version. Then press Create.
Framework Selection
Get the IronXL library, which is required for the following solution. To do this, enter the following command into the NuGet Package Manager Console:
Install-Package IronXL.Excel
IronXL Installation
Another option is to use the NuGet Package Manager to search for the package "IronXL". Then, select the desired package to download from the list of all NuGet packages related to IronXL.
NuGet Package Manager
With IronXL, you will be able to create new Excel files with just a few lines of code! IronXL can be used to create files in the XLS (an older Excel format) and XLSX (a more current format) as shown in the code samples below.
using IronXL;
public class IronXLExample
{
public static void Main(string[] args)
{
// Create a new workbook
WorkBook workbook = WorkBook.Create();
// Create a new worksheet named "Sheet1"
WorkSheet worksheet = workbook.CreateWorkSheet("Sheet1");
// Set the value of cell A1
worksheet["A1"].Value = "test";
// Save the workbook to a file
workbook.SaveAs("sample1.xlsx");
}
}
using IronXL;
public class IronXLExample
{
public static void Main(string[] args)
{
// Create a new workbook
WorkBook workbook = WorkBook.Create();
// Create a new worksheet named "Sheet1"
WorkSheet worksheet = workbook.CreateWorkSheet("Sheet1");
// Set the value of cell A1
worksheet["A1"].Value = "test";
// Save the workbook to a file
workbook.SaveAs("sample1.xlsx");
}
}
Imports IronXL
Public Class IronXLExample
Public Shared Sub Main(ByVal args() As String)
' Create a new workbook
Dim workbook As WorkBook = WorkBook.Create()
' Create a new worksheet named "Sheet1"
Dim worksheet As WorkSheet = workbook.CreateWorkSheet("Sheet1")
' Set the value of cell A1
worksheet("A1").Value = "test"
' Save the workbook to a file
workbook.SaveAs("sample1.xlsx")
End Sub
End Class
The code above demonstrates the use of the IronXL package to create a new Excel workbook and worksheet. The WorkBook.Create()
method is used to create a new workbook, and then the CreateWorkSheet("Sheet1")
function creates a worksheet tab with the specified name. Changes are saved in a specified location using the SaveAs
method.
Excel Output
To learn more about creating Excel files, check this tutorial to create one in .NET.
Exporting data to the XLSX or XLS formats requires only a few lines of code. Below is an example of source code that can be used to export data from an Excel file into a simple tabular format:
using IronXL;
public class ExcelReadExportExample
{
public static void Main(string[] args)
{
// Load an existing Excel file
var workbook = WorkBook.LoadExcel("Demo file.xlsx");
// Get a worksheet from the workbook
WorkSheet workSheet = workbook.GetWorkSheet("Sheet1");
// Read the value of cell A1
string addressVal = workSheet["A1"].ToString();
Console.WriteLine(addressVal);
// Modify the value of cell A2
workSheet["A2"].Value = "test";
// Save the workbook in multiple formats
workbook.SaveAs("export.xlsx");
// Or save as XLS
workbook.SaveAs("export.xls");
// Or save the specific worksheet as an XLS file
workbook.WorkSheets[0].SaveAs("export.xls");
}
}
using IronXL;
public class ExcelReadExportExample
{
public static void Main(string[] args)
{
// Load an existing Excel file
var workbook = WorkBook.LoadExcel("Demo file.xlsx");
// Get a worksheet from the workbook
WorkSheet workSheet = workbook.GetWorkSheet("Sheet1");
// Read the value of cell A1
string addressVal = workSheet["A1"].ToString();
Console.WriteLine(addressVal);
// Modify the value of cell A2
workSheet["A2"].Value = "test";
// Save the workbook in multiple formats
workbook.SaveAs("export.xlsx");
// Or save as XLS
workbook.SaveAs("export.xls");
// Or save the specific worksheet as an XLS file
workbook.WorkSheets[0].SaveAs("export.xls");
}
}
Imports IronXL
Public Class ExcelReadExportExample
Public Shared Sub Main(ByVal args() As String)
' Load an existing Excel file
Dim workbook = WorkBook.LoadExcel("Demo file.xlsx")
' Get a worksheet from the workbook
Dim workSheet As WorkSheet = workbook.GetWorkSheet("Sheet1")
' Read the value of cell A1
Dim addressVal As String = workSheet("A1").ToString()
Console.WriteLine(addressVal)
' Modify the value of cell A2
workSheet("A2").Value = "test"
' Save the workbook in multiple formats
workbook.SaveAs("export.xlsx")
' Or save as XLS
workbook.SaveAs("export.xls")
' Or save the specific worksheet as an XLS file
workbook.WorkSheets(0).SaveAs("export.xls")
End Sub
End Class
In the preceding example, an existing Excel file is loaded using the LoadExcel
method, which takes the file name and path as an argument. The file is then imported into the WorkBook
object. Worksheets are loaded using the GetWorkSheet
function by specifying the sheet name. The value of a cell can be read by specifying its address. The code also demonstrates modifying a worksheet value and saving the workbook in different file formats using the SaveAs
method.
Console Output
A worksheet can also be referenced either by name or index value to export data from an Excel spreadsheet to a separate file. For more details, visit another tutorial to learn more about exporting Excel files.
From creating new files to carrying out precise calculations, IronXL has you covered for all your Excel projects. Today, we took a closer look at how IronXL can be utilized to create, read, and export Excel files, and saw how with just a few lines of code you can have full control over these processes.
For every programmable activity about Microsoft Excel documents, IronXL offers a comprehensive solution. It is possible to perform formula calculations, sort strings or numbers, cut and add data, search and replace, merge and unmerge cells, visualize data, and save files. With it, you can also set cell data types and validate spreadsheet data. The ability to read and write CSV files enables interaction with Excel data.
IronXL costs $749 upon launch, but customers can opt to pay a one-year membership fee for improvements and product support. IronXL levies an additional security fee that permits unrestricted redistribution. Visit this licensing page to learn more about the specifics of the pricing.
IronXL is a C# library that allows users to interact with Microsoft Excel documents programmatically in .NET technologies without the need for Microsoft Office or Excel Interop.
IronXL supports almost every .NET Framework including Web apps, Windows Forms, Console, and is compatible with .NET Core applications. It also works on Linux, macOS, and Windows operating systems.
You can create a new Excel file with IronXL using a simple C# script. By utilizing the WorkBook.Create() and CreateWorkSheet() methods, you can generate Excel files in various formats such as XLS and XLSX.
Yes, IronXL allows you to read and export data from Excel files. You can use methods like LoadExcel and GetWorkSheet to load and manipulate Excel files, and then export them in different formats using the SaveAs method.
No, IronXL is a standalone .NET Excel software library that does not require Microsoft Office or Microsoft.Office.Interop.Excel to be installed.
IronXL supports a wide range of Excel file types including XLSX, CSV, XLS, XLST, TSV, XLSM, and more.
Yes, IronXL is capable of producing calculations within Excel spreadsheets, allowing you to perform various computational tasks programmatically.
Yes, IronXL supports numerous data types for Excel columns including text, integers, dates, currencies, formulas, and percentages.
IronXL costs a certain amount upon launch, and customers can opt to pay a one-year membership fee for improvements and product support. Additional fees apply for security and unrestricted redistribution rights.
You can install IronXL using the NuGet Package Manager Console with the command Install-Package IronXL.Excel, or search for 'IronXL' in the NuGet Package Manager to add it to your project.