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.
IronXL enables you to insert rows or columns at any position using one fluent API call. Modify Excel sheets efficiently with minimal code.
-
1Install IronXL with NuGet Package Manager
-
2Copy and run this code snippet.
new WorkBook("example.xlsx").DefaultWorkSheet.InsertColumns(3, 2);C# -
3Deploy to test on your live environment
Start using IronXL in your project today with a free trial
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?
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")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.
InsertRow``InsertRows
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?
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")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.
RemoveColumn method, just like rows.System.InvalidOperationException with the message 'Sequence contains no elements'.What Code Adds Single vs Multiple Columns?
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")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:
- Direct Column Removal: Columns can be removed directly via the
RemoveColumnmethod (RangeColumn.RemoveColumn()orRange.RemoveColumn(int)), parallel toRemoveRow - 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 can I add rows to an Excel spreadsheet using C#?
You can add rows to an Excel spreadsheet using IronXL by utilizing the `InsertRow` and `InsertRows` methods, which allow you to insert single or multiple rows at any specified index.
What method should I use to add columns in C# without Office Interop?
You should use IronXL's `InsertColumn` and `InsertColumns` methods to add columns in C#, which operates independently of Office Interop, ensuring a seamless integration with your application.
What is the syntax for inserting multiple rows at a specific position in an Excel sheet?
The syntax involves using IronXL's `InsertRows` method, where you specify the starting index and the number of rows to insert, such as: `workSheet.InsertRows(3, 3);`.
How do I remove a row from an Excel spreadsheet programmatically?
To remove a row programmatically, first use the `GetRow` method to select the row, then apply the `RemoveRow` method to delete it. This maintains data integrity by shifting subsequent rows up automatically.
What issues should I consider when inserting rows and columns in Excel using IronXL?
Consider zero-based indexing (where row 1 is index 0), the effect on named table boundaries, and the performance impact of large insertions, as detailed in IronXL's documentation.
How does IronXL handle formula updates when modifying rows or columns?
IronXL automatically updates cell references in formulas when rows or columns are inserted or removed, although absolute references will remain unchanged.
Can IronXL insert columns into a completely empty Excel sheet?
Attempting to insert a column into a completely empty sheet will result in a `System.InvalidOperationException`, as IronXL requires a pre-existing data structure to perform insertions.
What best practices should I follow when using IronXL to modify Excel structure?
Best practices include validating indices, using batch operations for performance, preserving formatting, testing edge cases, and implementing appropriate error handling.
What are the limitations of inserting columns within named tables using IronXL?
Inserting columns within named tables may require special handling to maintain table integrity, as column insertions can affect table structure and existing data mappings.
How can I optimize performance when adding multiple rows or columns with IronXL?
To optimize performance, use batch operations to group multiple insertions, and refer to IronXL's performance milestones documentation for strategies on improving process efficiency.

Curtis Chau holds a Bachelor’s degree in Computer Science (Carleton University) and specializes in front-end development with expertise in Node.js, TypeScript, JavaScript, and React. Passionate about crafting intuitive and aesthetically pleasing user interfaces, Curtis enjoys working with modern frameworks and creating well-structured, visually appealing manuals.