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.
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.
Get started making PDFs with NuGet now:
Install IronPDF with NuGet Package Manager
Copy and run this code snippet.
new WorkBook("example.xlsx").DefaultWorkSheet.InsertColumns(3, 2);Deploy to test on your live environment
Minimal Workflow (5 steps)
- 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
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.
: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.
: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.
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")
Frequently Asked Questions
How can I insert new rows in Excel using C#?
You can insert new rows in Excel using IronXL. The library provides InsertRow and InsertRows methods to programmatically add rows at a specific index position in your spreadsheet.
What is the method to add columns to an Excel sheet in C#?
To add columns to an Excel sheet in C#, you can use IronXL's InsertColumn and InsertColumns methods. These allow you to insert columns at a specified index position within the sheet.
Is it possible to remove a row from an Excel spreadsheet using C#?
Yes, with IronXL, you can remove a row from an Excel spreadsheet by using the GetRow method to select the desired row and then applying the RemoveRow method.
Can columns be removed from an Excel sheet programmatically with IronXL?
Currently, IronXL does not support removing columns directly. However, you can insert new columns using the InsertColumn and InsertColumns methods.
What precautions should be taken when inserting rows or columns in Excel using IronXL?
Inserting rows or columns within certain areas, such as a filter row or a table range, may lead to conflicts in the Excel file. It may be necessary to run Excel repair to view the spreadsheet correctly.
What happens if I try to insert a column into an empty Excel sheet using IronXL?
Inserting a column into a completely empty sheet using IronXL will trigger a System.InvalidOperationException with the message 'Sequence contains no elements'.
How do I save modifications to an Excel workbook after using IronXL?
After modifying an Excel workbook with IronXL, you can save the changes by using the SaveAs method, specifying the file path for the updated workbook.
What indexing system does IronXL use for rows and columns in Excel?
IronXL uses zero-based indexing for rows and columns, meaning the index for both rows and columns starts at 0.
