Insert New Rows & Columns

IronXL library allows the ability to insert single & multiple rows and columns in C# code without using Office Interop.

Insert Row

The InsertRow and InsertRows methods enable adding brand-new rows. Using these methods will add row(s) before the specified index position.

using IronXL; // Import the IronXL library

class Program
{
    static void Main()
    {
        // Load an existing Excel file or create a new one
        var workbook = WorkBook.Load("example.xlsx");
        var sheet = workbook.WorkSheets.First();

        // Insert a single row before the 3rd row (zero-based)
        sheet.InsertRow(2);

        // Insert multiple rows before the 5th row (zero-based)
        sheet.InsertRows(4, 3); // Inserts 3 rows

        // Save changes to the Excel file
        workbook.SaveAs("example_modified.xlsx");
    }
}
using IronXL; // Import the IronXL library

class Program
{
    static void Main()
    {
        // Load an existing Excel file or create a new one
        var workbook = WorkBook.Load("example.xlsx");
        var sheet = workbook.WorkSheets.First();

        // Insert a single row before the 3rd row (zero-based)
        sheet.InsertRow(2);

        // Insert multiple rows before the 5th row (zero-based)
        sheet.InsertRows(4, 3); // Inserts 3 rows

        // Save changes to the Excel file
        workbook.SaveAs("example_modified.xlsx");
    }
}
Imports IronXL ' Import the IronXL library

Friend Class Program
	Shared Sub Main()
		' Load an existing Excel file or create a new one
		Dim workbook = WorkBook.Load("example.xlsx")
		Dim sheet = workbook.WorkSheets.First()

		' Insert a single row before the 3rd row (zero-based)
		sheet.InsertRow(2)

		' Insert multiple rows before the 5th row (zero-based)
		sheet.InsertRows(4, 3) ' Inserts 3 rows

		' Save changes to the Excel file
		workbook.SaveAs("example_modified.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Insert Column

Use InsertColumn and InsertColumns methods to add column(s) before the specified index position.

using IronXL; // Import the IronXL library

class Program
{
    static void Main()
    {
        // Load an existing Excel file or create a new one
        var workbook = WorkBook.Load("example.xlsx");
        var sheet = workbook.WorkSheets.First();

        // Insert a single column before the 2nd column (zero-based)
        sheet.InsertColumn(1);

        // Insert multiple columns before the 4th column (zero-based)
        sheet.InsertColumns(3, 2); // Inserts 2 columns

        // Save changes to the Excel file
        workbook.SaveAs("example_modified.xlsx");
    }
}
using IronXL; // Import the IronXL library

class Program
{
    static void Main()
    {
        // Load an existing Excel file or create a new one
        var workbook = WorkBook.Load("example.xlsx");
        var sheet = workbook.WorkSheets.First();

        // Insert a single column before the 2nd column (zero-based)
        sheet.InsertColumn(1);

        // Insert multiple columns before the 4th column (zero-based)
        sheet.InsertColumns(3, 2); // Inserts 2 columns

        // Save changes to the Excel file
        workbook.SaveAs("example_modified.xlsx");
    }
}
Imports IronXL ' Import the IronXL library

Friend Class Program
	Shared Sub Main()
		' Load an existing Excel file or create a new one
		Dim workbook = WorkBook.Load("example.xlsx")
		Dim sheet = workbook.WorkSheets.First()

		' Insert a single column before the 2nd column (zero-based)
		sheet.InsertColumn(1)

		' Insert multiple columns before the 4th column (zero-based)
		sheet.InsertColumns(3, 2) ' Inserts 2 columns

		' Save changes to the Excel file
		workbook.SaveAs("example_modified.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Please note that all the index positions mentioned above follow zero-based indexing.