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 tutorial, we explore the IronXL and NPOI libraries for handling Excel files within a C# Windows Form application. Beginning with the installation of the necessary libraries via NuGet, the tutorial dives into creating a simple GUI with buttons for loading Excel files and exporting data to CSV and JSON formats. The process highlights the simplicity and speed of IronXL in comparison to the more complex and time-consuming NPOI library. IronXL's capability to convert Excel sheets directly into data tables with minimal code stands out, while NPOI lacks this functionality and takes longer to process large datasets. Additionally, only IronXL supports exporting Excel files to other formats like CSV and JSON. Throughout the tutorial, IronXL demonstrates its superior user-friendliness and efficiency, making it the preferred choice for developers looking to handle Excel files easily and effectively. The tutorial concludes with a demonstration of exporting files, reinforcing IronXL’s capabilities in providing swift and error-free operations.
Further Reading: A Comparison of NPOI vs IronXL
// Sample code demonstrating basic usage of IronXL to read and export Excel files
using IronXL; // Include the IronXL namespace
namespace ExcelExample
{
class Program
{
static void Main(string[] args)
{
// Load the Excel file
var workbook = WorkBook.Load("example.xlsx");
// Select the first worksheet
WorkSheet sheet = workbook.WorkSheets.First();
// Export the worksheet data to a data table
var dataTable = sheet.ToDataTable();
// Display the data from the data table
Console.WriteLine("Data from Excel sheet:");
foreach (var row in dataTable.Rows)
{
foreach (var cell in row.ItemArray)
{
Console.Write($"{cell} ");
}
Console.WriteLine();
}
// Export the worksheet to a CSV file
Console.WriteLine("Exporting to CSV...");
sheet.SaveAs("output.csv");
// Export the worksheet to a JSON file
Console.WriteLine("Exporting to JSON...");
sheet.SaveAsJson("output.json");
Console.WriteLine("Export completed successfully.");
}
}
}
// Sample code demonstrating basic usage of IronXL to read and export Excel files
using IronXL; // Include the IronXL namespace
namespace ExcelExample
{
class Program
{
static void Main(string[] args)
{
// Load the Excel file
var workbook = WorkBook.Load("example.xlsx");
// Select the first worksheet
WorkSheet sheet = workbook.WorkSheets.First();
// Export the worksheet data to a data table
var dataTable = sheet.ToDataTable();
// Display the data from the data table
Console.WriteLine("Data from Excel sheet:");
foreach (var row in dataTable.Rows)
{
foreach (var cell in row.ItemArray)
{
Console.Write($"{cell} ");
}
Console.WriteLine();
}
// Export the worksheet to a CSV file
Console.WriteLine("Exporting to CSV...");
sheet.SaveAs("output.csv");
// Export the worksheet to a JSON file
Console.WriteLine("Exporting to JSON...");
sheet.SaveAsJson("output.json");
Console.WriteLine("Export completed successfully.");
}
}
}
' Sample code demonstrating basic usage of IronXL to read and export Excel files
Imports IronXL ' Include the IronXL namespace
Namespace ExcelExample
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Load the Excel file
Dim workbook = WorkBook.Load("example.xlsx")
' Select the first worksheet
Dim sheet As WorkSheet = workbook.WorkSheets.First()
' Export the worksheet data to a data table
Dim dataTable = sheet.ToDataTable()
' Display the data from the data table
Console.WriteLine("Data from Excel sheet:")
For Each row In dataTable.Rows
For Each cell In row.ItemArray
Console.Write($"{cell} ")
Next cell
Console.WriteLine()
Next row
' Export the worksheet to a CSV file
Console.WriteLine("Exporting to CSV...")
sheet.SaveAs("output.csv")
' Export the worksheet to a JSON file
Console.WriteLine("Exporting to JSON...")
sheet.SaveAsJson("output.json")
Console.WriteLine("Export completed successfully.")
End Sub
End Class
End Namespace
// Sample code demonstrating basic usage of NPOI to read an Excel file
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel; // Include necessary NPOI namespaces
using System.IO;
namespace ExcelExampleNPOI
{
class Program
{
static void Main(string[] args)
{
// Open the Excel file as a file stream
using (FileStream file = new FileStream("example.xlsx", FileMode.Open, FileAccess.Read))
{
// Initialize a workbook instance
IWorkbook workbook = new XSSFWorkbook(file);
// Access the first sheet of the workbook
ISheet sheet = workbook.GetSheetAt(0);
// Iterate through each row and cell to read the data
for (int rowIndex = 0; rowIndex <= sheet.LastRowNum; rowIndex++)
{
IRow row = sheet.GetRow(rowIndex);
if (row != null) // Ensure the row is not null
{
for (int cellIndex = 0; cellIndex < row.LastCellNum; cellIndex++)
{
ICell cell = row.GetCell(cellIndex);
if (cell != null)
{
// Output cell content based on its type
switch (cell.CellType)
{
case CellType.String:
Console.Write($"{cell.StringCellValue} ");
break;
case CellType.Numeric:
Console.Write($"{cell.NumericCellValue} ");
break;
case CellType.Boolean:
Console.Write($"{cell.BooleanCellValue} ");
break;
default:
Console.Write($"{cell} ");
break;
}
}
}
Console.WriteLine();
}
}
}
Console.WriteLine("Excel data reading completed with NPOI.");
}
}
}
// Sample code demonstrating basic usage of NPOI to read an Excel file
using NPOI.SS.UserModel;
using NPOI.XSSF.UserModel; // Include necessary NPOI namespaces
using System.IO;
namespace ExcelExampleNPOI
{
class Program
{
static void Main(string[] args)
{
// Open the Excel file as a file stream
using (FileStream file = new FileStream("example.xlsx", FileMode.Open, FileAccess.Read))
{
// Initialize a workbook instance
IWorkbook workbook = new XSSFWorkbook(file);
// Access the first sheet of the workbook
ISheet sheet = workbook.GetSheetAt(0);
// Iterate through each row and cell to read the data
for (int rowIndex = 0; rowIndex <= sheet.LastRowNum; rowIndex++)
{
IRow row = sheet.GetRow(rowIndex);
if (row != null) // Ensure the row is not null
{
for (int cellIndex = 0; cellIndex < row.LastCellNum; cellIndex++)
{
ICell cell = row.GetCell(cellIndex);
if (cell != null)
{
// Output cell content based on its type
switch (cell.CellType)
{
case CellType.String:
Console.Write($"{cell.StringCellValue} ");
break;
case CellType.Numeric:
Console.Write($"{cell.NumericCellValue} ");
break;
case CellType.Boolean:
Console.Write($"{cell.BooleanCellValue} ");
break;
default:
Console.Write($"{cell} ");
break;
}
}
}
Console.WriteLine();
}
}
}
Console.WriteLine("Excel data reading completed with NPOI.");
}
}
}
' Sample code demonstrating basic usage of NPOI to read an Excel file
Imports NPOI.SS.UserModel
Imports NPOI.XSSF.UserModel ' Include necessary NPOI namespaces
Imports System.IO
Namespace ExcelExampleNPOI
Friend Class Program
Shared Sub Main(ByVal args() As String)
' Open the Excel file as a file stream
Using file As New FileStream("example.xlsx", FileMode.Open, FileAccess.Read)
' Initialize a workbook instance
Dim workbook As IWorkbook = New XSSFWorkbook(file)
' Access the first sheet of the workbook
Dim sheet As ISheet = workbook.GetSheetAt(0)
' Iterate through each row and cell to read the data
For rowIndex As Integer = 0 To sheet.LastRowNum
Dim row As IRow = sheet.GetRow(rowIndex)
If row IsNot Nothing Then ' Ensure the row is not null
For cellIndex As Integer = 0 To row.LastCellNum - 1
Dim cell As ICell = row.GetCell(cellIndex)
If cell IsNot Nothing Then
' Output cell content based on its type
Select Case cell.CellType
Case CellType.String
Console.Write($"{cell.StringCellValue} ")
Case CellType.Numeric
Console.Write($"{cell.NumericCellValue} ")
Case CellType.Boolean
Console.Write($"{cell.BooleanCellValue} ")
Case Else
Console.Write($"{cell} ")
End Select
End If
Next cellIndex
Console.WriteLine()
End If
Next rowIndex
End Using
Console.WriteLine("Excel data reading completed with NPOI.")
End Sub
End Class
End Namespace