使用 IronXL 在 ASP.NET MVC 中讀取 Excel 文件
本教程指導開發人員如何在ASP.NET MVC應用程式中使用IronXL實現Excel文件解析。
如何在 ASP.NET 中讀取 Excel 文件
建立一個ASP.NET專案
使用 Visual Studio 2022(或類似產品版本),創建一個新的 ASP.NET 專案。 根據特定項目的需求添加額外的NuGet包和源代碼。
安裝 IronXL 程式庫
立即在您的專案中使用IronXL,並享受免費試用。
在創建新項目後,我們需要安裝 IronXL 庫。 按照以下步驟安裝 IronXL 程式庫。 在 NuGet 套件管理器控制台中打開並寫下以下命令:
Install-Package IronXL.Excel
讀取 Excel 檔案
在您的 ASP.NET 專案中打開預設控制器(即 HomeController)檔案,並將 Index
方法替換為以下程式碼:
public ActionResult Index()
{
WorkBook workbook = WorkBook.Load(@"C:\Files\Customer Data.xlsx");
WorkSheet sheet = workbook.WorkSheets.First();
var dataTable = sheet.ToDataTable();
return View(dataTable);
}
public ActionResult Index()
{
WorkBook workbook = WorkBook.Load(@"C:\Files\Customer Data.xlsx");
WorkSheet sheet = workbook.WorkSheets.First();
var dataTable = sheet.ToDataTable();
return View(dataTable);
}
Public Function Index() As ActionResult
Dim workbook As WorkBook = WorkBook.Load("C:\Files\Customer Data.xlsx")
Dim sheet As WorkSheet = workbook.WorkSheets.First()
Dim dataTable = sheet.ToDataTable()
Return View(dataTable)
End Function
在Index
動作方法中,我們使用IronXL的Load
加載Excel文件。 Excel 檔案的路徑(包括檔名)作為參數提供給方法呼叫。 接下來,我們選擇第一個 Excel 工作表作為工作表,並將其中包含的數據加載到一個Datatable
對象中。 最後,Datatable
被發送到前端。
在網頁上顯示 Excel 數據
下一個示例展示了如何在網頁瀏覽器中顯示上一個示例中返回的Datatable。
以下是本節範例中將使用的工作 Excel 文件:

Excel 文件
打開 index.cshtml(索引視圖)並將代碼替換為以下 HTML 代碼。
@{
ViewData ["Title"] = "Home Page";
}
@using System.Data
@model DataTable
<div class="text-center">
<h1 class="display-4">Welcome to IronXL Read Excel MVC</h1>
</div>
<table class="table table-dark">
<tbody>
@foreach (DataRow row in Model.Rows)
{
<tr>
@for (int i = 0; i < Model.Columns.Count; i++)
{
<td>@row [i]</td>
}
</tr>
}
</tbody>
</table>
@{
ViewData ["Title"] = "Home Page";
}
@using System.Data
@model DataTable
<div class="text-center">
<h1 class="display-4">Welcome to IronXL Read Excel MVC</h1>
</div>
<table class="table table-dark">
<tbody>
@foreach (DataRow row in Model.Rows)
{
<tr>
@for (int i = 0; i < Model.Columns.Count; i++)
{
<td>@row [i]</td>
}
</tr>
}
</tbody>
</table>
@
If True Then
ViewData ("Title") = "Home Page"
End If
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: using System.Data model DataTable <div class="text-center"> <h1 class="display-4"> Welcome to IronXL Read Excel MVC</h1> </div> <table class="table table-dark"> <tbody> foreach(DataRow row in Model.Rows)
"display-4"> Welcome [to] IronXL Read Excel MVC</h1> </div> <table class="table table-dark"> (Of tbody) foreach(DataRow row in Model.Rows)
If True Then
'INSTANT VB WARNING: An assignment within expression was extracted from the following statement:
'ORIGINAL LINE: using System.Data model DataTable <div class="text-center"> <h1 class="display-4"> Welcome to IronXL Read Excel MVC</h1> </div> <table class
"text-center"> <h1 class="display-4"> Welcome [to] IronXL Read Excel MVC</h1> </div> <table class
[using] System.Data model DataTable <div class="text-center"> <h1 class
'INSTANT VB TODO TASK: Local functions are not converted by Instant VB:
' (Of tr) @for(int i = 0; i < Model.Columns.Count; i++)
' {
' <td> @row [i]</td>
' }
</tr>
End If
'INSTANT VB TODO TASK: The following line uses invalid syntax:
' </tbody> </table>
上述代碼使用從Index
方法返回的Datatable
作為模型。 每行從表格中提取,並使用@for
循環在網頁上列印,包括Bootstrap格式進行裝飾。
執行該項目將產生以下所示的結果。

Bootstrap Table