使用 IRONXL 如何在 Razor Pages 中生成 Excel 文件 Curtis Chau 已更新:七月 28, 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 Razor Pages offer a modern approach to building web pages. In Razor Pages, the source code that renders the web page is written in C# rather than being generated by a server-side page. To efficiently work with Excel files, the IronXL library is recommended over others like the POI Java project NPOI package (notably used with PowerPoint files) and Office Interop. IronXL is a feature-rich, easy-to-use library that doesn't require Microsoft Office and supports multiple .NET versions. This article demonstrates using the IronXL C# library to generate and export Excel spreadsheets with dummy data and row headers in Razor Pages. How to Generate Excel Files in Razor Pages Install C# library to generate Excel files in Razor Access or create a new Excel file in a method Add data and style to the WorkBook object in Razor Use File method to automatically download Excel file when the button is pressed Inspect the generated Excel file IronXL: C# Excel Library IronXL is a C# Excel library that offers methods and functions for operating on large data sets using parallel processing to increase computing power. It's user-friendly as it doesn't require understanding the inner workings and supports both XLS and XLSX files. IronXL is ideal for importing, exporting, creating Excel formulas, and office documents without needing Microsoft Office to be installed. Learn how to import and export Excel files in Razor Pages using IronXL. Generate Excel File in Razor Pages To create Excel sheets in Razor Pages, start by opening Visual Studio, creating an ASP.NET Core Web Application, and installing IronXL. Prerequisites Setting up to create Excel files in Razor Pages requires: Visual Studio (Latest Version) .NET Framework 6 or 7 .NET Core Web Application in Visual Studio Install IronXL Library Install IronXL using the NuGet Package Manager Console. Enter this command to install the library in a .NET Core Web Application: Install-Package IronXL.Excel Code for Generating Excel file Here's how to write the code. Open "Index.cs" in the Pages folder and add the following method: public FileResult OnPostExport() { // Create a new WorkBook with XLSX format WorkBook workBook = new WorkBook(IronXL.ExcelFileFormat.XLSX); // Create a new WorkSheet named "data" WorkSheet workSheet = workBook.CreateWorkSheet("data"); // Add headers to the new worksheet workSheet["A1"].Value = "Product EN"; workSheet["B1"].Value = "SKU"; workSheet["C1"].Value = "Customer"; // Make header text bold workSheet["A1:C1"].Style.Font.Bold = true; // Add dummy data to the worksheet workSheet["A2"].Value = "Iron Rods"; workSheet["A3"].Value = "Mobile Phones"; workSheet["A4"].Value = "Chargers"; workSheet["B2"].Value = "105"; workSheet["B3"].Value = "285"; workSheet["B4"].Value = "301"; workSheet["C2"].Value = "Adam"; workSheet["C3"].Value = "Ellen"; workSheet["C4"].Value = "Tom"; // Convert the WorkBook to a byte array Stream for download return File(workBook.ToStream().ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Grid.xlsx"); } public FileResult OnPostExport() { // Create a new WorkBook with XLSX format WorkBook workBook = new WorkBook(IronXL.ExcelFileFormat.XLSX); // Create a new WorkSheet named "data" WorkSheet workSheet = workBook.CreateWorkSheet("data"); // Add headers to the new worksheet workSheet["A1"].Value = "Product EN"; workSheet["B1"].Value = "SKU"; workSheet["C1"].Value = "Customer"; // Make header text bold workSheet["A1:C1"].Style.Font.Bold = true; // Add dummy data to the worksheet workSheet["A2"].Value = "Iron Rods"; workSheet["A3"].Value = "Mobile Phones"; workSheet["A4"].Value = "Chargers"; workSheet["B2"].Value = "105"; workSheet["B3"].Value = "285"; workSheet["B4"].Value = "301"; workSheet["C2"].Value = "Adam"; workSheet["C3"].Value = "Ellen"; workSheet["C4"].Value = "Tom"; // Convert the WorkBook to a byte array Stream for download return File(workBook.ToStream().ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Grid.xlsx"); } Public Function OnPostExport() As FileResult ' Create a new WorkBook with XLSX format Dim workBook As New WorkBook(IronXL.ExcelFileFormat.XLSX) ' Create a new WorkSheet named "data" Dim workSheet As WorkSheet = workBook.CreateWorkSheet("data") ' Add headers to the new worksheet workSheet("A1").Value = "Product EN" workSheet("B1").Value = "SKU" workSheet("C1").Value = "Customer" ' Make header text bold workSheet("A1:C1").Style.Font.Bold = True ' Add dummy data to the worksheet workSheet("A2").Value = "Iron Rods" workSheet("A3").Value = "Mobile Phones" workSheet("A4").Value = "Chargers" workSheet("B2").Value = "105" workSheet("B3").Value = "285" workSheet("B4").Value = "301" workSheet("C2").Value = "Adam" workSheet("C3").Value = "Ellen" workSheet("C4").Value = "Tom" ' Convert the WorkBook to a byte array Stream for download Return File(workBook.ToStream().ToArray(), "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet", "Grid.xlsx") End Function $vbLabelText $csharpLabel In this code, a new Excel WorkBook is created and contains one WorkSheet. The sheet is populated with dummy data, and the generated file is responded to the server with: The WorkBook is converted to a Stream to download as a file. The MIME type is set. A name for the file download is specified. Create a button to download the Excel file Replace the existing code in "Index.cshtml" with the following: @page @model IndexModel @{ ViewData["Title"] = "Home page"; } <div class="text-center"> <h1 class="display-4">IronXL Generate Excel File</h1> <p class="m-5">IronXL is an Excel Library for C# that allows developers to read and edit Excel data from XLS and XLSX documents without using Microsoft.Office.Interop.Excel</p> <form method="post" asp-page-handler="Export"> <button class="btn btn-success p-3">Generate Excel File!</button> </form> </div> @page @model IndexModel @{ ViewData["Title"] = "Home page"; } <div class="text-center"> <h1 class="display-4">IronXL Generate Excel File</h1> <p class="m-5">IronXL is an Excel Library for C# that allows developers to read and edit Excel data from XLS and XLSX documents without using Microsoft.Office.Interop.Excel</p> <form method="post" asp-page-handler="Export"> <button class="btn btn-success p-3">Generate Excel File!</button> </form> </div> HTML This HTML creates a form with asp-page-handler set to "Export", enabling the application to export the Excel file to the local machine when the user clicks the "Generate Excel File!" button, triggering a browser download. Run the Project Run the project, and you'll see a screen in the browser. Click the green button to download the Excel file. IronXL Generate Excel Sheets After clicking "Generate Excel File!", the file will download. IronXL Downloaded File Open the Generated Excel File The generated Excel file's content matches the code's output with perfect formatting. The snapshot of the generated Excel file is displayed below. IronXL Generated File IronXL better than Competitors IronXL is a superior tool for working with both XLS and XLSX formats without limitations like NPOI, which is slower due to being Java-based, whereas IronXL is C#. In ASP.NET Core applications generating large/multiple spreadsheets, opt for IronXL over POI. IronXL allows for creating and editing Excel files without Microsoft Office. Unlike Microsoft Interop, IronXL does not need to call Microsoft Excel's ActiveX for Excel operations or exports. It also allows exporting CSV files to Excel format. IronXL also provides extensive features for interacting with Excel files, including converting between popular formats, inserting math functions, adding charts, and inserting images. Conclusion This article showcases using IronXL for generating an Excel file in Razor Pages without needing Microsoft Excel ActiveX and facilitating file downloads. Explore more tutorials on creating Excel files. IronXL is free for non-commercial development. A free trial is available for production testing. View pricing plans for more pricing and licensing details. 常见问题解答 如何在Razor Pages中生成不使用Interop的Excel文件? 您可以通过利用IronXL库在Razor Pages中生成不使用Interop的Excel文件。此库允许您在C#中直接创建和操作Excel文件,无需Microsoft Office,通过创建一个WorkBook并向WorkSheet添加数据即可。 在Razor Pages项目中安装IronXL涉及哪些步骤? 要在Razor Pages项目中安装IronXL,您可以使用Visual Studio中的NuGet包管理器控制台。执行命令Install-Package IronXL.Excel以将该库添加到您的项目中。 我可以使用IronXL从Razor Pages应用程序中导出数据吗? 是的,可以使用IronXL从Razor Pages应用程序中导出数据。您可以将数据转换为Excel格式,然后使用Razor Pages将该数据流回客户端作为可下载文件。 使用IronXL而非Microsoft Interop生成Excel文件的好处是什么? 与Microsoft Interop相比,IronXL具有几个优势,包括更快的性能,无需安装Microsoft Office,并且能够通过并行处理高效处理大型数据集。 在Razor Pages中使用IronXL可以处理大型Excel数据集吗? 是的,IronXL能够高效处理大型Excel数据集。它支持并行处理,能够在处理大量数据时提升性能和速度。 IronXL支持不同的Excel文件格式吗? IronXL支持XLS和XLSX两种文件格式,允许进行全面的Excel文件操作,包括导入、导出和格式化数据。 在开发中是否有使用IronXL的免费选项? IronXL可以免费用于非商业开发。提供免费试用用于生产测试,并有多种定价方案可用于商业用途。 如何在Razor Pages中使用IronXL触发Excel文件下载? 要在Razor Pages中使用IronXL触发Excel文件下载,创建一个WorkBook,用数据填充,然后将其转换为字节数组。使用Razor Pages的File方法将字节数组流化为可下载文件发送给用户。 是什么让IronXL比其他库如NPOI更具优势? IronXL通常比类似NPOI的库更受推荐,因为它是本地C#实现,提供更快的性能,易于使用,并且没有对Java组件的依赖,这使其非常适合ASP.NET Core应用程序。 Curtis Chau 立即与工程团队聊天 技术作家 Curtis Chau 拥有卡尔顿大学的计算机科学学士学位,专注于前端开发,精通 Node.js、TypeScript、JavaScript 和 React。他热衷于打造直观且美观的用户界面,喜欢使用现代框架并创建结构良好、视觉吸引力强的手册。除了开发之外,Curtis 对物联网 (IoT) 有浓厚的兴趣,探索将硬件和软件集成的新方法。在空闲时间,他喜欢玩游戏和构建 Discord 机器人,将他对技术的热爱与创造力相结合。 相关文章 已发布十月 27, 2025 如何在 C# 中创建 Excel 数据透视表 学习通过这个清晰的分步指南使用C# Interop和IronXL在Excel中创建数据透视表。 阅读更多 已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多 已发布十月 27, 2025 如何在.NET Core中使用CSV Reader与IronXL 学习通过实际示例有效地使用IronXL作为.NET Core的CSV读取器。 阅读更多 如何在 Blazor 中使用 IronXL 将数据导出到 Excel如何在 C# 中使用 CSV 解析器
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多