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 tutorial, you will learn how to view Excel files within a Blazor Server application by setting up an Excel file viewer using IronXL. The process involves integrating IronXL into a Visual Studio project and working with the necessary code.
First, import the required using
statements for IronXL and System.Data. Define a table structure in HTML with dynamic headers based on the Excel file's columns, while the rows are filled with data from the file.
In the @code
section, a data table named displayDataTable
is declared to store Excel data. The OpenExcelFromDisk
method manages file uploads and data extraction. When an Excel file is uploaded, it is read into a memory stream, which IronXL loads into a workbook object. The first row's headers become the data table's columns, and subsequent rows are added as data rows.
Running the code opens a Blazor web application page where you can navigate to the 'fetch data' menu, open an Excel file, and view its contents. This tutorial demonstrates the simplicity of using IronXL to create an Excel file viewer in a Blazor server app, enhancing web application capabilities.
@page "/fetchdata"
@using System.Data
@using IronXL
@* HTML Table to display Excel data *@
<table>
<thead>
<tr>
@* Table headers are generated dynamically *@
@foreach (DataColumn column in displayDataTable.Columns)
{
<th>@column.ColumnName</th>
}
</tr>
</thead>
<tbody>
@* Table rows are generated dynamically *@
@foreach (DataRow row in displayDataTable.Rows)
{
<tr>
@foreach (var item in row.ItemArray)
{
<td>@item</td>
}
</tr>
}
</tbody>
</table>
@code {
// DataTable to hold the data extracted from Excel
private DataTable displayDataTable = new DataTable();
/// <summary>
/// Method to open Excel file from disk and extract data
/// </summary>
/// <param name="filePath">Path to the Excel file</param>
private void OpenExcelFromDisk(string filePath)
{
// Load the Excel file into a WorkBook
var workbook = WorkBook.Load(filePath);
// Get the first worksheet
var sheet = workbook.WorkSheets[0];
// Initialize DataTable columns from the first row
foreach (var column in sheet[0])
{
displayDataTable.Columns.Add(column.ToString());
}
// Append DataTable rows from subsequent rows in the Excel sheet
for (int i = 1; i < sheet.RowCount; i++)
{
DataRow newRow = displayDataTable.NewRow();
for (int j = 0; j < sheet.ColumnCount; j++)
{
newRow[j] = sheet[i][j].ToString();
}
displayDataTable.Rows.Add(newRow);
}
}
}
@page "/fetchdata"
@using System.Data
@using IronXL
@* HTML Table to display Excel data *@
<table>
<thead>
<tr>
@* Table headers are generated dynamically *@
@foreach (DataColumn column in displayDataTable.Columns)
{
<th>@column.ColumnName</th>
}
</tr>
</thead>
<tbody>
@* Table rows are generated dynamically *@
@foreach (DataRow row in displayDataTable.Rows)
{
<tr>
@foreach (var item in row.ItemArray)
{
<td>@item</td>
}
</tr>
}
</tbody>
</table>
@code {
// DataTable to hold the data extracted from Excel
private DataTable displayDataTable = new DataTable();
/// <summary>
/// Method to open Excel file from disk and extract data
/// </summary>
/// <param name="filePath">Path to the Excel file</param>
private void OpenExcelFromDisk(string filePath)
{
// Load the Excel file into a WorkBook
var workbook = WorkBook.Load(filePath);
// Get the first worksheet
var sheet = workbook.WorkSheets[0];
// Initialize DataTable columns from the first row
foreach (var column in sheet[0])
{
displayDataTable.Columns.Add(column.ToString());
}
// Append DataTable rows from subsequent rows in the Excel sheet
for (int i = 1; i < sheet.RowCount; i++)
{
DataRow newRow = displayDataTable.NewRow();
for (int j = 0; j < sheet.ColumnCount; j++)
{
newRow[j] = sheet[i][j].ToString();
}
displayDataTable.Rows.Add(newRow);
}
}
}
page "/fetchdata" [using] System.Data [using] IronXL * HTML Table [to] display Excel data * (Of thead) (Of tr) * Table headers are generated dynamically *[Function]n foreach(DataColumn column ByVal displayDataTable.Columns As in) As (Of table)
(Of th) column.ColumnName</th>
End Function
</tr> </thead> * Table rows are generated dynamically *[Function]n foreach(DataRow row ByVal displayDataTable.Rows As in) As (Of tbody)
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
' (Of tr) @foreach(var item in row.ItemArray)
' {
' <td> @item</td>
' }
</tr>
End Function
'INSTANT VB TODO TASK: The following line could not be converted:
</tbody> </table> code
If True Then
' DataTable to hold the data extracted from Excel
private DataTable displayDataTable = New DataTable()
''' <summary>
''' Method to open Excel file from disk and extract data
''' </summary>
''' <param name="filePath">Path to the Excel file</param>
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
' private void OpenExcelFromDisk(string filePath)
' {
' ' Load the Excel file into a WorkBook
' var workbook = WorkBook.Load(filePath);
'
' ' Get the first worksheet
' var sheet = workbook.WorkSheets[0];
'
' ' Initialize DataTable columns from the first row
' foreach (var column in sheet[0])
' {
' displayDataTable.Columns.Add(column.ToString());
' }
'
' ' Append DataTable rows from subsequent rows in the Excel sheet
' for (int i = 1; i < sheet.RowCount; i++)
' {
' DataRow newRow = displayDataTable.NewRow();
' for (int j = 0; j < sheet.ColumnCount; j++)
' {
' newRow[j] = sheet[i][j].ToString();
' }
' displayDataTable.Rows.Add(newRow);
' }
' }
End If
Further Reading: Blazor Read Excel File in C# Using IronXL (Example Tutorial)