Inserting, Deleting Rows and Columns
Introduction
Whether creating a new worksheet from scratch or working on an existing worksheet, we may need to add extra rows or columns to accommodate more data. Inversely, we may also need to delete rows or columns from specified positions in the worksheet. To fulfill these requirements, Aspose.Cells provides a very simplest set of classes and methods, discussed below.
Managing Rows and Columns
Aspose.Cells provides a class, Workbook, that represents a Microsoft Excel file. The Workbook class contains an Worksheets collection that allows access to each worksheet in an Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides an Cells collection that represents all cells in the worksheet.
The Cells collection provides several methods managing rows and columns in a worksheet. Some of these are discussed below.
Insert a Row
Insert a row into the worksheet at any location by calling the InsertRow method of the Cells collection. The InsertRow method takes the index of the row where the new row will be inserted.
Inserting Multiple Rows
To insert multiple rows into a worksheet, call the InsertRows method of the Cells collection. The InsertRows method takes two parameters:
- Row index, the index of the row from where the new rows will be inserted.
- Number of rows, the total number of rows that need to be inserted.
Deleting Multiple Rows
To delete multiple rows from a worksheet, call the DeleteRows method of the Cells collection. The DeleteRows method takes two parameters:
- Row index, the index of the row from where the rows will be deleted.
- Number of rows, the total number of rows that need to be deleted.
Insert a Column
Developers can also insert a column into the worksheet at any location by calling the InsertColumn method of the Cells collection. InsertColumn method takes the index of the column where the new column will be inserted.
Delete a Column
To delete a column from the worksheet at any location, call the DeleteColumn method of the Cells collection. The DeleteColumn method takes the index of the column to delete.