使用 IRONXL 如何在 C# 中带格式导出到 Excel Curtis Chau 已更新:六月 22, 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 will explore various methods and techniques for exporting data to Excel while incorporating formatting elements that can help you create professional, visually engaging spreadsheets using IronXL. How to Export to Excel with Formatting Install the C# library required for exporting to Excel with formatting. Utilize the WorkBook.Load method to load an already existing CSV file. Set the background color of the cell using SetBackgroundColor function. Add the border to the file using BottomBorder and BorderType methods. Save the XLSX file using the SaveAs method. IronXL IronXL is a powerful and versatile Excel library for working with Excel files in the .NET Framework. It provides developers with a comprehensive set of tools to create, manipulate, and manage Excel documents programmatically. Whether you're building a desktop application, a web-based system, or working on data-driven projects in C# or VB.NET, IronXL simplifies the process of interacting with Excel files. This library is designed to streamline tasks such as reading, writing, and formatting Excel spreadsheets, making it an indispensable resource for those seeking efficient and reliable solutions for Excel integration in their .NET applications. This introduction will explore the key features and capabilities of IronXL, demonstrating how it can empower developers to work seamlessly with Excel data, unlocking new possibilities for data processing and reporting within the .NET ecosystem. Creating a New Project in C# To leverage the capabilities of the IronXL library for Excel-related tasks, the first step involves creating a .NET project in Visual Studio. Although any version of Visual Studio is compatible, it is advisable to use the most recent one. In this tutorial, the Console Application project is recommended to illustrate how to work with IronXL. Create a new project in Visual Studio After selecting the project type, proceed to specify a name and location for the project. Configure new project You also have the flexibility to choose the preferred framework for the project, such as .NET Core 6. Target framework selection Once the solution is generated, access the Program.cs file. In this file, you can input your code and execute the application. Installing IronXL The IronXL library offers various methods for downloading and installation, and this article will discuss two of them. Using Visual Studio NuGet Packages To install the IronXL library using NuGet Package Manager in Visual Studio, open the NuGet Package Manager and search for "IronXL" in the Browse tab. Once you've located IronXL in the search results, select it and proceed with the installation. After the installation is completed, you can utilize the IronXL library in your project. Search and install the IronXL package in NuGet Package Manager UI Using the Visual Studio Command Line Many developers prefer to install packages using the command line interface. To install IronXL via the command line interface, follow these steps: Go to Tools > NuGet Package Manager > Package Manager Console in Visual Studio. Navigate to Package Manager Console In the Package Manager Console tab, enter the following command: Install-Package IronXL.Excel Now the package will download and install into the current project, making it ready for use. Installation of IronXL in Package Manager Console Export DataTable from CSV File to Excel File With Formatting This section will explain how to convert the CSV file data table to a worksheet and then add formatting to it before saving it as an Excel file using IronXL. Here is the complete source code of the conversion method or export function that converts CSV files and DataTable to Excel files. In the below code example, the CSV file is converted to an XLSX file and formatted. using IronXL; using IronXL.Styles; using IronSoftware.Drawing; using System.Linq; // Load an existing CSV file as a workbook WorkBook workBook = WorkBook.Load("table.csv"); // Access the default worksheet within the workbook WorkSheet ws = workBook.DefaultWorkSheet; // Select a specific cell range and apply a background color var cell = ws["B4:B4"]; cell.Style.SetBackgroundColor("#428D65"); // Set background color to green // Apply an underline style to a range of cells var range1 = ws["A2:E6"]; range1.Style.Font.Underline = FontUnderlineType.SingleAccounting; // Single accounting underline // Apply bold and italic font styles to another range of cells var range2 = ws["A7:E11"]; range2.Style.Font.Bold = true; // Set font to bold range2.Style.Font.Italic = true; // Set font to italic // Add a medium-thickness bottom border to a range of cells var range = ws["A1:E11"]; range.Style.BottomBorder.Type = BorderType.Medium; // Medium border // Save the modified workbook as an XLSX file workBook.SaveAs("sample.xlsx"); using IronXL; using IronXL.Styles; using IronSoftware.Drawing; using System.Linq; // Load an existing CSV file as a workbook WorkBook workBook = WorkBook.Load("table.csv"); // Access the default worksheet within the workbook WorkSheet ws = workBook.DefaultWorkSheet; // Select a specific cell range and apply a background color var cell = ws["B4:B4"]; cell.Style.SetBackgroundColor("#428D65"); // Set background color to green // Apply an underline style to a range of cells var range1 = ws["A2:E6"]; range1.Style.Font.Underline = FontUnderlineType.SingleAccounting; // Single accounting underline // Apply bold and italic font styles to another range of cells var range2 = ws["A7:E11"]; range2.Style.Font.Bold = true; // Set font to bold range2.Style.Font.Italic = true; // Set font to italic // Add a medium-thickness bottom border to a range of cells var range = ws["A1:E11"]; range.Style.BottomBorder.Type = BorderType.Medium; // Medium border // Save the modified workbook as an XLSX file workBook.SaveAs("sample.xlsx"); Imports IronXL Imports IronXL.Styles Imports IronSoftware.Drawing Imports System.Linq ' Load an existing CSV file as a workbook Private workBook As WorkBook = WorkBook.Load("table.csv") ' Access the default worksheet within the workbook Private ws As WorkSheet = workBook.DefaultWorkSheet ' Select a specific cell range and apply a background color Private cell = ws("B4:B4") cell.Style.SetBackgroundColor("#428D65") ' Set background color to green ' Apply an underline style to a range of cells Dim range1 = ws("A2:E6") range1.Style.Font.Underline = FontUnderlineType.SingleAccounting ' Single accounting underline ' Apply bold and italic font styles to another range of cells Dim range2 = ws("A7:E11") range2.Style.Font.Bold = True ' Set font to bold range2.Style.Font.Italic = True ' Set font to italic ' Add a medium-thickness bottom border to a range of cells Dim range = ws("A1:E11") range.Style.BottomBorder.Type = BorderType.Medium ' Medium border ' Save the modified workbook as an XLSX file workBook.SaveAs("sample.xlsx") $vbLabelText $csharpLabel The provided C# code uses the IronXL library for working with Excel files in the .NET Framework. Here's how it functions: Namespace Importing: The code imports necessary IronXL libraries for Excel functionalities, styling, and coloring. Workbook Loading: It loads an Excel workbook from "table.csv" using WorkBook.Load, preparing for conversion from CSV to Excel format. Accessing Worksheet: The default worksheet in the workbook is accessed and assigned to the variable ws. Applying Styles: Cell B4 is set to have a green background. The range A2 to E6 is underlined using a single accounting style. The range A7 to E11 is formatted to have bold and italic fonts. A medium-thickness bottom border is added to the range A1 to E11. Saving the Workbook: The workbook, with all applied styles, is saved as "sample.xlsx", ensuring that the formatting is preserved. The result Excel file Conclusion Exporting data to Excel with formatting is a crucial aspect of data management and reporting, enabling professionals from various fields to present information in a visually appealing and organized manner. The provided C# code leverages the IronXL library to streamline this process, allowing users to create a new project, install IronXL, and transform data from a CSV file into an XLSX file with applied formatting. You can also convert it into an XLS file. IronXL simplifies the interaction between C# applications and Excel, making it easier to apply various formatting styles, such as background colors, font underlining, bold and italic text, and border additions. This capability enhances the visual appeal and clarity of the resulting Excel documents, making it a valuable resource for data analysts, business professionals, and developers seeking to create polished and professional reports. The complete source code example of export to Excel can be found in the following how-to page. For more detailed instructions, please visit examples of formatting Excel files and customizing background cells. If you want to try out IronXL today, be sure to check out the free trial, a risk-free opportunity to explore its capabilities. You can purchase the license after exploring all the functionalities IronXL has to offer. 常见问题解答 如何安装 IronXL 以导出数据到 Excel? 要安装 IronXL,您可以在 Visual Studio 中使用 NuGet 包管理器。在浏览选项卡中搜索 'IronXL',选择它,然后继续安装。或者,通过导航到工具 > NuGet 包管理器 > 包管理控制台并执行 Install-Package IronXL.Excel 使用命令行进行安装。 如何使用 C# 将 CSV 文件转换为 Excel 电子表格? 使用 IronXL,您可以通过加载 CSV 文件 WorkBook.Load,应用所需样式,并使用 workBook.SaveAs 将工作簿保存为 XLSX 文件来将 CSV 文件转换为 Excel 电子表格。 使用 IronXL 进行 Excel 格式化的好处是什么? IronXL 提供了一套强大的功能,可用于以编程方式创建和管理 Excel 文档。它支持多种格式选项,如设置背景颜色、应用字体样式和添加边框,这对于制作专业且视觉吸引人的电子表格至关重要。 如何使用 C# 在 Excel 中设置单元格的背景颜色? 要使用 IronXL 设置单元格的背景颜色,利用 SetBackgroundColor 方法。例如,要设置绿色背景色,应用 cell.Style.SetBackgroundColor("#428D65") 到特定单元格。 我可以使用库将字体样式如粗体和斜体应用到 Excel 单元格吗? 是的,使用 IronXL,您可以将字体样式(如粗体和斜体)应用到 Excel 单元格。这是通过 IronXL 提供的样式功能实现的,让您可以在电子表格中增强文本展示效果。 IronXL 是否支持导出到 XLSX 和 XLS 格式? IronXL 支持将数据导出为多种 Excel 文件格式,包括 XLSX 和 XLS,提供了选择最适合您的要求的格式的灵活性。 是否有免费试用版可以探索 IronXL 的功能? 是的,IronXL 提供免费试用版,让您能够在购买许可证前探索其功能并了解其能力。 为什么我应该使用 IronXL 而不是 Interop 进行 Excel 操作? IronXL 与 Interop 相比,提供了一种更高效和简洁的方法进行 Excel 操作。它消除了需要在服务器上安装 Excel 的需求,支持多种格式选项,并简化了在 .NET 框架中创建和管理 Excel 文档的过程。 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读取器。 阅读更多 如何在 C# 中写入 CSV 文件如何在 C# 中导出文件到 CSV
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多