How to Insert New Rows and Columns

The IronXL library offers a convenient way to insert single or multiple rows and columns in C# code without using Office Interop.


Get started with IronXL

Start using IronXL in your project today with a free trial.

First Step:
green 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.

Please note
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.

Please note
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.

Please note
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).

Before proceeding
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

Tips
All row and column index positions follow zero-based indexing.

Frequently Asked Questions

How can I insert new rows in Excel programmatically?

To insert new rows in Excel using IronXL, you can use the InsertRow and InsertRows methods. These methods allow you to add rows at a specific index position in your spreadsheet.

What methods are used to insert columns in an Excel sheet programmatically?

To insert columns in an Excel sheet using IronXL, you can utilize the InsertColumn and InsertColumns methods. These methods let you add columns before a specific index position.

Is it possible to remove a row from an Excel spreadsheet programmatically?

Yes, you can remove a row from an Excel spreadsheet using IronXL by selecting the desired row with the GetRow method and then using the RemoveRow method on the selected row.

Can I remove columns in Excel programmatically?

Currently, removing columns is not supported in IronXL. However, you can insert new columns using the InsertColumn and InsertColumns methods.

Are there any precautions when inserting rows or columns in Excel programmatically?

Yes, inserting rows or columns directly in certain areas, like the filter row or within a table range, may cause conflicts in the Excel file. You may need to run Excel repair to view the spreadsheet properly.

What happens if I try to insert a new column into an empty sheet programmatically?

Inserting a new column into a completely empty sheet will result in a System.InvalidOperationException with the message 'Sequence contains no elements'.

How do I save changes to an Excel workbook after modifying it programmatically?

After making modifications to an Excel workbook using IronXL, you can save the changes using the SaveAs method, specifying the file path for the updated workbook.

What indexing system is used for rows and columns in programmatic Excel manipulation?

IronXL uses zero-based indexing for row and column positions, meaning the index starts at 0.

Chaknith related to Insert a New Column Example
Software Engineer
Chaknith is the Sherlock Holmes of developers. It first occurred to him he might have a future in software engineering, when he was doing code challenges for fun. His focus is on IronXL and IronBarcode, but he takes pride in helping customers with every product. Chaknith leverages his knowledge from talking directly with customers, to help further improve the products themselves. His anecdotal feedback goes beyond Jira tickets and supports product development, documentation and marketing, to improve customer’s overall experience.When he isn’t in the office, he can be found learning about machine learning, coding and hiking.