使用 IRONXL 在 C# 中生成 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 The applications we develop are constantly communicating with Excel spreadsheets to obtain data for evaluation and results. It's really helpful to be able to generate Excel files in C# programmatically, saving us time and effort with our development. In this tutorial, we'll learn about generating Excel files in different formats, setting cell styles, and inserting data using efficient function C# programming. C# Excel File Generator Generate Excel Files with IronXL Generate C# Excel files in .XLSX & .XLS Generate CSV files Generate JSON, XML, TSV files and more in C# projects Step 1 1. Generate Excel Files with IronXL Generate Excel Files using the IronXL Excel for C# Library, providing a range of functions for generating and manipulating data in your project. The library is free for development, with licenses available when you're ready to push live. To follow this tutorial, you can download IronXL for generating or access it through Visual Studio and NuGet gallery. dotnet add package IronXL.Excel How to Tutorial 2. C# Excel File Generator Overview In business application development, we often need to generate different types of Excel files programmatically. For this purpose, we need the easiest and quickest way to generate different types of files and save them in the required location automatically. After installing IronXL, we can use the functions to generate different Excel file types: Excel file with .xlsx extension. Excel file with .xls extension. Comma Separated Value (.csv) files. Tab Separated Value (.tsv) files. JavaScript Object Notation (.json) files. Extensible Markup Language (.xml) files. To generate any type of file, firstly we need to create an Excel WorkBook. // Generate a new WorkBook WorkBook wb = WorkBook.Create(); // Generate a new WorkBook WorkBook wb = WorkBook.Create(); ' Generate a new WorkBook Dim wb As WorkBook = WorkBook.Create() $vbLabelText $csharpLabel The above line of code will create a new WorkBook named wb. Now we will create a WorkSheet object. // Generate a new WorkSheet WorkSheet ws = wb.CreateWorkSheet("SheetName"); // Generate a new WorkSheet WorkSheet ws = wb.CreateWorkSheet("SheetName"); ' Generate a new WorkSheet Dim ws As WorkSheet = wb.CreateWorkSheet("SheetName") $vbLabelText $csharpLabel This will create a WorkSheet named ws that we can use to insert data in Excel files. 3. Generate XLSX File C# First, we follow the steps above to generate the WorkBook and WorkSheet. Then, we insert data into it to create our .xlsx extension file. For this purpose, IronXL provides a Cell Addressing System that allows us to insert data into a specific cell address programmatically. // Insert data by cell addressing ws["CellAddress"].Value = "MyValue"; // Insert data by cell addressing ws["CellAddress"].Value = "MyValue"; ' Insert data by cell addressing ws("CellAddress").Value = "MyValue" $vbLabelText $csharpLabel It will insert a new value "MyValue" in a specific cell address. In the same way, we can insert data into as many cells as we require. After this, we will save the Excel file in the specified path as follows: // Specify file path and name wb.SaveAs("Path + FileName.xlsx"); // Specify file path and name wb.SaveAs("Path + FileName.xlsx"); ' Specify file path and name wb.SaveAs("Path + FileName.xlsx") $vbLabelText $csharpLabel This will create a new Excel file with the extension .xlsx in the specified path. Don't forget to write the extension .xlsx with the file name while saving. To take a step further into how to create an Excel WorkBook in a C# project, check out the code examples here. /** * Generate XLSX File */ using System; using IronXL; class Program { static void Main(string[] args) { // Create new WorkBook of .xlsx Extension WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX); // Create workSheet WorkSheet ws = wb.CreateWorkSheet("Sheet1"); // Insert data in the cells of WorkSheet ws["A1"].Value = "Hello"; ws["A2"].Value = "World"; ws["C4"].Value = "IronXL"; // Save the file as .xlsx wb.SaveAs("sample.xlsx"); Console.WriteLine("Successfully created."); Console.ReadKey(); } } /** * Generate XLSX File */ using System; using IronXL; class Program { static void Main(string[] args) { // Create new WorkBook of .xlsx Extension WorkBook wb = WorkBook.Create(ExcelFileFormat.XLSX); // Create workSheet WorkSheet ws = wb.CreateWorkSheet("Sheet1"); // Insert data in the cells of WorkSheet ws["A1"].Value = "Hello"; ws["A2"].Value = "World"; ws["C4"].Value = "IronXL"; // Save the file as .xlsx wb.SaveAs("sample.xlsx"); Console.WriteLine("Successfully created."); Console.ReadKey(); } } ''' ''' * Generate XLSX File ''' Imports System Imports IronXL Friend Class Program Shared Sub Main(ByVal args() As String) ' Create new WorkBook of .xlsx Extension Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLSX) ' Create workSheet Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1") ' Insert data in the cells of WorkSheet ws("A1").Value = "Hello" ws("A2").Value = "World" ws("C4").Value = "IronXL" ' Save the file as .xlsx wb.SaveAs("sample.xlsx") Console.WriteLine("Successfully created.") Console.ReadKey() End Sub End Class $vbLabelText $csharpLabel You can see a screenshot of the newly created Excel file sample.xlsx here: The result of the modified value in cell C4 4. Generate XLS File C# It is also possible to generate .xls files using IronXL. For this purpose, we will use the WorkBook.Create() function as follows: // Create a workbook with XLS format WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS); // Create a workbook with XLS format WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS); ' Create a workbook with XLS format Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLS) $vbLabelText $csharpLabel This will create a new Excel file with a .xls extension. Keep in mind that while assigning a name to an Excel file, you must write the extension .xls with the file name, like this: // Save the file as .xls wb.SaveAs("Path + FileName.xls"); // Save the file as .xls wb.SaveAs("Path + FileName.xls"); ' Save the file as .xls wb.SaveAs("Path + FileName.xls") $vbLabelText $csharpLabel Now, let's see the example of how to generate an Excel file with a .xls extension: /** * Generate XLS File */ using System; using IronXL; class Program { static void Main(string[] args) { // Create new WorkBook of .xls Extension WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS); // Create WorkSheet WorkSheet ws = wb.CreateWorkSheet("Sheet1"); // Insert data in the cells of WorkSheet ws["A1"].Value = "Hello"; ws["A2"].Value = "World"; // Save the file as .xls wb.SaveAs("sample.xls"); Console.WriteLine("Successfully created."); Console.ReadKey(); } } /** * Generate XLS File */ using System; using IronXL; class Program { static void Main(string[] args) { // Create new WorkBook of .xls Extension WorkBook wb = WorkBook.Create(ExcelFileFormat.XLS); // Create WorkSheet WorkSheet ws = wb.CreateWorkSheet("Sheet1"); // Insert data in the cells of WorkSheet ws["A1"].Value = "Hello"; ws["A2"].Value = "World"; // Save the file as .xls wb.SaveAs("sample.xls"); Console.WriteLine("Successfully created."); Console.ReadKey(); } } ''' ''' * Generate XLS File ''' Imports System Imports IronXL Friend Class Program Shared Sub Main(ByVal args() As String) ' Create new WorkBook of .xls Extension Dim wb As WorkBook = WorkBook.Create(ExcelFileFormat.XLS) ' Create WorkSheet Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1") ' Insert data in the cells of WorkSheet ws("A1").Value = "Hello" ws("A2").Value = "World" ' Save the file as .xls wb.SaveAs("sample.xls") Console.WriteLine("Successfully created.") Console.ReadKey() End Sub End Class $vbLabelText $csharpLabel 5. Generate CSV File C# Comma Separated Value (.csv) files also play a very important role in keeping data in different types of organizations. So, we also need to learn how to generate .csv files and insert data into them programmatically. We can use the same process as above, but we need to specify the .csv extension with the file name while saving. Let's see an example of how to create .csv files in our C# project: /** * Generate CSV File */ using System; using IronXL; class Program { static void Main(string[] args) { // Create new WorkBook WorkBook wb = WorkBook.Create(); // Create WorkSheet WorkSheet ws = wb.CreateWorkSheet("Sheet1"); // Insert data in the cells of WorkSheet ws["A1"].Value = "Hello"; ws["A2"].Value = "World"; // Save the file as .csv wb.SaveAsCsv("sample.csv"); Console.WriteLine("Successfully created."); Console.ReadKey(); } } /** * Generate CSV File */ using System; using IronXL; class Program { static void Main(string[] args) { // Create new WorkBook WorkBook wb = WorkBook.Create(); // Create WorkSheet WorkSheet ws = wb.CreateWorkSheet("Sheet1"); // Insert data in the cells of WorkSheet ws["A1"].Value = "Hello"; ws["A2"].Value = "World"; // Save the file as .csv wb.SaveAsCsv("sample.csv"); Console.WriteLine("Successfully created."); Console.ReadKey(); } } ''' ''' * Generate CSV File ''' Imports System Imports IronXL Friend Class Program Shared Sub Main(ByVal args() As String) ' Create new WorkBook Dim wb As WorkBook = WorkBook.Create() ' Create WorkSheet Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1") ' Insert data in the cells of WorkSheet ws("A1").Value = "Hello" ws("A2").Value = "World" ' Save the file as .csv wb.SaveAsCsv("sample.csv") Console.WriteLine("Successfully created.") Console.ReadKey() End Sub End Class $vbLabelText $csharpLabel To be able to interact more with CSV files, you can follow this tutorial to read .csv file. 6. Generate TSV File C# Sometimes we need to generate Tab Separated Value (.tsv) files and insert data programmatically. Using IronXL we can also generate .tsv extension files, insert data into them, and then save it to the required location. Let's see the example of how to generate .tsv extension files: /** * Generate TSV File */ using System; using IronXL; class Program { static void Main(string[] args) { // Create new WorkBook WorkBook wb = WorkBook.Create(); // Create WorkSheet WorkSheet ws = wb.CreateWorkSheet("Sheet1"); // Insert data in the cells of WorkSheet ws["A1"].Value = "Hello"; ws["A2"].Value = "World"; // Save the file as .tsv wb.SaveAsTsv("sample.tsv"); Console.WriteLine("Successfully created."); Console.ReadKey(); } } /** * Generate TSV File */ using System; using IronXL; class Program { static void Main(string[] args) { // Create new WorkBook WorkBook wb = WorkBook.Create(); // Create WorkSheet WorkSheet ws = wb.CreateWorkSheet("Sheet1"); // Insert data in the cells of WorkSheet ws["A1"].Value = "Hello"; ws["A2"].Value = "World"; // Save the file as .tsv wb.SaveAsTsv("sample.tsv"); Console.WriteLine("Successfully created."); Console.ReadKey(); } } ''' ''' * Generate TSV File ''' Imports System Imports IronXL Friend Class Program Shared Sub Main(ByVal args() As String) ' Create new WorkBook Dim wb As WorkBook = WorkBook.Create() ' Create WorkSheet Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1") ' Insert data in the cells of WorkSheet ws("A1").Value = "Hello" ws("A2").Value = "World" ' Save the file as .tsv wb.SaveAsTsv("sample.tsv") Console.WriteLine("Successfully created.") Console.ReadKey() End Sub End Class $vbLabelText $csharpLabel 7. Generate JSON File C# We can comfortably say that JavaScript Object Notation (.json) files are the most common data files and are used in almost all software development companies. Therefore, we often need to save the data in JSON format. For this, we need the simplest method to generate JSON format files and insert the data into them. In such conditions, IronXL is the best option by which we can easily generate these files for C#. Let's see the example. /** * Generate JSON File */ using System; using IronXL; class Program { static void Main(string[] args) { // Create new WorkBook WorkBook wb = WorkBook.Create(); // Create WorkSheet WorkSheet ws = wb.CreateWorkSheet("Sheet1"); // Insert data in the cells of WorkSheet ws["A1"].Value = "1"; ws["A2"].Value = "john"; ws["B1"].Value = "2"; ws["B2"].Value = "alex"; ws["C1"].Value = "3"; ws["C2"].Value = "stokes"; // Save the file as .json wb.SaveAsJson("sample.json"); Console.WriteLine("Successfully created."); Console.ReadKey(); } } /** * Generate JSON File */ using System; using IronXL; class Program { static void Main(string[] args) { // Create new WorkBook WorkBook wb = WorkBook.Create(); // Create WorkSheet WorkSheet ws = wb.CreateWorkSheet("Sheet1"); // Insert data in the cells of WorkSheet ws["A1"].Value = "1"; ws["A2"].Value = "john"; ws["B1"].Value = "2"; ws["B2"].Value = "alex"; ws["C1"].Value = "3"; ws["C2"].Value = "stokes"; // Save the file as .json wb.SaveAsJson("sample.json"); Console.WriteLine("Successfully created."); Console.ReadKey(); } } ''' ''' * Generate JSON File ''' Imports System Imports IronXL Friend Class Program Shared Sub Main(ByVal args() As String) ' Create new WorkBook Dim wb As WorkBook = WorkBook.Create() ' Create WorkSheet Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1") ' Insert data in the cells of WorkSheet ws("A1").Value = "1" ws("A2").Value = "john" ws("B1").Value = "2" ws("B2").Value = "alex" ws("C1").Value = "3" ws("C2").Value = "stokes" ' Save the file as .json wb.SaveAsJson("sample.json") Console.WriteLine("Successfully created.") Console.ReadKey() End Sub End Class $vbLabelText $csharpLabel And review the screenshot of the newly created JSON file sample.json : Navigating to NuGet Package Manager in Visual Studio 8. Generate XML File C# In business application development, we often need to save the data in the Extensible Markup Language (.xml) file format. This is important because .xml file data is readable by both humans and machines. Through the following examples, we will learn how to generate .xml files for C# and insert data programmatically. /** * Generate XML File */ using System; using IronXL; class Program { static void Main(string[] args) { // Create new WorkBook WorkBook wb = WorkBook.Create(); // Create WorkSheet WorkSheet ws = wb.CreateWorkSheet("Sheet1"); // Insert data in the cells of WorkSheet ws["A1"].Value = "Hello"; ws["A2"].Value = "World"; // Save the file as .xml wb.SaveAsXml("sample.xml"); Console.WriteLine("Successfully created."); Console.ReadKey(); } } /** * Generate XML File */ using System; using IronXL; class Program { static void Main(string[] args) { // Create new WorkBook WorkBook wb = WorkBook.Create(); // Create WorkSheet WorkSheet ws = wb.CreateWorkSheet("Sheet1"); // Insert data in the cells of WorkSheet ws["A1"].Value = "Hello"; ws["A2"].Value = "World"; // Save the file as .xml wb.SaveAsXml("sample.xml"); Console.WriteLine("Successfully created."); Console.ReadKey(); } } ''' ''' * Generate XML File ''' Imports System Imports IronXL Friend Class Program Shared Sub Main(ByVal args() As String) ' Create new WorkBook Dim wb As WorkBook = WorkBook.Create() ' Create WorkSheet Dim ws As WorkSheet = wb.CreateWorkSheet("Sheet1") ' Insert data in the cells of WorkSheet ws("A1").Value = "Hello" ws("A2").Value = "World" ' Save the file as .xml wb.SaveAsXml("sample.xml") Console.WriteLine("Successfully created.") Console.ReadKey() End Sub End Class $vbLabelText $csharpLabel You can read more about converting Excel spreadsheets and files programmatically for use in C# projects. IronXL library also offers a wide range of features to interact with Excel files such as cell data formatting, merging cells, inserting math functions, and even managing charts. Tutorial Quick Access IronXL Generator Documentation Read the full documentation on how IronXL generates files in every Excel format necessary for your C# project. IronXL Generator Documentation 常见问题解答 如何在 C# 中生成 Excel 文件? 您可以使用 IronXL 在 C# 中生成 Excel 文件。首先创建一个工作簿和工作表,使用单元格地址系统插入数据,并使用 IronXL 的方法以所需格式保存文件。 以编程方式在 C# 中操作 Excel 文件的步骤是什么? 要在 C# 中以编程方式操作 Excel 文件,请通过 NuGet 包管理器安装 IronXL,创建工作簿和工作表,使用 C# 代码插入和操作数据,并以您喜欢的文件格式保存更改。 如何使用 C# 将 Excel 文件保存为 JSON? 使用 IronXL,您可以通过创建工作簿和工作表,添加所需数据,并使用 SaveAsJson 方法以 JSON 格式导出文件,将 Excel 文件保存为 JSON。 我可以使用 C# 将 Excel 文件转换为 CSV 吗? 是的,IronXL 允许您在 C# 中将 Excel 文件转换为 CSV。您需要将 Excel 文件加载到工作簿中,根据需要进行处理,并使用 SaveAsCsv 方法导出。 我可以使用 C# 将 Excel 数据导出为哪些格式? 使用 IronXL,您可以将 Excel 数据导出为多种格式,如 XLSX、CSV、TSV、JSON 和 XML。此多功能性对 C# 项目中的不同数据处理需求非常有利。 如何在 C# 中安装 IronXL 以进行 Excel 操作? 要在 C# 中安装 IronXL 以进行 Excel 操作,请在 Visual Studio 中使用 NuGet 包管理器,运行命令 dotnet add package IronXL.Excel。 IronXL 适合涉及 Excel 文件的商业应用吗? IronXL 非常适合商业应用,因为它简化了以编程方式生成和操作 Excel 文件的过程,提高了自动化数据处理任务的效率。 我可以在开发过程中免费使用 IronXL 吗? IronXL 在开发过程中是免费使用的。但是,当您准备部署或在生产环境中使用该库时,需要获得许可证。 如何在 C# 生成 Excel 文件时设置单元格样式? 使用 IronXL 时,您可以通过库中提供的方法设置属性如字体大小、颜色和样式,来对生成的 Excel 文件中的单元格进行样式设置。 我可以在 C# 中从 Excel 数据生成 XML 文件吗? 是的,您可以使用 IronXL 从 Excel 数据生成 XML 文件。在工作簿和工作表中准备好数据后,使用 SaveAsXml 方法以 XML 格式导出数据。 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 文件在 .NET Core 中处理 Excel
已发布十月 27, 2025 如何在C#中将DataGridView导出到包含列头的Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多