IronXL 操作指南 插入行和列 How to Insert New Rows and Columns Chaknith Bin 已更新:七月 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 was translated from English: Does it need improvement? Translated View the article in English The IronXL library offers a convenient way to insert single or multiple rows and columns in C# code without using Office Interop. Quickstart: Add Rows or Columns with IronXL Effortlessly See how simply IronXL lets you insert rows or columns at any position using just one fluent API call. Get started quickly and modify Excel sheets without hassle. Get started making PDFs with NuGet now: Install IronXL with NuGet Package Manager PM > Install-Package IronXL.Excel Copy and run this code snippet. new WorkBook("example.xlsx").DefaultWorkSheet.InsertColumns(3, 2); Deploy to test on your live environment Start using IronXL in your project today with a free trial Free 30 day Trial Minimal Workflow (5 steps) Download the C# library for inserting new rows and columns in Excel Use the InsertRow and InsertRows methods to insert new rows Use the InsertColumn and InsertColumns methods to insert new columns Add data to the newly inserted rows and columns Export the edited Excel file to various file types Insert a New Row Example Use the InsertRow and InsertRows methods to add new rows to the spreadsheet. These methods enable you to insert rows at a specific index position. 请注意Inserting rows directly on the filter row can cause conflicts in the Excel file, requiring you to run Excel repair to properly view the spreadsheet. :path=/static-assets/excel/content-code-examples/how-to/add-rows-columns-rows.cs using IronXL; // Load existing spreadsheet WorkBook workBook = WorkBook.Load("sample.xlsx"); WorkSheet workSheet = workBook.DefaultWorkSheet; // Add a row before row 2 workSheet.InsertRow(1); // Insert multiple rows after row 3 workSheet.InsertRows(3, 3); workBook.SaveAs("addRow.xlsx"); Imports IronXL ' Load existing spreadsheet Private workBook As WorkBook = WorkBook.Load("sample.xlsx") Private workSheet As WorkSheet = workBook.DefaultWorkSheet ' Add a row before row 2 workSheet.InsertRow(1) ' Insert multiple rows after row 3 workSheet.InsertRows(3, 3) workBook.SaveAs("addRow.xlsx") $vbLabelText $csharpLabel Remove a Row Example To remove a row from the spreadsheet, you can use the GetRow method to select the desired row and then use the RemoveRow method on the selected row. 请注意It is not possible to remove the header row of the table. :path=/static-assets/excel/content-code-examples/how-to/add-rows-columns-remove-row.cs using IronXL; // Load existing spreadsheet WorkBook workBook = WorkBook.Load("sample.xlsx"); WorkSheet workSheet = workBook.DefaultWorkSheet; // Remove row 5 workSheet.GetRow(4).RemoveRow(); workBook.SaveAs("removeRow.xlsx"); Imports IronXL ' Load existing spreadsheet Private workBook As WorkBook = WorkBook.Load("sample.xlsx") Private workSheet As WorkSheet = workBook.DefaultWorkSheet ' Remove row 5 workSheet.GetRow(4).RemoveRow() workBook.SaveAs("removeRow.xlsx") $vbLabelText $csharpLabel Insert a New Column Example To add new column(s) before a specific index position in the table, you can utilize the InsertColumn and InsertColumns methods. 请注意Inserting new column(s) within the table range may cause conflicts in the Excel file, requiring you to run Excel repair to properly view the spreadsheet. To remove all empty rows and columns on the range borders, use the Trim() method. Currently, it is not possible to remove column(s). 警告Attempting to insert a new column into a completely empty sheet will result in a System.InvalidOperationException with the message 'Sequence contains no elements'. :path=/static-assets/excel/content-code-examples/how-to/add-rows-columns-columns.cs using IronXL; // Load existing spreadsheet WorkBook workBook = WorkBook.Load("sample.xlsx"); WorkSheet workSheet = workBook.DefaultWorkSheet; // Add a column before column A workSheet.InsertColumn(0); // Insert multiple columns after column B workSheet.InsertColumns(2, 2); workBook.SaveAs("addColumn.xlsx"); Imports IronXL ' Load existing spreadsheet Private workBook As WorkBook = WorkBook.Load("sample.xlsx") Private workSheet As WorkSheet = workBook.DefaultWorkSheet ' Add a column before column A workSheet.InsertColumn(0) ' Insert multiple columns after column B workSheet.InsertColumns(2, 2) workBook.SaveAs("addColumn.xlsx") $vbLabelText $csharpLabel 提示All row and column index positions follow zero-based indexing. 常见问题解答 如何使用 C# 在 Excel 中插入新行? 您可以使用 IronXL 在 Excel 中插入新行。该库提供 InsertRow 和 InsertRows 方法,以编程方式在电子表格中的特定索引位置添加行。 在 C# 中将列添加到 Excel 工作表的方法是什么? 要在 C# 中将列添加到 Excel 工作表,您可以使用 IronXL 的 InsertColumn 和 InsertColumns 方法。这些方法允许您在工作表中的指定索引位置插入列。 是否可以使用 C# 从 Excel 电子表格中删除行? 是的,使用 IronXL,您可以通过使用 GetRow 方法选择所需行,然后应用 RemoveRow 方法从 Excel 电子表格中删除行。 能否使用 IronXL 以编程方式从 Excel 工作表中删除列? 目前,IronXL 不支持直接删除列。然而,您可以使用 InsertColumn 和 InsertColumns 方法插入新列。 在使用 IronXL 在 Excel 中插入行或列时应采取哪些预防措施? 在某些区域(例如过滤器行或表格范围)内插入行或列可能导致 Excel 文件中的冲突。可能需要运行 Excel 修复才能正确查看电子表格。 如果我尝试使用 IronXL 向空的 Excel 表格中插入一列会发生什么? 使用 IronXL 向完全空的表格中插入列将触发 System.InvalidOperationException 异常,消息为“序列不包含元素”。 使用 IronXL 修改 Excel 工作簿后,我如何保存更改? 使用 IronXL 修改 Excel 工作簿后,您可以通过使用 SaveAs 方法指定更新后工作簿的文件路径来保存更改。 IronXL 在 Excel 中使用哪种索引系统来处理行和列? IronXL 使用基于零的索引来处理行和列,即行和列的索引从 0 开始。 Chaknith Bin 立即与工程团队聊天 软件工程师 Chaknith 在 IronXL 和 IronBarcode 工作。他在 C# 和 .NET 方面有着深厚的专业知识,帮助改进软件并支持客户。他从用户互动中获得的见解有助于更好的产品、文档和整体体验。 准备开始了吗? Nuget 下载 1,686,155 | 版本: 2025.11 刚刚发布 免费 NuGet 下载 总下载量:1,686,155 查看许可证