Test in production without watermarks.
Works wherever you need it to.
Get 30 days of fully functional product.
Have it up and running in minutes.
Full access to our support engineering team during your product trial
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
Further Reading: How to Insert New Rows and Columns