Insert or Delete Rows in an Excel Worksheet
Aspose.Cells for Python via .NET offers two methods for inserting and deleting rows: Cells.insert_rows and Cells.delete_rows. These methods are optimized for performance and do the job very quickly.
To insert or remove a number of rows, we recommend that you always use the Cells.insert_rows and Cells.delete_rows methods instead of using the Cells.insert_row or delete_row methods in a loop.
Aspose.Cells for Python via .NET works in the same way as Microsoft Excel does. When rows or columns are added, the worksheet content is shifted down and to the right. When rows or columns are removed, the worksheet content is shifted up or to the left. Any references in other worksheets and cells are updated when rows are added or removed.
from aspose.cells import Workbook | |
# For complete examples and data files, please go to https:# github.com/aspose-cells/Aspose.Cells-for-.NET | |
# The path to the documents directory. | |
dataDir = RunExamples.GetDataDir(".") | |
# Instantiate a Workbook object. | |
# Load a template file. | |
workbook = Workbook(dataDir + "book1.xlsx") | |
# Get the first worksheet in the book. | |
sheet = workbook.worksheets[0] | |
# Insert 10 rows at row index 2 (insertion starts at 3rd row) | |
sheet.cells.insert_rows(2, 10) | |
# Delete 5 rows now. (8th row - 12th row) | |
sheet.cells.delete_rows(7, 5) | |
# Save the excel file. | |
workbook.save(dataDir + "out_book1.out.xlsx") |