How to Insert New Rows and Columns in C#
The IronXL library offers a convenient way to insert single or multiple rows and columns in C# code without using Office Interop. Use InsertRow/InsertRows and InsertColumn/InsertColumns methods at any index position to dynamically modify Excel spreadsheets programmatically.
Quickstart: Add Rows or Columns with IronXL Effortlessly
IronXL enables you to insert rows or columns at any position using one fluent API call. Modify Excel sheets efficiently with minimal code.
Get started making PDFs with NuGet now:
Install IronXL 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
InsertRowandInsertRowsmethods to insert new rows - Use the
InsertColumnandInsertColumnsmethods to insert new columns - Add data to the newly inserted rows and columns
- Export the edited Excel file to various file types
How Do I Insert New Rows in Excel Using C#?
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. The IronXL API Reference provides comprehensive documentation on all available row manipulation methods.
What Is the Syntax for Adding a Single Row?
:path=/static-assets/excel/content-code-examples/how-to/add-rows-columns-rows.csusing 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")The InsertRow method accepts a single parameter indicating the zero-based index where the new row should be inserted. All existing rows at and below that position shift down automatically. For more complex scenarios involving working with data, you can combine row insertion with formulas and calculations.
How Do Multiple Row Insertions Work?

When using InsertRows, specify both the starting index and the number of rows to insert. This method proves invaluable when preparing data templates or expanding datasets dynamically. The method maintains all existing formatting and formulas, adjusting cell references automatically.
What Are Common Pitfalls When Adding Rows?
Several challenges can arise when inserting rows programmatically:
- Index Confusion: IronXL uses zero-based indexing, so row 1 in Excel corresponds to index 0 in code
- Table Boundaries: Inserting rows within named tables requires special consideration - see our guide on named tables
- Performance Impact: When inserting many rows, consider batch operations to improve performance as detailed in our performance milestones
- Formula Updates: Cell references in formulas update automatically, but absolute references remain fixed
Why Does the Index Position Matter?
The index position determines where your new row appears and how existing data shifts. Using incorrect indices can overwrite data or create gaps in your spreadsheet. For complex data manipulation scenarios, consider reviewing our guide on selecting ranges to better understand positioning.
How Can I Remove Rows from an Excel Sheet?
To remove a row from the spreadsheet, use the GetRow method to select the desired row and then use the RemoveRow method on the selected row. This two-step process ensures precise control over which data gets removed.
What Is the Basic Removal Syntax?
:path=/static-assets/excel/content-code-examples/how-to/add-rows-columns-remove-row.csusing 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")When Should I Remove Rows Programmatically?
Row removal proves essential in several scenarios:
- Data Cleanup: Remove empty or invalid rows from imported datasets
- Dynamic Filtering: Delete rows that don't meet specific criteria
- Template Preparation: Clear sample data before generating reports
- Performance Optimization: Reduce file size by removing unnecessary rows
For more advanced data manipulation, explore our tutorial on editing Excel files which covers comprehensive editing techniques.
What Happens to Data Below Removed Rows?

When you remove a row, all rows below it automatically shift up to fill the gap. This maintains data continuity without manual intervention. Formulas referencing the removed row may return errors, so consider updating dependent calculations. For handling formula updates, see our guide on editing formulas.
How Do I Add New Columns to Excel Spreadsheets?
To add new columns before a specific index position in the table, utilize the InsertColumn and InsertColumns methods. These methods mirror the row insertion functionality but operate on the horizontal axis.
System.InvalidOperationException with the message 'Sequence contains no elements'.What Code Adds Single vs Multiple Columns?
:path=/static-assets/excel/content-code-examples/how-to/add-rows-columns-columns.csusing 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")How Does Column Indexing Work?
Column indexing follows the same zero-based pattern as rows:
- Column A = index 0
- Column B = index 1
- Column C = index 2
This consistency simplifies working with both dimensions of your spreadsheet. When combined with cell data formatting, you can create properly structured data tables programmatically.
What Are the Limitations of Column Operations?

Current limitations include:
- No Direct Column Removal: Unlike rows, columns cannot be directly removed (workaround: copy needed data to new sheet)
- Table Constraints: Columns within named tables require special handling
- Maximum Column Limit: Excel supports up to 16,384 columns (XFD)
- Performance Considerations: Large column insertions may impact file size significantly
For working around these limitations, our troubleshooting guide on file size limits provides optimization strategies.
Best Practices for Row and Column Operations
When modifying Excel structure programmatically:
- Always Validate Indices: Check that your target index exists before operations
- Batch Operations: Group multiple insertions for better performance
- Preserve Formatting: Use style copying when inserting formatted rows/columns
- Test Edge Cases: Verify behavior at sheet boundaries (row 1, column A)
- Handle Exceptions: Implement proper error handling for invalid operations
For comprehensive Excel automation workflows, explore our tutorial on creating Excel files which demonstrates these principles in action.
Frequently Asked Questions
How do I insert new rows in Excel using C#?
Use IronXL's InsertRow and InsertRows methods to add new rows to Excel spreadsheets. The InsertRow method adds a single row at a specified zero-based index position, while InsertRows allows you to insert multiple rows at once. IronXL automatically shifts existing rows down and maintains formatting and formulas.
What is the syntax for adding columns to an Excel file programmatically?
IronXL provides InsertColumn and InsertColumns methods for adding columns to Excel files. Similar to row insertion, you specify the index position where columns should be inserted. For example, InsertColumns(3, 2) inserts 2 columns starting at index position 3.
Can I insert rows and columns without Microsoft Office installed?
Yes, IronXL allows you to insert rows and columns in Excel files without requiring Microsoft Office or Office Interop. It's a standalone C# library that works directly with Excel file formats, making it ideal for server environments or systems without Office installations.
What happens to existing data when inserting new rows or columns?
When using IronXL's insertion methods, existing data automatically shifts to accommodate the new rows or columns. Rows shift down when inserting rows, and columns shift right when inserting columns. All formatting, formulas, and cell references are automatically adjusted.
How do I add data to newly inserted rows and columns?
After inserting rows or columns with IronXL, you can populate them using standard cell assignment methods. The library provides a fluent API for accessing cells by their coordinates, allowing you to set values, formulas, and formatting in the newly created space.
What are the common pitfalls when programmatically adding rows?
Common challenges include zero-based indexing confusion (Excel row 1 = index 0 in IronXL), conflicts when inserting on filter rows, and performance considerations for large batch operations. IronXL's documentation provides guidance on handling named tables and optimizing performance for bulk insertions.






