How to Add Rows and Columns in Excel as a C# Developer

In this tutorial, C# developers learn how to add rows and columns to Excel using IronXL with Visual Studio 2022. The process begins by downloading IronXL from the NuGet Package Manager, ensuring the latest version for optimal performance. Developers load the Excel file using the WorkBook.Load method and then manipulate the default worksheet. To add a column before Column B, the InsertColumn method is used, while the InsertColumns method allows adding multiple columns. Similarly, rows are added using the InsertRow and InsertRows methods. Removing a row involves getting the row with the GetRow method and then applying the RemoveRow method. The tutorial emphasizes zero-based indexing for accurate placement. After modifications, the workbook is saved with a new file name using the SaveAs method to preserve the original file. The tutorial concludes by demonstrating the successful execution of the code, showcasing the modified Excel file. This guide provides a comprehensive approach to Excel manipulation using C# and IronXL, ensuring a smooth and efficient process for developers.

using IronXL;  // Import IronXL namespace

class ExcelManipulation
{
    public static void Main()
    {
        // Load the Excel workbook
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Access the default worksheet
        WorkSheet sheet = workbook.DefaultWorkSheet;

        // Insert a column before Column B (zero-based index 1)
        sheet.InsertColumn(1);

        // Insert multiple columns before Column C (zero-based index 2)
        sheet.InsertColumns(2, 3);  // Inserts three columns at index 2

        // Insert a row at row 2 (zero-based index 1)
        sheet.InsertRow(1);

        // Insert multiple rows starting from row 3 (zero-based index 2)
        sheet.InsertRows(2, 2);  // Inserts two rows starting at index 2

        // Remove a specific row (e.g., row 0)
        sheet.RemoveRow(0);  // Remove row at index 0

        // Save the modified workbook with a new file name
        workbook.SaveAs("example_modified.xlsx");
    }
}
using IronXL;  // Import IronXL namespace

class ExcelManipulation
{
    public static void Main()
    {
        // Load the Excel workbook
        WorkBook workbook = WorkBook.Load("example.xlsx");

        // Access the default worksheet
        WorkSheet sheet = workbook.DefaultWorkSheet;

        // Insert a column before Column B (zero-based index 1)
        sheet.InsertColumn(1);

        // Insert multiple columns before Column C (zero-based index 2)
        sheet.InsertColumns(2, 3);  // Inserts three columns at index 2

        // Insert a row at row 2 (zero-based index 1)
        sheet.InsertRow(1);

        // Insert multiple rows starting from row 3 (zero-based index 2)
        sheet.InsertRows(2, 2);  // Inserts two rows starting at index 2

        // Remove a specific row (e.g., row 0)
        sheet.RemoveRow(0);  // Remove row at index 0

        // Save the modified workbook with a new file name
        workbook.SaveAs("example_modified.xlsx");
    }
}
Imports IronXL ' Import IronXL namespace

Friend Class ExcelManipulation
	Public Shared Sub Main()
		' Load the Excel workbook
		Dim workbook As WorkBook = WorkBook.Load("example.xlsx")

		' Access the default worksheet
		Dim sheet As WorkSheet = workbook.DefaultWorkSheet

		' Insert a column before Column B (zero-based index 1)
		sheet.InsertColumn(1)

		' Insert multiple columns before Column C (zero-based index 2)
		sheet.InsertColumns(2, 3) ' Inserts three columns at index 2

		' Insert a row at row 2 (zero-based index 1)
		sheet.InsertRow(1)

		' Insert multiple rows starting from row 3 (zero-based index 2)
		sheet.InsertRows(2, 2) ' Inserts two rows starting at index 2

		' Remove a specific row (e.g., row 0)
		sheet.RemoveRow(0) ' Remove row at index 0

		' Save the modified workbook with a new file name
		workbook.SaveAs("example_modified.xlsx")
	End Sub
End Class
$vbLabelText   $csharpLabel

Further Reading: How to Insert New Rows and Columns

Regan Pun
Software Engineer
Regan graduated from the University of Reading, with a BA in Electronic Engineering. Before joining Iron Software, his previous job roles had him laser-focused on single tasks; and what he most enjoys at Iron Software is the spectrum of work he gets to undertake, whether it’s adding value to sales, technical support, product development or marketing. He enjoys understanding the way developers are using the Iron Software library, and using that knowledge to continually improve documentation and develop the products.
< PREVIOUS
How to Auto Resize Rows and Columns in Excel
NEXT >
How to Add Freeze Panes to Spreadsheets in Excel