Excel files are widely used to create and manipulate spreadsheets within different applications programmatically. While processing a spreadsheet, you may need to add new rows and columns or delete the existing ones from the worksheets. To achieve that, this article shows how to add or delete rows and columns in Excel in C#.
- C# API to Add or Delete Excel Rows and Columns
- Add Rows in an Excel Worksheet in C#
- Add Columns in an Excel Worksheet in C#
- Delete Rows in an Excel Worksheet in C#
- Delete Columns in an Excel Worksheet in C#
- Get a Free License
C# Library to Add or Delete Excel Rows and Columns
To add or delete rows and columns in Excel XLSX/XLS files, we’ll use Aspose.Cells for .NET. It is a high-speed and powerful library to create and process Excel files seamlessly. In addition, it provides a high fidelity converter for back and forth conversion of XLSX files. You can download it’s DLL from the downloads section or install it from NuGet.
PM> Install-Package Aspose.Cells
How to Add Rows in Excel in C#
The following are the steps to add rows in an Excel worksheet in C#.
- First, load the Excel file using the Workbook class.
- Then, access the desired worksheet by index from Workbook.Worksheets collection.
- Add rows using Worksheet.Cells.InsertRows(rowIndex, totalRows) method in which the first parameter is the index to add row on and the second parameter is the number of rows you want to add.
- Finally, save the updated file using Workbook.Save(fileName) method.
The following code sample shows how to add rows in an Excel worksheet using C#.
How to Add Columns in Excel in C#
The following are the steps to add columns in an Excel worksheet using C#.
- First, load the Excel file using the Workbook class.
- Then, access the desired worksheet by index from Workbook.Worksheets collection.
- Add columns using Worksheet.Cells.InsertColumns(columnIndex, totalColumns) method. The first parameter is the index to add column on and the second parameter is the number of columns you want to add.
- Finally, save the updated file using Workbook.Save(fileName) method.
The following code sample shows how to add columns in an Excel worksheet using C#.
Delete Rows in an Excel XLSX in C#
The following are the steps to delete rows from an Excel worksheet using C#.
- First, load the Excel file using the Workbook class.
- Access the desired worksheet by index from Workbook.Worksheets collection.
- Delete rows using Worksheet.Cells.DeleteRows(rowIndex, totalRows) method in which the first parameter is the row index and the second parameter is the number of rows you want to delete.
- Finally, save the updated file using Workbook.Save(fileName) method.
The following code sample shows how to delete rows from an Excel worksheet in C#.
Delete Columns in Excel XLSX in C#
The following are the steps to delete columns from an Excel worksheet using C#.
- First, load the Excel file using the Workbook class.
- Access the desired worksheet by index from Workbook.Worksheets collection.
- Delete columns using Worksheet.Cells.DeleteColumns(columnIndex, totalColumns, updateReference) method. The first parameter is the column index, the second parameter is the number of columns you want to delete and the third parameter indicates if references need to be updated in other worksheets.
- Finally, save the updated file using Workbook.Save(fileName) method.
The following code sample shows how to delete columns from an Excel worksheet using C#.
Get a Free API License
You can get a free temporary license in order to use the API without evaluation limitations.
Conclusion
In this article, you have learned how to work with rows and columns in Excel programmatically. Particularly, you have seen how to add or delete rows and columns in Excel worksheets in C#. In addition, you can explore more about the C# Excel API using the documentation. Furthermore, you can share your queries with us via our forum.