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 video, a comparative analysis is conducted between two prominent .NET libraries, IronXL and ClosedXML, focusing on their capabilities in reading Excel files. Both libraries are popular for handling Excel data, but they have distinct differences. The video begins by demonstrating how to read an Excel file using the ClosedXML library, which involves creating an instance of the Excel workbook class and reading the data with a for
loop. This approach is noted as somewhat complex. In contrast, the IronXL library is showcased for its simplicity and versatility. It allows reading the Excel file using a foreach
loop, offering support for almost all spreadsheet formats, unlike ClosedXML, which is limited to Excel files only. The video concludes by recommending IronXL for users seeking a library with broader file compatibility and ease of use. Viewers are encouraged to subscribe for more tutorials and try a trial subscription of Iron Software for further exploration.
Here’s a code example showing how to read an Excel file using both libraries:
// Using the ClosedXML library
using ClosedXML.Excel;
class Program
{
static void Main()
{
// Load the Excel workbook
using (var workbook = new XLWorkbook("example.xlsx"))
{
// Get the first worksheet in the workbook
var worksheet = workbook.Worksheet(1);
// Loop through the rows in the worksheet
for (int i = 1; i <= worksheet.LastRowUsed().RowNumber(); i++)
{
// Access cells in the row. Assume the first column for data.
var cellValue = worksheet.Cell(i, 1).Value;
System.Console.WriteLine($"Row {i}: {cellValue}");
}
}
}
}
// Using the ClosedXML library
using ClosedXML.Excel;
class Program
{
static void Main()
{
// Load the Excel workbook
using (var workbook = new XLWorkbook("example.xlsx"))
{
// Get the first worksheet in the workbook
var worksheet = workbook.Worksheet(1);
// Loop through the rows in the worksheet
for (int i = 1; i <= worksheet.LastRowUsed().RowNumber(); i++)
{
// Access cells in the row. Assume the first column for data.
var cellValue = worksheet.Cell(i, 1).Value;
System.Console.WriteLine($"Row {i}: {cellValue}");
}
}
}
}
' Using the ClosedXML library
Imports ClosedXML.Excel
Friend Class Program
Shared Sub Main()
' Load the Excel workbook
Using workbook = New XLWorkbook("example.xlsx")
' Get the first worksheet in the workbook
Dim worksheet = workbook.Worksheet(1)
' Loop through the rows in the worksheet
Dim i As Integer = 1
Do While i <= worksheet.LastRowUsed().RowNumber()
' Access cells in the row. Assume the first column for data.
Dim cellValue = worksheet.Cell(i, 1).Value
System.Console.WriteLine($"Row {i}: {cellValue}")
i += 1
Loop
End Using
End Sub
End Class
// Using the IronXL library
using IronXL;
class Program
{
static void Main()
{
// Load the Excel workbook
WorkBook workbook = WorkBook.Load("example.xlsx");
// Get the first worksheet in the workbook
WorkSheet sheet = workbook.WorkSheets.First();
// Iterate through rows using foreach
foreach (var row in sheet.Rows)
{
// Access cells in the row. Assume the first column for data.
var cellValue = row.Columns[0].Value;
System.Console.WriteLine($"Row {row.RowIndex}: {cellValue}");
}
}
}
// Using the IronXL library
using IronXL;
class Program
{
static void Main()
{
// Load the Excel workbook
WorkBook workbook = WorkBook.Load("example.xlsx");
// Get the first worksheet in the workbook
WorkSheet sheet = workbook.WorkSheets.First();
// Iterate through rows using foreach
foreach (var row in sheet.Rows)
{
// Access cells in the row. Assume the first column for data.
var cellValue = row.Columns[0].Value;
System.Console.WriteLine($"Row {row.RowIndex}: {cellValue}");
}
}
}
' Using the IronXL library
Imports IronXL
Friend Class Program
Shared Sub Main()
' Load the Excel workbook
Dim workbook As WorkBook = WorkBook.Load("example.xlsx")
' Get the first worksheet in the workbook
Dim sheet As WorkSheet = workbook.WorkSheets.First()
' Iterate through rows using foreach
For Each row In sheet.Rows
' Access cells in the row. Assume the first column for data.
Dim cellValue = row.Columns(0).Value
System.Console.WriteLine($"Row {row.RowIndex}: {cellValue}")
Next row
End Sub
End Class
Further Reading: A Comparison Between IronXL and ClosedXML