使用 IRONXL 如何在 VB.NET 中写入 Excel 文件(简易方式) Jacob Mellor 已更新:七月 28, 2025 下载 IronXL NuGet 下载 DLL 下载 免费试用 法学硕士副本 法学硕士副本 将页面复制为 Markdown 格式,用于 LLMs 在 ChatGPT 中打开 向 ChatGPT 咨询此页面 在双子座打开 向 Gemini 询问此页面 在双子座打开 向 Gemini 询问此页面 打开困惑 向 Perplexity 询问有关此页面的信息 分享 在 Facebook 上分享 分享到 X(Twitter) 在 LinkedIn 上分享 复制链接 电子邮件文章 使用 Office Interop 一直是自动化 Microsoft Excel 任务的事实标准选择,但它存在一些明显的缺点——性能问题、服务器端复杂性以及需要在计算机上安装 Microsoft Excel。 本指南演示了一种更现代、更高效的方法,教您如何在 VB.NET 中创建和写入 Excel 文件。 我们将使用IronXL for .NET ,这是一个专门为帮助开发人员创建、读取和编辑 Excel 电子表格而构建的库,无需依赖 Office Interop。您将看到如何仅用几行直观的代码来处理常见任务,例如创建新文件、从集合写入数据以及修改现有文档。 如何在VB.NET中开始使用Excel自动化? 要开始在 VB.NET 项目中写入 Excel 文件,首先需要添加 IronXL 库。 该过程很简单,可以直接在 Visual Studio 中完成。 首先,请确保您的开发环境已准备就绪: Visual Studio 2022或更高版本。 一个面向.NET Framework 4.6.2 或更高版本,或.NET Core/.NET 5+ 的活跃项目。 IronXL 与所有现代 .NET 版本兼容,包括 .NET 9 和 10。 接下来,安装 IronXL NuGet 包: 在 Visual Studio 中打开NuGet 包管理器。 搜索IronXL.Excel包。 点击"安装"。 或者,您也可以通过软件包管理器控制台使用以下命令进行安装: Install-Package IronXL.Excel 在 Visual Studio 的菜单中找到 NuGet 包管理器,找到用于 VB.NET Excel 集成的正确包。 安装完成后,就可以开始编写代码了。 只需在 VB.NET 文件的顶部添加Imports IronXL ,即可将库中的类引入作用域。 在 NuGet 包管理器 UI 中搜索 IronXL 包 正在查找 IronXL 软件包,以便安装使用 VB.NET 将数据写入 Excel 文件所需的库。 如何创建一个新的Excel文件并向其中写入数据? 从头开始创建一个新的Excel文件是一项基本任务。 使用 IronXL,您可以生成工作簿、添加工作表,并使用来自任何来源(例如对象集合)的数据填充单元格。 这非常适合导出应用程序数据或生成每日报告等任务。 我们来看一个实际例子。 假设我们有一份产品数据列表,想要将其导出到 Excel 表格中。 将集合写入新的 Excel 工作表 以下代码演示了如何获取List(Of Product)并将其内容写入新创建的 Excel 文件中。 Imports IronXL Imports System.Collections.Generic Imports System.Linq Public Class Product Public Property ID As Integer Public Property Name As String Public Property Price As Decimal End Class Module Program Sub Main(args As String()) ' 1. Prepare a list of data to write to Excel. Dim products As New List(Of Product) From { New Product With {.ID = 1, .Name = "Laptop", .Price = 1200.50D}, New Product With {.ID = 2, .Name = "Keyboard", .Price = 75.00D}, New Product With {.ID = 3, .Name = "Mouse", .Price = 25.99D} } ' 2. Create a new Excel workbook and select the default worksheet. Dim workbook As WorkBook = WorkBook.Create() Dim sheet As WorkSheet = workbook.DefaultWorkSheet ' 3. Write headers for our data. sheet("A1").Value = "Product ID" sheet("B1").Value = "Product Name" sheet("C1").Value = "Price" ' Style the header row. Dim headerRange = sheet.GetRange("A1:C1") headerRange.Style.Font.Bold = True headerRange.Style.SetBackgroundColor("#D3D3D3") ' Light Gray ' 4. Iterate through the product list and write data to subsequent rows. For i As Integer = 0 To products.Count - 1 Dim product = products(i) Dim row = i + 2 ' Start from the second row sheet("A" & row).Value = product.ID sheet("B" & row).Value = product.Name sheet("C" & row).Value = product.Price Next ' 5. Apply currency formatting to the price column. Dim priceColumn = sheet.GetRange("C2:C" & products.Count + 1) priceColumn.Style.Format = "$#,##0.00" ' 6. Auto-size columns for better readability. sheet.Columns.AutoFit() ' 7. Save the newly created and populated workbook to a file. workbook.SaveAs("ProductReport.xlsx") End Sub End Module Imports IronXL Imports System.Collections.Generic Imports System.Linq Public Class Product Public Property ID As Integer Public Property Name As String Public Property Price As Decimal End Class Module Program Sub Main(args As String()) ' 1. Prepare a list of data to write to Excel. Dim products As New List(Of Product) From { New Product With {.ID = 1, .Name = "Laptop", .Price = 1200.50D}, New Product With {.ID = 2, .Name = "Keyboard", .Price = 75.00D}, New Product With {.ID = 3, .Name = "Mouse", .Price = 25.99D} } ' 2. Create a new Excel workbook and select the default worksheet. Dim workbook As WorkBook = WorkBook.Create() Dim sheet As WorkSheet = workbook.DefaultWorkSheet ' 3. Write headers for our data. sheet("A1").Value = "Product ID" sheet("B1").Value = "Product Name" sheet("C1").Value = "Price" ' Style the header row. Dim headerRange = sheet.GetRange("A1:C1") headerRange.Style.Font.Bold = True headerRange.Style.SetBackgroundColor("#D3D3D3") ' Light Gray ' 4. Iterate through the product list and write data to subsequent rows. For i As Integer = 0 To products.Count - 1 Dim product = products(i) Dim row = i + 2 ' Start from the second row sheet("A" & row).Value = product.ID sheet("B" & row).Value = product.Name sheet("C" & row).Value = product.Price Next ' 5. Apply currency formatting to the price column. Dim priceColumn = sheet.GetRange("C2:C" & products.Count + 1) priceColumn.Style.Format = "$#,##0.00" ' 6. Auto-size columns for better readability. sheet.Columns.AutoFit() ' 7. Save the newly created and populated workbook to a file. workbook.SaveAs("ProductReport.xlsx") End Sub End Module VB .NET 运作机制 此代码示例展示了 IronXL API 在自动化 Excel 输入方面的简洁性和强大功能。 1.数据准备:我们从一个简单的Product类和一个List(Of Product)开始,它表示我们要导出的数据。 这模拟从数据库或其他业务逻辑层提取数据。 2.创建工作簿和工作表: WorkBook.Create()在内存中生成一个新的、空白的 Excel 工作簿。 每个新工作簿都会自动包含一个工作表,我们可以通过DefaultWorkSheet属性访问该工作表。 对于更复杂的文档,您可以使用CreateWorkSheet方法添加更多工作表。 3.写入数据:我们使用熟悉的 A1 样式表示法访问单元格(例如, sheet("A1") )。 .Value属性用于设置和获取单元格的内容。 IronXL 会自动处理数字、字符串和日期的数据类型转换。 4.样式和格式:IronXL 提供全面的样式 API。 在这个例子中,我们选择一系列单元Range ,并对标题应用粗体格式和背景颜色。 我们还使用Style.Format属性对价格列应用货币格式,这是创建专业报表必不可少的功能。 如需更多样式选项,请查阅Style 对象的 API 文档。 5.保存文件:最后, workbook.SaveAs("ProductReport.xlsx")将内存中的工作簿写入文件系统。 IronXL 支持多种格式,包括.xlsx 、 .xls 、 .csv和.tsv ,让您可以灵活地导出数据。 如何将数据写入现有的Excel文件? 通常情况下,你不需要创建新文件,而是将数据添加到现有文件中。 这通常用于记录日志、向数据集追加记录或更新报告。 IronXL 让这个过程就像创建新文件一样简单。 以下示例演示如何打开现有电子表格,找到最后使用的行,并追加新数据。 Imports IronXL Imports System.Collections.Generic Imports System.Linq ' (Assuming the Product class from the previous example is available) Module Program Sub Main(args As String()) ' Ensure the file from our previous example exists. If Not System.IO.File.Exists("ProductReport.xlsx") Then Console.WriteLine("Please run the first example to create ProductReport.xlsx") Return End If ' 1. Load the existing workbook from the file system. Dim workbook As WorkBook = WorkBook.Load("ProductReport.xlsx") Dim sheet As WorkSheet = workbook.DefaultWorkSheet ' 2. Find the first empty row to append new data. ' The LastRowUsed property gives us the last row with data. Dim lastRow = sheet.Info.LastRowUsed Dim newRowIndex = lastRow.RowNumber + 1 ' 3. Define the new data to be added. Dim newProducts As New List(Of Product) From { New Product With {.ID = 4, .Name = "Monitor", .Price = 350.00D}, New Product With {.ID = 5, .Name = "Webcam", .Price = 99.99D} } ' 4. Loop through the new data and write it to the worksheet. For i As Integer = 0 To newProducts.Count - 1 Dim product = newProducts(i) Dim currentRow = newRowIndex + i sheet("A" & currentRow).Value = product.ID sheet("B" & currentRow).Value = product.Name sheet("C" & currentRow).Value = product.Price Next ' 5. Re-apply formatting and auto-fit columns to include new data. Dim priceColumn = sheet.GetRange("C2:C" & sheet.Info.LastRowUsed.RowNumber) priceColumn.Style.Format = "$#,##0.00" sheet.Columns.AutoFit() ' 6. Save the changes back to the original file. workbook.Save() ' Or save as a new file to preserve the original. ' workbook.SaveAs("ProductReport_Updated.xlsx") End Sub End Module Imports IronXL Imports System.Collections.Generic Imports System.Linq ' (Assuming the Product class from the previous example is available) Module Program Sub Main(args As String()) ' Ensure the file from our previous example exists. If Not System.IO.File.Exists("ProductReport.xlsx") Then Console.WriteLine("Please run the first example to create ProductReport.xlsx") Return End If ' 1. Load the existing workbook from the file system. Dim workbook As WorkBook = WorkBook.Load("ProductReport.xlsx") Dim sheet As WorkSheet = workbook.DefaultWorkSheet ' 2. Find the first empty row to append new data. ' The LastRowUsed property gives us the last row with data. Dim lastRow = sheet.Info.LastRowUsed Dim newRowIndex = lastRow.RowNumber + 1 ' 3. Define the new data to be added. Dim newProducts As New List(Of Product) From { New Product With {.ID = 4, .Name = "Monitor", .Price = 350.00D}, New Product With {.ID = 5, .Name = "Webcam", .Price = 99.99D} } ' 4. Loop through the new data and write it to the worksheet. For i As Integer = 0 To newProducts.Count - 1 Dim product = newProducts(i) Dim currentRow = newRowIndex + i sheet("A" & currentRow).Value = product.ID sheet("B" & currentRow).Value = product.Name sheet("C" & currentRow).Value = product.Price Next ' 5. Re-apply formatting and auto-fit columns to include new data. Dim priceColumn = sheet.GetRange("C2:C" & sheet.Info.LastRowUsed.RowNumber) priceColumn.Style.Format = "$#,##0.00" sheet.Columns.AutoFit() ' 6. Save the changes back to the original file. workbook.Save() ' Or save as a new file to preserve the original. ' workbook.SaveAs("ProductReport_Updated.xlsx") End Sub End Module VB .NET 更深入的解释 修改现有文件遵循加载-编辑-保存的逻辑模式。 1.加载工作簿:我们不使用WorkBook.Create() ,而是使用WorkBook.Load("ProductReport.xlsx") 。 这会打开指定的文件,并将其内容解析为WorkBook对象,以便进行操作。 2.寻找插入点:追加数据的一个关键挑战是找到从哪里开始写入。 IronXL 通过sheet.Info.LastRowUsed属性简化了这一过程,该属性返回包含数据的最后一个Row对象。 我们只需获取它的行号,然后加一,即可找到第一个空行。 3.追加数据:写入新数据的过程与前面的示例相同。 我们遍历新的Product列表,并将新识别出的空白行中的单元格填充进去。 4.保存更改: workbook.Save()方法会将修改后的工作簿从内存中覆盖到原始文件。 如果需要保留原始文件,只需使用SaveAs()函数并指定一个新文件名,如注释掉的行所示。 如何将DataTable写入Excel工作表? 对于使用来自 SQL Server 等数据源的数据的开发人员来说,一项常见的任务是将DataTable的内容直接写入 Excel 工作表。 IronXL 通过内置方法简化了这一过程,无需手动遍历行和列。 本示例演示如何一步完成DataTable填充并将其导出到新的 Excel 文件。 Imports IronXL Imports System.Data Module Program Sub Main(args As String()) ' 1. Create and populate a DataTable. This often comes from a database query. Dim dt As New DataTable("EmployeeData") dt.Columns.Add("EmployeeID", GetType(Integer)) dt.Columns.Add("FullName", GetType(String)) dt.Columns.Add("Department", GetType(String)) dt.Columns.Add("HireDate", GetType(Date)) dt.Rows.Add(101, "John Smith", "Sales", New Date(2022, 5, 20)) dt.Rows.Add(102, "Jane Doe", "Engineering", New Date(2021, 8, 15)) dt.Rows.Add(103, "Peter Jones", "Marketing", New Date(2023, 1, 10)) ' 2. Create a new workbook. Dim workbook As WorkBook = WorkBook.Create() Dim sheet As WorkSheet = workbook.CreateWorkSheet("Employees") ' 3. Write the DataTable to the worksheet starting at cell A1. ' The second parameter (True) indicates that column headers should be included. sheet.Write(dt, "A1", True) ' 4. Apply some styling for a more polished look. sheet.Columns.AutoFit() Dim headerRange = sheet.GetRange("A1:D1") headerRange.Style.Font.Bold = True headerRange.Style.SetBackgroundColor("#C5D9F1") ' Light Blue Dim dateColumn = sheet.GetRange("D2:D" & dt.Rows.Count + 1) dateColumn.Style.Format = "yyyy-mm-dd" ' 5. Save the workbook. workbook.SaveAs("EmployeeDatabaseExport.xlsx") End Sub End Module Imports IronXL Imports System.Data Module Program Sub Main(args As String()) ' 1. Create and populate a DataTable. This often comes from a database query. Dim dt As New DataTable("EmployeeData") dt.Columns.Add("EmployeeID", GetType(Integer)) dt.Columns.Add("FullName", GetType(String)) dt.Columns.Add("Department", GetType(String)) dt.Columns.Add("HireDate", GetType(Date)) dt.Rows.Add(101, "John Smith", "Sales", New Date(2022, 5, 20)) dt.Rows.Add(102, "Jane Doe", "Engineering", New Date(2021, 8, 15)) dt.Rows.Add(103, "Peter Jones", "Marketing", New Date(2023, 1, 10)) ' 2. Create a new workbook. Dim workbook As WorkBook = WorkBook.Create() Dim sheet As WorkSheet = workbook.CreateWorkSheet("Employees") ' 3. Write the DataTable to the worksheet starting at cell A1. ' The second parameter (True) indicates that column headers should be included. sheet.Write(dt, "A1", True) ' 4. Apply some styling for a more polished look. sheet.Columns.AutoFit() Dim headerRange = sheet.GetRange("A1:D1") headerRange.Style.Font.Bold = True headerRange.Style.SetBackgroundColor("#C5D9F1") ' Light Blue Dim dateColumn = sheet.GetRange("D2:D" & dt.Rows.Count + 1) dateColumn.Style.Format = "yyyy-mm-dd" ' 5. Save the workbook. workbook.SaveAs("EmployeeDatabaseExport.xlsx") End Sub End Module VB .NET sheet.Write()方法用于此目的非常高效。 它可以智能地将DataTable结构映射到工作表,如果指定,还包括列标题。 对于任何数据驱动型应用程序来说,这都是一项强大的功能,与手动迭代相比,可以显著节省时间。 有关数据导入和导出的更多详细信息,请查看我们关于不同电子表格格式之间转换的教程。 编写 Excel 文件时我还能做什么? 数据录入仅仅是开始。 一个强大的 VB.NET Excel 库应该提供一整套功能,以自动执行任何电子表格任务。 IronXL 提供广泛的功能,包括: -公式:通过编程方式设置和计算公式。 您可以将公式分配给单元格的Value (例如, sheet("C1").Value = "=SUM(A1:B1)" ),IronXL 将计算结果。 -图表:根据工作表数据创建各种类型的图表,以可视化信息。 -数据验证:对单元格强制执行规则,例如只允许特定范围内的数字或下拉列表中的值。 -条件格式:根据单元格的值应用样式,帮助突出显示重要数据点。 -安全与保护:使用密码保护工作表或整个工作簿,以控制访问并防止修改。 这些功能允许您直接在 .NET 应用程序中完全自动化复杂的报告生成和数据分析工作流程。 立即开始使用 VB.NET 向 Excel 文件写入数据 如前所述,使用 IronXL 等现代库可以大大简化在 VB.NET 中向 Excel 文件写入数据的过程。 通过避免 Office Interop 的复杂性和依赖性,您可以构建更可靠、性能更佳、更易于部署的应用程序。 无论您需要从头开始创建 Excel 文件、向现有报表追加数据,还是导出DataTable ,IronXL 都提供了一个直观而强大的 API 来高效地完成工作。 今天在您的项目中使用 IronXL,免费试用。 第一步: 免费开始 您可以下载 IronXL并使用免费试用许可证进行试用,看看它如何轻松地集成到您的项目中。 对于有更广泛需求的用户,请记住 IronXL 也是Iron Suite for .NET的一部分。 只需一个许可证,即可访问一套全面的库,用于处理 PDF、条形码、OCR 等,为任何 .NET 开发人员提供卓越的价值。 常见问题解答 如何在 VB.NET 中写入 Excel 文件而不使用 Office Interop? 您可以使用 IronXL 库在 VB.NET 中写入 Excel 文件,而无需 Office Interop。IronXL 让您可以轻松创建、读取和操作 Excel 文件,而无需安装 Microsoft Excel。 使用 IronXL 而非 Office Interop 进行 Excel 自动化的好处是什么? IronXL 相对 Office Interop 提供了显著的优势,例如改善的性能、更简单的部署以及在无需安装 Microsoft Excel 的服务器上运行的能力。在 .NET 应用程序中简化了 Excel 自动化任务。 如何在 VB.NET 项目中安装 IronXL 以操作 Excel 文件? 要在 VB.NET 项目中安装 IronXL,请使用 Visual Studio 中的 NuGet 包管理器。搜索 IronXL.Excel 并安装它。或者,使用包管理器控制台执行命令:Install-Package IronXL.Excel。 如何在 VB.NET 中创建新 Excel 文件并插入数据? 使用 IronXL,您可以通过调用 WorkBook.Create() 创建一个新的 Excel 文件。访问工作表并使用 A1 标记法插入数据,例如 sheet("A1").Value = "Example Data"。使用 SaveAs() 方法保存文件。 如何使用 IronXL 向现有 Excel 文件追加数据? 要使用 IronXL 向现有 Excel 文件追加数据,请使用 WorkBook.Load("filename.xlsx") 加载工作簿。使用 sheet.Info.LastRowUsed 确定下一个空行并插入新数据。使用 workbook.Save() 保存修改。 在 VB.NET 中是否可以将 DataTable 导出到 Excel 工作表? 可以,IronXL 简化了将 DataTable 导出到 Excel 工作表。使用 sheet.Write() 方法可以高效地将整个表转移到工作表中。 IronXL 可以处理哪些 Excel 文件格式? IronXL 支持多种 Excel 文件格式,包括 .xlsx、.xls、.csv 和 .tsv,可以灵活处理各种类型的电子表格数据。 我可以用 IronXL 格式化 Excel 单元格并使用公式吗? 可以,IronXL 提供了广泛的格式化和公式支持。您可以以编程方式调整字体、颜色、边框和数字格式,并设置像 "=SUM(A1:A10)" 这样的 Excel 公式进行计算。 Jacob Mellor 立即与工程团队聊天 首席技术官 Jacob Mellor 是 Iron Software 的首席技术官,是 C# PDF 技术的先锋工程师。作为 Iron Software 核心代码库的原始开发者,自公司成立以来,他就塑造了公司的产品架构,并与首席执行官 Cameron Rimington 一起将其转变成一家公司,拥有50多人,服务于 NASA、特斯拉和全球政府机构。Jacob 拥有曼彻斯特大学 (1998-2001) 的一级荣誉土木工程学士学位。1999 年在伦敦创办了自己的第一家软件公司,并于 2005 年创建了他的第一个 .NET 组件后,他专注于解决微软生态系统中的复杂问题。他的旗舰 IronPDF 和 Iron Suite .NET 库在全球已获得超过 3000 万次的 NuGet 安装,其基础代码继续为全球使用的开发者工具提供支持。拥有 25 年商业经验和 41 年编程经验的 Jacob 仍专注于推动企业级 C#、Java 和 Python PDF 技术的创新,同时指导下一代技术领导者。 相关文章 已发布十二月 19, 2025 如何使用 C# Interop 与 IronXL.Excel 在 Excel 中创建透视表 无需 Office 依赖在 C# 中构建 Excel 数据透视表。IronXL 提供强大的数据处理功能,用于创建透视风格的报告,无需 Excel Interop 复杂化。 阅读更多 已发布十二月 18, 2025 C# 使用 IronXL.Excel 将带列标题的 DataGridView 导出到 Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多 已发布十二月 18, 2025 如何在 C# 中使用 IronXL 创建 Excel 报告 在 C# 中使用 IronXL 进行 Excel 报告生成。学习构建具有格式、公式和数据库集成的专业报告。 阅读更多 如何在 C# 中将数据集转换为 CSV如何在 Excel 中冻结行(初...
已发布十二月 19, 2025 如何使用 C# Interop 与 IronXL.Excel 在 Excel 中创建透视表 无需 Office 依赖在 C# 中构建 Excel 数据透视表。IronXL 提供强大的数据处理功能,用于创建透视风格的报告,无需 Excel Interop 复杂化。 阅读更多
已发布十二月 18, 2025 C# 使用 IronXL.Excel 将带列标题的 DataGridView 导出到 Excel 学习如何在将DataGridView数据导出到Excel时保留列头。使用IronXL库的C#逐步教程。 阅读更多
已发布十二月 18, 2025 如何在 C# 中使用 IronXL 创建 Excel 报告 在 C# 中使用 IronXL 进行 Excel 报告生成。学习构建具有格式、公式和数据库集成的专业报告。 阅读更多