Test in a live environment
Test in production without watermarks.
Works wherever you need it to.
The ability to work with Excel files to generate reports and build databases has become essential for today's software applications. There are many libraries now available that allow users to do this without Microsoft Excel.
In this article, we are going to discuss and compare how to work with Microsoft Excel documents programmatically in C# .NET technologies using two of the most popular libraries, IronXL and Aspose Cells.
IronXL and Aspose Cells both provide the methods to create, edit, and read Excel documents in the .NET framework. The next question is to decide which C# Excel library is best suited to your project. This article will help you decide on the best option for your applications.
Let's look firstly at what both libraries have to offer, and then move on to the comparison itself.
IronXL is a .NET library that facilitates reading and editing Microsoft Excel documents with C#. IronXL.Excel is a standalone .NET software library for reading a wide range of spreadsheet formats. It does not require Microsoft Excel to be installed, nor does it depend on Interop.
IronXL is an intuitive C# API that allows you to read, edit and create Excel spreadsheet files in .NET with lightning-fast performance. IronXL fully supports .NET Core, .NET Framework, Xamarin, Mobile, Linux, macOS and Azure.
IronXL is a leading .NET core and .NET framework Excel spreadsheet library for C#.
Aspose.Cells for .NET is an Excel Spreadsheet Programming API to speed up spreadsheet management and processing tasks. The API supports the building of cross-platform applications that have the ability to generate, modify, convert, render and print spreadsheets. Moreover, it does not rely on Microsoft Excel or any Microsoft Office Interop components to be installed but instead offers a robust set of APIs that deal with all Excel formats as well as CSV and SpreadsheetML formats.
By integrating the API, developers can perform basic tasks such as managing multiple worksheets, creating spreadsheet content and styles from scratch, importing data into worksheets from different data sources, adding common and complex mathematical, financial, and text formulas, manipulating charts, pictures, comments, drawing objects, and much more.
Here are some standout features of Aspose Cells:
Rendering
Data Processing
Grid Suite
The rest of this article continues as follows:
Use the following steps to create a Console Application:
Now the project is created and we are almost ready to test the libraries. However, we still need to install and integrate them into our project. Let's install IronXL first.
You can download and install the IronXL library using the following methods:
Let’s take a closer look at each one.
Visual Studio provides the NuGet Package Manager to install NuGet packages in your projects. You can access it through the Project Menu, or by right-clicking your project in the Solution Explorer.
This can be done by visiting the NuGet site directly and downloading the package. The steps are:
Another way to download and install the IronXL C# Library is to make use of the following steps to install the IronXL NuGet package through the Developer Command Prompt.
Type the following command:
PM > Install-Package IronXL.Excel
using IronXL;
using IronXL;
Imports IronXL
All done! IronXL is downloaded, installed, and ready to use. However, before that, we should install Microsoft.Office.Interop.Excel.
After creating the project, access NuGet Package Manager through the Project Menu, or by right-clicking your project in the Solution Explorer.
Another way to download and install the Aspose Cells C# Library is to make use of the following steps to install the NuGet package through the Developer Command Prompt.
Type the following command:
PM> Install-Package Aspose.Cells
You can download any version of Aspose Cells directly from the website by clicking on this link: https://downloads.aspose.com/cells/net
Or:
download from the NuGet website: https://www.nuget.org/packages/Aspose.Cells/
using Aspose.Cells;
using Aspose.Cells;
Imports Aspose.Cells
A workbook is an Excel file containing multiple worksheets with rows and columns. Both the libraries provide the facility to create a new Excel workbook and sheets. Let's have a look at the code step-by-step.
It could not be any easier to create a new Excel Workbook using IronXL! It is just one line of code! Yes, really. Add the following code to your static void main function in the Program.cs file:
WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
WorkBook workbook = WorkBook.Create(ExcelFileFormat.XLSX);
Dim workbook As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX)
Both XLS (older Excel file version) and XLSX (current and newer file version) file formats can be created with IronXL.
And, it’s even simpler to create a default Worksheet:
var worksheet = workbook.CreateWorkSheet("IronXL Features");
var worksheet = workbook.CreateWorkSheet("IronXL Features");
Dim worksheet = workbook.CreateWorkSheet("IronXL Features")
You can now use the worksheet variable to set cell values and do almost everything an Excel file can do.
Save Excel files:
//Save spreadsheet
workbook.SaveAs("NewExcelFile.xlsx");
//Save spreadsheet
workbook.SaveAs("NewExcelFile.xlsx");
'Save spreadsheet
workbook.SaveAs("NewExcelFile.xlsx")
Before creating a spreadsheet and its content, a license object must be created to avoid the evaluation limitations if a licensed version was purchased. The code goes as follows:
// Create a License object
License license = new License();
// Set the license of Aspose.Cells to avoid the evaluation limitations
license.SetLicense(dataDir + "Aspose.Cells.lic");
// Create a License object
License license = new License();
// Set the license of Aspose.Cells to avoid the evaluation limitations
license.SetLicense(dataDir + "Aspose.Cells.lic");
' Create a License object
Dim license As New License()
' Set the license of Aspose.Cells to avoid the evaluation limitations
license.SetLicense(dataDir & "Aspose.Cells.lic")
Next, lets have a look at the code to create a workbook and worksheet. The code sample goes as follows:
// Instantiate a Workbook object that represents Excel file.
Workbook wb = new Workbook();
// When you create a new workbook, a default "Sheet1" is added to the workbook.
Worksheet sheet = wb.Worksheets [0];
// Instantiate a Workbook object that represents Excel file.
Workbook wb = new Workbook();
// When you create a new workbook, a default "Sheet1" is added to the workbook.
Worksheet sheet = wb.Worksheets [0];
' Instantiate a Workbook object that represents Excel file.
Dim wb As New Workbook()
' When you create a new workbook, a default "Sheet1" is added to the workbook.
Dim sheet As Worksheet = wb.Worksheets (0)
Save Excel files:
//Save spreadsheet
wb.Save("NewExcelFile.xlsx");
//Save spreadsheet
wb.Save("NewExcelFile.xlsx");
'Save spreadsheet
wb.Save("NewExcelFile.xlsx")
Both the libraries can open and read existing Excel documents. Let's have a look at the sample code.
The IronXL WorkBook class represents an Excel sheet. To open an Excel file using C#, we use WorkBook.Load and specify the path of the Excel file (.xlsx). The following one-line code is used to open the file for reading:
//Load WorkBook
var workbook = WorkBook.Load(@"Spreadsheets\\sample.xlsx");
//Load WorkBook
var workbook = WorkBook.Load(@"Spreadsheets\\sample.xlsx");
'Load WorkBook
Dim workbook = WorkBook.Load("Spreadsheets\\sample.xlsx")
Each WorkBook can have multiple WorkSheet objects. These represent worksheets in the Excel document. If the workbook contains worksheets, retrieve them by name as follows:
//Open Sheet for reading
var worksheet = workbook.GetWorkSheet("sheetnamegoeshere");
//Open Sheet for reading
var worksheet = workbook.GetWorkSheet("sheetnamegoeshere");
'Open Sheet for reading
Dim worksheet = workbook.GetWorkSheet("sheetnamegoeshere")
Code for reading the cell values:
// Read from Ranges of cells elegantly.
foreach (var cell in worksheet ["A2:A10"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
// Read from Ranges of cells elegantly.
foreach (var cell in worksheet ["A2:A10"])
{
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text);
}
' Read from Ranges of cells elegantly.
For Each cell In worksheet ("A2:A10")
Console.WriteLine("Cell {0} has value '{1}'", cell.AddressString, cell.Text)
Next cell
Reading Excel files in Aspose cells is also a straightforward task, as it can manage multiple worksheets easily. Take a look at the code below:
// Creating a Workbook object and opening an Excel file using its file path
var workbook = new Workbook(@"Spreadsheets\\sample.xlsx");
//Adding new sheet
Worksheet sheet = workbook1.Worksheets.Add("MySheet");
// Creating a Workbook object and opening an Excel file using its file path
var workbook = new Workbook(@"Spreadsheets\\sample.xlsx");
//Adding new sheet
Worksheet sheet = workbook1.Worksheets.Add("MySheet");
' Creating a Workbook object and opening an Excel file using its file path
Dim workbook As New Workbook("Spreadsheets\\sample.xlsx")
'Adding new sheet
Dim sheet As Worksheet = workbook1.Worksheets.Add("MySheet")
The code for reading cell values:
//Get Cells
Cells cells = sheet.Cells;
foreach (var cell in cells)
{
Console.WriteLine(cell.Value);
}
//Get Cells
Cells cells = sheet.Cells;
foreach (var cell in cells)
{
Console.WriteLine(cell.Value);
}
'Get Cells
Dim cells As Cells = sheet.Cells
For Each cell In cells
Console.WriteLine(cell.Value)
Next cell
Excel formulas are one of the most important features of working with Excel files. Both the libraries have a powerful formula calculation engine.
They provide the facility to work with formulas and easily apply them to cells.
After loading the workbook and worksheet, the following code sample can be used to either make changes to formulas, or be applied to specific cells. The code goes as follows:
// Set Formulas
worksheet ["A1"].Formula = "Sum(B8:C12)";
worksheet ["B8"].Formula = "=C9/C11";
worksheet ["G30"].Formula = "Max(C3:C7)";
// Force recalculate all formula values in all sheets.
workbook.EvaluateAll();
// Set Formulas
worksheet ["A1"].Formula = "Sum(B8:C12)";
worksheet ["B8"].Formula = "=C9/C11";
worksheet ["G30"].Formula = "Max(C3:C7)";
// Force recalculate all formula values in all sheets.
workbook.EvaluateAll();
' Set Formulas
worksheet ("A1").Formula = "Sum(B8:C12)"
worksheet ("B8").Formula = "=C9/C11"
worksheet ("G30").Formula = "Max(C3:C7)"
' Force recalculate all formula values in all sheets.
workbook.EvaluateAll()
You can also retrieve formulas and their values:
// Get the formula's calculated value. e.g. "52"
string formulaValue = worksheet ["G30"].Value;
//Get the formula as a string. e.g. "Max(C3:C7)"
string formulaString = worksheet ["G30"].Formula;
//Save your changes with updated formulas and calculated values.
workbook.Save();
// Get the formula's calculated value. e.g. "52"
string formulaValue = worksheet ["G30"].Value;
//Get the formula as a string. e.g. "Max(C3:C7)"
string formulaString = worksheet ["G30"].Formula;
//Save your changes with updated formulas and calculated values.
workbook.Save();
' Get the formula's calculated value. e.g. "52"
Dim formulaValue As String = worksheet ("G30").Value
'Get the formula as a string. e.g. "Max(C3:C7)"
Dim formulaString As String = worksheet ("G30").Formula
'Save your changes with updated formulas and calculated values.
workbook.Save()
With Aspose.Cells it is also easy to work with formulas. Add the below code to the Excel file being read. The code goes as follows:
//Setting values
Cells cells = sheet.Cells;
//Setting formula
cells ["A4"].Formula = "=SUM(A1:A3)";
// Calculating the results of formulas
workbook.CalculateFormula();
//Setting values
Cells cells = sheet.Cells;
//Setting formula
cells ["A4"].Formula = "=SUM(A1:A3)";
// Calculating the results of formulas
workbook.CalculateFormula();
'Setting values
Dim cells As Cells = sheet.Cells
'Setting formula
cells ("A4").Formula = "=SUM(A1:A3)"
' Calculating the results of formulas
workbook.CalculateFormula()
Following the above code you can set more formulas with ease.
Both IronXL and Aspose Cells provide the facility to convert Excel xlsx files to different formats, and from different formats to Excel workbooks.
The process is pretty simple and easy. It is usually done with one line of code.
CSV to Excel formats:
WorkBook workbook = WorkBook.LoadCSV("test.csv", fileFormat:
ExcelFileFormat.XLSX, ListDelimiter: ",");
WorkSheet ws = workbook.DefaultWorkSheet;
workbook.SaveAs("CsvToExcelConversion.xlsx");
WorkBook workbook = WorkBook.LoadCSV("test.csv", fileFormat:
ExcelFileFormat.XLSX, ListDelimiter: ",");
WorkSheet ws = workbook.DefaultWorkSheet;
workbook.SaveAs("CsvToExcelConversion.xlsx");
Dim workbook As WorkBook = WorkBook.LoadCSV("test.csv", fileFormat:= ExcelFileFormat.XLSX, ListDelimiter:= ",")
Dim ws As WorkSheet = workbook.DefaultWorkSheet
workbook.SaveAs("CsvToExcelConversion.xlsx")
XLSX file to XLSM:
WorkBook workbook = WorkBook.Load("test.xlsx");
//This is how you can export workbook to .xlsm format
workbook.SaveAs("test.xlsm");
WorkBook workbook = WorkBook.Load("test.xlsx");
//This is how you can export workbook to .xlsm format
workbook.SaveAs("test.xlsm");
Dim workbook As WorkBook = WorkBook.Load("test.xlsx")
'This is how you can export workbook to .xlsm format
workbook.SaveAs("test.xlsm")
Excel to HTML with options:
WorkBook workbook = WorkBook.Load("test.xlsx");
var options = new HtmlExportOptions()
{
//This is how we can make row/column numbers visible in html document
OutputRowNumbers = true,
OutputColumnHeaders = true,
OutputHiddenColumns = true,
//This is how we can make hidden rows/columns visible in html document
OutputHiddenRows = true,
OutputLeadingSpacesAsNonBreaking = true
};
//This is how we can export workbook to the HTML file
workbook.ExportToHtml("workbook.html",options);
WorkBook workbook = WorkBook.Load("test.xlsx");
var options = new HtmlExportOptions()
{
//This is how we can make row/column numbers visible in html document
OutputRowNumbers = true,
OutputColumnHeaders = true,
OutputHiddenColumns = true,
//This is how we can make hidden rows/columns visible in html document
OutputHiddenRows = true,
OutputLeadingSpacesAsNonBreaking = true
};
//This is how we can export workbook to the HTML file
workbook.ExportToHtml("workbook.html",options);
Dim workbook As WorkBook = WorkBook.Load("test.xlsx")
Dim options = New HtmlExportOptions() With {
.OutputRowNumbers = True,
.OutputColumnHeaders = True,
.OutputHiddenColumns = True,
.OutputHiddenRows = True,
.OutputLeadingSpacesAsNonBreaking = True
}
'This is how we can export workbook to the HTML file
workbook.ExportToHtml("workbook.html",options)
It is fairly simple to convert Excel xlsx files in Aspose cells as well. Take a look at the codes below:
Excel to HTML:
//Load your source workbook
Workbook workbook = new Workbook("Book1.xlsx");
//save file to html format
workbook.Save("out.html");
//Load your source workbook
Workbook workbook = new Workbook("Book1.xlsx");
//save file to html format
workbook.Save("out.html");
'Load your source workbook
Dim workbook As New Workbook("Book1.xlsx")
'save file to html format
workbook.Save("out.html")
ODs text to Excel:
//Load your source ods text file
Workbook workbook = new Workbook("book1.ods");
//Save as xlsx file
workbook.Save("ods_out.xlsx");
//Load your source ods text file
Workbook workbook = new Workbook("book1.ods");
//Save as xlsx file
workbook.Save("ods_out.xlsx");
'Load your source ods text file
Dim workbook As New Workbook("book1.ods")
'Save as xlsx file
workbook.Save("ods_out.xlsx")
Excel Worksheet to SVG formats:
// Instantiate a workbook
var workbook = new Workbook();
// Populate worksheets first cell
workbook.Worksheets [0].Cells ["A1"].Value = "DEMO TEXT ON SHEET1";
// Convert Worksheets
workbook.Save("ConvertWorksheetToSVG_out.svg");
// Instantiate a workbook
var workbook = new Workbook();
// Populate worksheets first cell
workbook.Worksheets [0].Cells ["A1"].Value = "DEMO TEXT ON SHEET1";
// Convert Worksheets
workbook.Save("ConvertWorksheetToSVG_out.svg");
' Instantiate a workbook
Dim workbook As New Workbook()
' Populate worksheets first cell
workbook.Worksheets (0).Cells ("A1").Value = "DEMO TEXT ON SHEET1"
' Convert Worksheets
workbook.Save("ConvertWorksheetToSVG_out.svg")
IronXL is an openly commercial C# Excel library. It is free for development and can always be licensed for commercial deployment. Licenses are available for single-project use, single developers, agencies, and global corporations, as well as SaaS and OEM redistribution. All licenses include a 30-day money-back guarantee, one year of product support and updates, validity for dev/staging/production, and also a permanent license (one-time purchase). The Lite package starts from $749.
Aspose Cells is an open-source API and can be licensed for commercial use. The evaluation version of Aspose.Cells product provides full product functionality, but it is limited to the opening of 100 files in one program and has an extra worksheet with the evaluation watermark. Technical support is free and unlimited and is provided both to licensed and evaluation users. If you want to test Aspose.Cells without evaluation version limitations, request a 30-day temporary license or buy a licensed version from this link. The developer small business package starts from $1199.
In comparison, IronXL and Aspose.Cells go head-to-head when working with Excel. IronXL and Aspose.Cells both provide advanced Excel functions along with basic Excel operations. IronXL is free for development while Aspose.Cells has an evaluation period with files limited to 100 per application. Here, the advantage lies with IronXL for non-commercial use. Moreover, IronXL also provides conversion to and from the JSON format.
9 .NET API products for your office documents