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.
How to Insert New Rows and Columns in Excel
- 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
Get started with IronXL
Start using IronXL in your project today with a free trial.
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
: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")
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
: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")
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
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
: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")