如何作为C#开发人员在Excel中添加行和列

How to Insert New Rows and Columns

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.

Nuget IconGet started making PDFs with NuGet now:

  1. Install IronXL with NuGet Package Manager

    PM > Install-Package IronXL.Excel

  2. Copy and run this code snippet.

    new WorkBook("example.xlsx").DefaultWorkSheet.InsertColumns(3, 2);
  3. Deploy to test on your live environment

    Start using IronXL in your project today with a free trial
    arrow pointer


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
Add New Row

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
Remove Row

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
Add New Column

提示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 刚刚发布