How to Insert New Rows and Columns

by Chaknith Bin

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


C# NuGet Library for Excel

Install with NuGet

Install-Package IronXL.Excel
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

C# NuGet Library for Excel

Install with NuGet

Install-Package IronXL.Excel
or
Java PDF JAR

Download DLL

Download DLL

Manually install into your project

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

First Step:
green arrow pointer

Check out IronXL on Nuget for quick installation and deployment. With over 8 million downloads, it's transforming Excel with C#.

C# NuGet Library for Excel nuget.org/packages/IronXL.Excel/
Install-Package IronXL.Excel

Consider installing the IronXL DLL directly. Download and manually install it for your project or GAC form: IronXL.zip

Manually install into your project

Download DLL

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 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")
VB   C#
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")
VB   C#
Add New 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 to run Excel repair to properly view the spreadsheet.

To remove all empty rows and columns on the range borders, you can 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' exception.

: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")
VB   C#
Add New Column

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

Chaknith Bin

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.