IronXL 操作指南 ASP.NET MVC 讀取 Excel 檔案 Read Excel Files in ASP.NET MVC Using IronXL Curtis Chau 更新日期:8月 17, 2025 Download IronXL NuGet 下載 DLL 下載 Start Free Trial Copy for LLMs Copy for LLMs Copy page as Markdown for LLMs Open in ChatGPT Ask ChatGPT about this page Open in Gemini Ask Gemini about this page Open in Grok Ask Grok about this page Open in Perplexity Ask Perplexity about this page Share Share on Facebook Share on X (Twitter) Share on LinkedIn Copy URL Email article This article was translated from English: Does it need improvement? Translated View the article in English 本教程指導開發人員如何在 ASP.NET MVC 應用程式中使用 IronXL 解析 Excel 文件。 快速入門:在 MVC 中加載並將 Excel 工作表轉換為 DataTable 本例展示了如何在數秒內啟動:加載 Excel 工作簿,選擇其第一個工作表,並使用 IronXL 將其轉換為 System.Data.DataTable——無需 Interop,無需麻煩。 Get started making PDFs with NuGet now: Install IronXL with NuGet Package Manager PM > Install-Package IronXL.Excel Copy and run this code snippet. var dataTable = IronXL.WorkBook.Load("CustomerData.xlsx").DefaultWorkSheet.ToDataTable(true); Deploy to test on your live environment Start using IronXL in your project today with a free trial Free 30 day Trial class="hsg-featured-snippet"> 最簡工作流程(5 步) 安裝 C# 庫以在 ASP.NET 中讀取 Excel 文件 加載並訪問 Excel 文件中的目標工作表 訪問 ToDataTable 方法並將其返回給 View 使用循環在網頁上顯示 Excel 數據 遍歷所有數據並從中構建 HTML 表格 創建 ASP.NET 專案 使用 Visual Studio 2022(或類似產品版本),創建一個新的 ASP.NET 專案。 根據項目的具體需求添加其他 NuGet 套件和源代碼。 安裝 IronXL 庫 立即開始在您的項目中使用 IronXL 並免費試用。 第一步: 免費啟動 創建新項目後,我們必須安裝 IronXL 庫。 按照以下步驟安裝 IronXL 庫。 打開 NuGet Package Manager Console 並運行以下命令: Install-Package IronXL.Excel 讀取 Excel 文件 打開 ASP.NET 專案中的預設控制器(例如,HomeController.cs),並將 Index 方法替換為以下代碼: public ActionResult Index() { // Load the Excel workbook from a specified path. WorkBook workBook = WorkBook.Load(@"C:\Files\Customer Data.xlsx"); // Access the first worksheet from the workbook. WorkSheet workSheet = workBook.WorkSheets.First(); // Convert the worksheet data to a DataTable object. var dataTable = workSheet.ToDataTable(); // Send the DataTable to the view for rendering. return View(dataTable); } public ActionResult Index() { // Load the Excel workbook from a specified path. WorkBook workBook = WorkBook.Load(@"C:\Files\Customer Data.xlsx"); // Access the first worksheet from the workbook. WorkSheet workSheet = workBook.WorkSheets.First(); // Convert the worksheet data to a DataTable object. var dataTable = workSheet.ToDataTable(); // Send the DataTable to the view for rendering. return View(dataTable); } Public Function Index() As ActionResult ' Load the Excel workbook from a specified path. Dim workBook As WorkBook = WorkBook.Load("C:\Files\Customer Data.xlsx") ' Access the first worksheet from the workbook. Dim workSheet As WorkSheet = workBook.WorkSheets.First() ' Convert the worksheet data to a DataTable object. Dim dataTable = workSheet.ToDataTable() ' Send the DataTable to the view for rendering. Return View(dataTable) End Function $vbLabelText $csharpLabel 在 Index 操作方法中,我們使用 IronXL 的 Load 方法加載 Excel 文件。 Excel 文件的路徑(包括文件名)作為該方法調用的參數提供。 接下來,我們選擇第一個 Excel 工作表作為工作表,並將其包含的數據加載到 DataTable 對象中。 最後,DataTable 被送到前端。 在網頁上顯示 Excel 數據 下一個範例顯示如何在網頁瀏覽器中顯示前例返回的 DataTable。 本範例中將使用的工作 Excel 文件如下面所示: class="content-img-align-center"> class="center-image-wrapper"> 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> $vbLabelText $csharpLabel 上述代碼使用從 Index 方法返回的 DataTable 作為模型。 表格中的每一行都使用 @for 循環打印在網頁上,並包括 Bootstrap 格式裝飾。 運行項目將產生如下所示的結果。 class="content-img-align-center"> class="center-image-wrapper"> Bootstrap 表格 常見問題解答 如何在 ASP.NET MVC 中不使用 Interop 讀取 Excel 檔案? 您可以通過使用 IronXL 程式庫在 ASP.NET MVC 中不使用 Interop 讀取 Excel 檔案。首先,通過 NuGet 安裝 IronXL,然後使用 WorkBook.Load 方法加載 Excel 文件。存取工作表並將其轉換為 DataTable 以便進一步處理。 使用 ASP.NET MVC 在網頁上顯示 Excel 數據需要哪些步驟? 要使用 ASP.NET MVC 在網頁上顯示 Excel 數據,使用 IronXL 加載 Excel 檔案,將其轉換為 DataTable,並傳遞給視圖。在視圖中使用 Razor 語法迭代 DataTable,並將數據渲染為 HTML 表格。 如何安裝處理 ASP.NET 應用程式中 Excel 文件所需的程式庫? 要處理 ASP.NET 應用中的 Excel 文件,請在 Visual Studio 中打開 NuGet 套件管理器控制台並執行命令 Install-Package IronXL.Excel 來安裝 IronXL 程式庫。 使用 IronXL 在 ASP.NET MVC 中執行 Excel 操作的優勢是什麼? IronXL 通過提供一個易於使用的 API,簡化了在 ASP.NET MVC 中執行 Excel 操作,允許讀取、寫入和操縱 Excel 文件而無需 Excel Interop,從而提高性能並降低複雜性。 如何在 ASP.NET MVC 中將 Excel 工作表轉換為 DataTable? 在 ASP.NET MVC 中,載入 Excel 檔案後,可以使用 IronXL 的 ToDataTable 方法將工作表轉換為 DataTable,這有助於輕鬆處理和操作數據。 IronXL 能否用於 ASP.NET Core MVC 應用程式的 Excel 處理? 是的,IronXL 與 ASP.NET Core MVC 應用兼容,可以用於高效讀取和操縱 Excel 文件而無需依賴 Excel Interop。 在控制器中需要哪些代碼修改來讀取 Excel 檔案? 修改默認控制器,如 HomeController.cs,使用 IronXL 的 WorkBook.Load 載入 Excel 工作簿,將工作表數據轉換為 DataTable,並傳遞給視圖。 IronXL 如何改進在 ASP.NET 中讀取 Excel 文件的過程? IronXL 通過提供簡潔的 API,消除了對 Excel Interop 的需求,從而提高了在 ASP.NET 中讀取、寫入和操作 Excel 數據的流程效率。 Curtis Chau 立即與工程團隊聊天 技術作家 Curtis Chau 擁有卡爾頓大學計算機科學學士學位,專注於前端開發,擅長於 Node.js、TypeScript、JavaScript 和 React。Curtis 熱衷於創建直觀且美觀的用戶界面,喜歡使用現代框架並打造結構良好、視覺吸引人的手冊。除了開發之外,Curtis 對物聯網 (IoT) 有著濃厚的興趣,探索將硬體和軟體結合的創新方式。在閒暇時間,他喜愛遊戲並構建 Discord 機器人,結合科技與創意的樂趣。 準備好開始了嗎? Nuget 下載 1,686,155 | 版本: 2025.11 剛剛發布 免費 NuGet 下載 總下載量:1,686,155 查看許可證