Inserting and 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.
How to Manage Rows/Columns
Aspose.Cells provides a Workbook class that represents a Microsoft Excel file. The Workbook class contains a WorksheetCollection that allows access to each worksheet in an Excel file. A worksheet is represented by the Worksheet class. The Worksheet class provides a Cells collection that represents all cells in the worksheet.
The Cells collection provides several methods for managing rows and columns in a worksheet. Some of these are discussed below.
How to Insert a Row
Insert a row into at any location by calling the insertRows method of the Cells collection. The insertRows method takes the index of the row where the new row will be inserted as the first argument, and the number of rows to be inserted as the second argument.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getSharedDataDir(InsertingARow.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Inserting a row into the worksheet at 3rd position | |
worksheet.getCells().insertRows(2, 1); | |
// Saving the modified Excel file in default (that is Excel 2000) format | |
workbook.save(dataDir + "InsertingARow_out.xls"); |
How to Insert Multiple Rows
To insert multiple rows into the 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.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getSharedDataDir(InsertingMultipleRows.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Inserting 10 rows into the worksheet starting from 3rd row | |
worksheet.getCells().insertRows(2, 10); | |
// Saving the modified Excel file in default (that is Excel 2000) format | |
workbook.save(dataDir + "InsertingMultipleRows_out.xls"); |
How to Insert a Row with Formatting
To insert a row with formatting options, use the insertRows overload that takes InsertOptions as a parameter. Set the CopyFormatType property of InsertOptions class with CopyFormatType Enumeration. The CopyFormatType Enumeration has three members as listed below.
- SAME_AS_ABOVE: Formats the row same as the above row.
- SAME_AS_BELOW: Formats the row same as below row.
- CLEAR: Clears the formatting.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getSharedDataDir(InsertingARowWithFormatting.class) + "RowsAndColumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Setting Formatting options | |
InsertOptions insertOptions = new InsertOptions(); | |
insertOptions.setCopyFormatType(CopyFormatType.SAME_AS_ABOVE); | |
// Inserting a row into the worksheet at 3rd position | |
worksheet.getCells().insertRows(2, 1, insertOptions); | |
// Saving the modified Excel file | |
workbook.save(dataDir + "InsertingARowWithFormatting_out.xlsx"); |
How to Delete a Row
To delete a row at any location, 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.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(DeleteARow.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Deleting 3rd row from the worksheet | |
worksheet.getCells().deleteRows(2, 1, true); | |
// Saving the modified Excel file in default (that is Excel 2000) format | |
workbook.save(dataDir + "DeleteARow_out.xls"); |
How to Delete 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.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(DeleteMultipleRows.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Deleting 10 rows from the worksheet starting from 3rd row | |
worksheet.getCells().deleteRows(2, 10, true); | |
// Saving the modified Excel file in default (that is Excel 2000) format | |
workbook.save(dataDir + "DeleteMultipleRows_out.xls"); |
How to Insert one or Multiple Columns
Developers can also insert a column into the worksheet at any location by calling the insertColumns method of the Cells collection. The insertColumns method takes two parameters:
- Column index, the index of the column from where the column will be inserted
- Number of columns, the total number of columns that need to be inserted
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
String dataDir = Utils.getSharedDataDir(InsertingAColumn.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "book1.xls"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Inserting a column into the worksheet at 2nd position | |
worksheet.getCells().insertColumns(1, 1); | |
// Saving the modified Excel file in default (that is Excel 2000) format | |
workbook.save(dataDir + "InsertingAColumn_out.xls"); |
How to Delete a Column
To delete a column from the worksheet at any location, call the deleteColumns method of the Cells collection. The deleteColumns method takes the following parameters:
- Column index: the index of the column from where the column will be deleted.
- Number of columns: the total number of columns that need to be deleted.
- Update Reference: Boolean parameter to indicate whether to update references in other worksheets.
// For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-Java | |
// The path to the documents directory. | |
String dataDir = Utils.getSharedDataDir(DeleteAColumn.class) + "rows_cloumns/"; | |
// Instantiating a Workbook object | |
Workbook workbook = new Workbook(dataDir + "Book1.xlsx"); | |
// Accessing the first worksheet in the Excel file | |
Worksheet worksheet = workbook.getWorksheets().get(0); | |
// Deleting a column from the worksheet at 2nd position | |
worksheet.getCells().deleteColumns(1, 1, true); | |
// Saving the modified Excel file in default (that is Excel 2000) format | |
workbook.save(dataDir + "DeleteAColumn_out.xls"); |