IronXL 开始 Blazor 读取 Excel 文件 Blazor Read Excel File in C# Using IronXL (Example Tutorial) Curtis Chau 已更新:六月 10, 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 Introduction Blazor is an open-source .NET Web framework that was created by Microsoft. A Blazor application works by compiling the C# code into browser-compliant JavaScript and HTML. In this tutorial, I'll share knowledge about the best and easy method for reading Excel documents/worksheets in a Blazor server-side application using the IronXL C# library. How to Read Excel File in Blazor Install C# library to read Excel file in Blazor Create a File Upload button on your Blazor Application Use C# library to read Excel file from disk Configure Blazor app to display the read data in a table on the window Get started with IronXL 今天在您的项目中使用 IronXL,免费试用。 第一步: 免费开始 Step 1 - Create a Blazor Project in Visual Studio I have an XLSX file containing the following data that I will read into and open in the Blazor Server App: Input XLSX Excel Sheet Result in Blazor Server Browser First name Last name ID John Applesmith 1 Richard Smith 2 Sherry Robins 3 Start off by creating a Blazor Project from the Visual Studio IDE: Choose the Blazor Server App Project type: Go ahead and run the Application without changing the solution with the F5 key. Navigate to the Fetch data tab of the Application like so: Our goal will be to load our Excel file into the Blazor app with an upload button and then display it on this page. Step 2 - Add IronXL to your Solution IronXL: .NET Excel Library (Installation Instructions): IronXL is a .NET library that allows you to treat the spreadsheet in Microsoft Excel like an object, enabling the developer to use the full power of C# and the .NET Framework to manipulate data streams. As a developer, we want a nice way through which we can get every row's cells and column information from Excel documents/worksheets into our applications or databases. With IronXL, it is possible to get all sorts of information from a worksheet such as cell values, content of cells, images, references, and formatting. IronXL is better than NPOI in many aspects. IronXL provides more functions and can make writing complex logic easier. It also has more preferable licenses and the support team is more competent. IronXL supports all the latest versions of .NET (8, 7, and 6) and .NET Core Framework 4.6.2+. Add IronXL to your solution using one of the methods below then build the solution. Option 2A - Use NuGet Package Manager Install-Package IronXL.Excel Option 2B - Add PackageReference in the csproj file You can add IronXL directly to your project by adding the following line to any <ItemGroup> in the .csproj file of your solution: <PackageReference Include="IronXL.Excel" Version="*" /> <PackageReference Include="IronXL.Excel" Version="*" /> XML As shown here in Visual Studio: Step 3 - Coding the File Upload and View In the Visual Studio Solution View, go to the Pages/ folder and find the FetchData.razor file. You may use any other razor file but we will use this one because it comes with the Blazor Server App Template. Replace the file contents with the following code: @using IronXL; @using System.Data; @page "/fetchdata" <PageTitle>Excel File Viewer</PageTitle> <h1>Open Excel File to View</h1> <InputFile OnChange="@OpenExcelFileFromDisk" /> <table> <thead> <tr> @foreach (DataColumn column in displayDataTable.Columns) { <th> @column.ColumnName </th> } </tr> </thead> <tbody> @foreach (DataRow row in displayDataTable.Rows) { <tr> @foreach (DataColumn column in displayDataTable.Columns) { <td> @row[column.ColumnName].ToString() </td> } </tr> } </tbody> </table> @code { // Create a DataTable instance private DataTable displayDataTable = new DataTable(); // This method is triggered when a file is uploaded async Task OpenExcelFileFromDisk(InputFileChangeEventArgs e) { IronXL.License.LicenseKey = "PASTE TRIAL OR LICENSE KEY"; // Load the uploaded file into a MemoryStream MemoryStream ms = new MemoryStream(); await e.File.OpenReadStream().CopyToAsync(ms); ms.Position = 0; // Create an IronXL workbook from the MemoryStream WorkBook loadedWorkBook = WorkBook.FromStream(ms); WorkSheet loadedWorkSheet = loadedWorkBook.DefaultWorkSheet; // Or use .GetWorkSheet() // Add header Columns to the DataTable RangeRow headerRow = loadedWorkSheet.GetRow(0); for (int col = 0; col < loadedWorkSheet.ColumnCount; col++) { displayDataTable.Columns.Add(headerRow.ElementAt(col).ToString()); } // Populate the DataTable with data from the Excel sheet for (int row = 1; row < loadedWorkSheet.RowCount; row++) { IEnumerable<string> excelRow = loadedWorkSheet.GetRow(row).ToArray().Select(c => c.ToString()); displayDataTable.Rows.Add(excelRow.ToArray()); } } } @using IronXL; @using System.Data; @page "/fetchdata" <PageTitle>Excel File Viewer</PageTitle> <h1>Open Excel File to View</h1> <InputFile OnChange="@OpenExcelFileFromDisk" /> <table> <thead> <tr> @foreach (DataColumn column in displayDataTable.Columns) { <th> @column.ColumnName </th> } </tr> </thead> <tbody> @foreach (DataRow row in displayDataTable.Rows) { <tr> @foreach (DataColumn column in displayDataTable.Columns) { <td> @row[column.ColumnName].ToString() </td> } </tr> } </tbody> </table> @code { // Create a DataTable instance private DataTable displayDataTable = new DataTable(); // This method is triggered when a file is uploaded async Task OpenExcelFileFromDisk(InputFileChangeEventArgs e) { IronXL.License.LicenseKey = "PASTE TRIAL OR LICENSE KEY"; // Load the uploaded file into a MemoryStream MemoryStream ms = new MemoryStream(); await e.File.OpenReadStream().CopyToAsync(ms); ms.Position = 0; // Create an IronXL workbook from the MemoryStream WorkBook loadedWorkBook = WorkBook.FromStream(ms); WorkSheet loadedWorkSheet = loadedWorkBook.DefaultWorkSheet; // Or use .GetWorkSheet() // Add header Columns to the DataTable RangeRow headerRow = loadedWorkSheet.GetRow(0); for (int col = 0; col < loadedWorkSheet.ColumnCount; col++) { displayDataTable.Columns.Add(headerRow.ElementAt(col).ToString()); } // Populate the DataTable with data from the Excel sheet for (int row = 1; row < loadedWorkSheet.RowCount; row++) { IEnumerable<string> excelRow = loadedWorkSheet.GetRow(row).ToArray().Select(c => c.ToString()); displayDataTable.Rows.Add(excelRow.ToArray()); } } } Private IronXL As [using] Private System As [using] 'INSTANT VB TODO TASK: Local functions are not converted by Instant VB: '@page "/fetchdata" (Of PageTitle) Excel File Viewer</PageTitle> (Of h1) Open Excel File @to View</h1> <InputFile OnChange="@OpenExcelFileFromDisk" /> (Of table) (Of thead) (Of tr) @foreach(DataColumn column in displayDataTable.Columns) ' { ' <th> @column.ColumnName </th> ' } 'INSTANT VB TODO TASK: Local functions are not converted by Instant VB: ' </tr> </thead> (Of tbody) @foreach(DataRow row in displayDataTable.Rows) ' { ' <tr> @foreach(DataColumn column in displayDataTable.Columns) ' { ' <td> @row[column.ColumnName].ToString() </td> ' } ' </tr> ' } 'INSTANT VB TODO TASK: Local functions are not converted by Instant VB: ' </tbody> </table> @code ' { ' ' Create a DataTable instance ' private DataTable displayDataTable = New DataTable(); ' ' ' This method is triggered when a file is uploaded ' async Task OpenExcelFileFromDisk(InputFileChangeEventArgs e) ' { ' IronXL.License.LicenseKey = "PASTE TRIAL OR LICENSE KEY"; ' ' ' Load the uploaded file into a MemoryStream ' MemoryStream ms = New MemoryStream(); ' ' await e.File.OpenReadStream().CopyToAsync(ms); ' ms.Position = 0; ' ' ' Create an IronXL workbook from the MemoryStream ' WorkBook loadedWorkBook = WorkBook.FromStream(ms); ' WorkSheet loadedWorkSheet = loadedWorkBook.DefaultWorkSheet; ' Or use .GetWorkSheet() ' ' ' Add header Columns to the DataTable ' RangeRow headerRow = loadedWorkSheet.GetRow(0); ' for (int col = 0; col < loadedWorkSheet.ColumnCount; col++) ' { ' displayDataTable.Columns.Add(headerRow.ElementAt(col).ToString()); ' } ' ' ' Populate the DataTable with data from the Excel sheet ' for (int row = 1; row < loadedWorkSheet.RowCount; row++) ' { ' IEnumerable<string> excelRow = loadedWorkSheet.GetRow(row).ToArray().@Select(c => c.ToString()); ' displayDataTable.Rows.Add(excelRow.ToArray()); ' } ' } '} $vbLabelText $csharpLabel Summary The <InputFile> component allows you to upload a file on this webpage. We have set the invoked event callback to call OpenExcelFileFromDisk, which is the async method in the @code block at the bottom. The HTML will render your Excel sheet as a table on the tab. IronXL.Excel is a standalone .NET software library for reading a wide variety of spreadsheet formats. It does not require Microsoft Excel to be installed, and is not dependent on Interop. Further Reading View the API Reference Explore the API Reference for IronXL, outlining the details of all of IronXL’s features, namespaces, classes, methods fields and enums. View the API Reference Download the software product. 常见问题解答 我如何在 Blazor 服务器端应用程序中读取 Excel 文件? 要在 Blazor 服务器端应用程序中读取 Excel 文件,您可以使用 IronXL C# 库。它允许您轻松集成到您的 Blazor 项目中,通过使用 NuGet 包管理器安装库,然后实现代码以读取和显示 Excel 数据。 设置 Blazor 项目以读取 Excel 文件的步骤是什么? 首先,通过 NuGet 包管理器安装 IronXL。接下来,在您的 Blazor 应用程序中创建一个文件上传按钮。使用 IronXL 读取上传的 Excel 文件,并配置应用程序使用 Razor 组件在表格中显示数据。 在没有安装 Excel 的情况下,可以在 Blazor 应用程序中读取 Excel 文件吗? 是的,使用 IronXL,您可以在没有系统中安装 Microsoft Excel 的情况下在 Blazor 应用程序中读取和操作 Excel 文件。 我如何在 Blazor 应用程序中显示 Excel 数据? 使用 IronXL 读取 Excel 文件后,您可以在 Blazor 应用程序中使用 Razor 组件以表格式显示数据,增强用户界面。 IronXL 相对其他 Excel 库有什么优势? IronXL 提供广泛的功能、处理复杂逻辑的简便性、优越的许可条款和专门的支持,使其成为 NPOI 等替代方案的首选。 IronXL 支持哪些 .NET 版本进行 Excel 操作? IronXL 支持包括 8、7 和 6 在内的所有最新 .NET 版本以及.NET Core Framework 4.6.2+,确保与现代应用程序的广泛兼容性。 我如何将 Excel 库集成到我的 Blazor 项目中? 您可以通过使用命令dotnet add package IronXL.Excel的 NuGet 包管理器或在.csproj文件中添加PackageReference将像 IronXL 这样的 Excel 库集成到您的 Blazor 项目中。 如果我的 Blazor 应用程序无法读取 Excel 文件,我该采取哪些故障排除步骤? 确保 IronXL 已通过 NuGet 正确安装,并且您的 Blazor 应用程序具备从磁盘读取文件的必要权限。仔细检查 Excel 文件路径是否正确,以及文件格式是否受 IronXL 支持。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 准备开始了吗? Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:1,686,155 查看许可证