Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
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. It is not just a static library.
IronXL's simple C# API makes it simple to read multiple worksheets, modify, and generate Excel spreadsheets in a .NET environment and its not just a static library. IronXL completely 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 code 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 version of Excel) and XLSX (a current and newer version) file formats as shown in the code samples below.
using IronXL;
WorkBook wb = WorkBook.Create();
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
ws["A1"].Value = "test";
wb.SaveAs("sample1.xlsx");
using IronXL;
WorkBook wb = WorkBook.Create();
WorkSheet ws = wb.CreateWorkSheet("Sheet1");
ws["A1"].Value = "test";
wb.SaveAs("sample1.xlsx");
IRON VB CONVERTER ERROR developers@ironsoftware.com
To access all functions, the code above calls the IronXL package first. The WorkBook
is then used to generate a workbook using the Create
method. The name inside the brackets is used to create a worksheet tab using the wb.CreateWorkSheet("Sheet1")
function after the workbook has been created. Following the aforementioned procedures, the modifications are stored in a designated area using SaveAs
function.
Excel Output
To learn more about creating Excel check this tutorial to create one in .NET.
Exporting data to the XLSX or XLS formats only requires 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:
var workbook = IronXL.WorkBook.LoadExcel("Demo file.xlsx");
WorkSheet workSheet = workbook.GetWorkSheet("Sheet1");
string addressVal = workSheet["A1"].ToString();
Console.WriteLine(addressVal);
workSheet["A2"].Value = "test";
workbook.SaveAs("export.xlsx");
//or
workbook.SaveAs("export.xls");
//or
workbook.WorkSheets[0].SaveAs("export.xls");
var workbook = IronXL.WorkBook.LoadExcel("Demo file.xlsx");
WorkSheet workSheet = workbook.GetWorkSheet("Sheet1");
string addressVal = workSheet["A1"].ToString();
Console.WriteLine(addressVal);
workSheet["A2"].Value = "test";
workbook.SaveAs("export.xlsx");
//or
workbook.SaveAs("export.xls");
//or
workbook.WorkSheets[0].SaveAs("export.xls");
Dim workbook = IronXL.WorkBook.LoadExcel("Demo file.xlsx")
Dim workSheet As WorkSheet = workbook.GetWorkSheet("Sheet1")
Dim addressVal As String = workSheet("A1").ToString()
Console.WriteLine(addressVal)
workSheet("A2").Value = "test"
workbook.SaveAs("export.xlsx")
'or
workbook.SaveAs("export.xls")
'or
workbook.WorkSheets(0).SaveAs("export.xls")
First, an Excel file that already exists is loaded in the preceding example using the LoadExcel
method, which allows the file name and file path as an argument. The file has now been imported into the WorkBook
object. Then the Excel worksheets are loaded with the help of the GetWorkSheet
using the sheetname. Then the Excel address is used to read the value. To know more about reading Excel files, visit this tutorial.
Also, it is possible to modify the Excel sheet values by using the same Excel address. The SaveAs
function can be used to save the Excel document as an XLSX or XLS file. This method saves the full file in the chosen format.
Console Output
Furthermore, an Excel worksheet can be selected by referring to it by name or by using its index value. Next, use the SaveAs
option to export the data from the Excel spreadsheet to a separate file. For more details, visit another tutorial to learn more about export 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.
9 .NET API products for your office documents