在Excel工作表中插入或删除行
Contents
[
Hide
]
在创建新工作表或处理现有工作表时,您可能需要添加额外的行或列以容纳数据。其他时候,您可能需要从工作表中指定位置删除行或列。
Aspose.Cells for Python via .NET提供了两种插入和删除行的方法:Cells.insert_rows和Cells.delete_rows。这些方法经过了性能优化,可以非常快速地完成任务。
要插入或删除多行,我们建议始终使用Cells.insert_rows和Cells.delete_rows方法,而不是在循环中使用Cells.insert_row或delete_row方法。
Aspose.Cells for Python via .NET的工作方式与Microsoft Excel相同。当添加行或列时,工作表内容会向下和向右移动。当移除行或列时,工作表内容会向上或向左移动。在添加或删除行时,其他工作表和单元格中的引用会得到更新。
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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") |