How to Insert New Rows and Columns

by Chaknith Bin

The IronXL library provides 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
C# Excel DLL

Download DLL

Download DLL

Manually install into your project

Insert New Row Example

Utilize the InsertRow and InsertRows methods to add new rows to spreadsheet. These methods allow you to insert rows before a specified index position.

Please note
Inserting rows directly on the filter row can cause conflicts in the Excel file, requiring to run Excel repair in order to view the spreadsheet properly.

: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 utilize the RemoveRow method of 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 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) in between the table range may cause conflicts in the Excel file, which would require running 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 throw 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 the row and column index position follows 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.