复制和移动工作表
有时,您确实需要一些具有共同格式和数据的工作表。例如,如果您在季度预算上工作,您可能希望创建一个包含具有相同列标题、行标题和公式的工作表的工作簿。有一种方法可以做到这一点:先创建一个工作表,然后进行复制。
Aspose.Cells for Python via .NET支持在工作簿内或工作簿之间复制和移动工作表。工作表以数据、格式、表格、矩阵、图表、图像和其他对象完整地精确复制。
如何使用Microsoft Excel移动或复制工作表
以下是在Microsoft Excel中在工作簿内部或不同工作簿之间复制和移动工作表所涉及的步骤。
- 要将工作表移动或复制到另一个工作簿中,请打开将要接收工作表的工作簿。
- 切换到包含您想要移动或复制的工作表的工作簿,然后选择这些工作表。
- 在“编辑”菜单上,单击“移动或复制工作表”
- 在“选择工作簿”对话框中,单击要接收工作表的工作簿。
- 要将所选工作表移动或复制到新工作簿中,请单击“新建工作簿”
- 在“工作表之前”框中,单击要在其之前插入移动或复制的工作表。
- 要复制工作表而不是移动它们,请选择“创建副本”复选框。
如何使用Aspose.Cells for Python Excel库在工作簿内复制工作表
Aspose.Cells for Python via .NET提供了一个重载的方法,Aspose.Cells.WorksheetCollection.add_copy(),用于向集合中添加工作表并从现有工作表复制数据。该方法的一个版本以源工作表的索引作为参数。另一个版本以源工作表的名称作为参数。
以下示例显示了如何在工作簿内复制现有工作表。
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(".") | |
InputPath = dataDir + "book1.xls" | |
# Open an existing Excel file. | |
wb = Workbook(InputPath) | |
# Create a Worksheets object with reference to | |
# the sheets of the Workbook. | |
sheets = wb.worksheets | |
# Copy data to a new sheet from an existing | |
# sheet within the Workbook. | |
sheets.add_copy("Sheet1") | |
# Save the Excel file. | |
wb.save(dataDir + "CopyWithinWorkbook_out.xls") |
如何在工作簿之间复制工作表
Aspose.Cells for Python via .NET提供了一个方法,Aspose.Cells.Worksheet.copy(),用于在工作簿内或工作簿之间从源工作表复制数据和格式到另一个工作表。该方法以源工作表对象作为参数。
以下示例显示了如何将一个工作表从一个工作簿复制到另一个工作簿。
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(".") | |
InputPath = dataDir + "book1.xls" | |
# Create a Workbook. | |
# Open a file into the first book. | |
excelWorkbook0 = Workbook(InputPath) | |
# Create another Workbook. | |
excelWorkbook1 = Workbook() | |
# Copy the first sheet of the first book into second book. | |
excelWorkbook1.worksheets[0].copy(excelWorkbook0.worksheets[0]) | |
# Save the file. | |
excelWorkbook1.save(dataDir + "CopyWorksheetsBetweenWorkbooks_out.xls") |
以下示例显示了如何将一个工作表从一个工作簿复制到另一个。
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(".") | |
# Create a new Workbook. | |
excelWorkbook0 = Workbook() | |
# Get the first worksheet in the book. | |
ws0 = excelWorkbook0.worksheets[0] | |
# Put some data into header rows (A1:A4) | |
for i in range(5): | |
headerRow = "Header Row " + str(i) | |
ws0.cells.get(i, 0).put_value(headerRow) | |
# Put some detail data (A5:A999) | |
for i in range(5, 1000): | |
detailRow = "Detail Row " + str(i) | |
ws0.cells.get(i, 0).put_value(detailRow) | |
# Define a pagesetup object based on the first worksheet. | |
pagesetup = ws0.page_setup | |
# The first five rows are repeated in each page... | |
# It can be seen in print preview. | |
pagesetup.print_title_rows = "$1:$5" | |
# Create another Workbook. | |
excelWorkbook1 = Workbook() | |
# Get the first worksheet in the book. | |
ws1 = excelWorkbook1.worksheets[0] | |
# Name the worksheet. | |
ws1.name = "MySheet" | |
# Copy data from the first worksheet of the first workbook into the | |
# first worksheet of the second workbook. | |
ws1.copy(ws0) | |
# Save the excel file. | |
excelWorkbook1.save(dataDir + "CopyWorksheetFromWorkbookToOther_out.xls") |
如何在工作簿内移动工作表
Aspose.Cells for Python via .NET提供了一个方法Aspose.Cells.Worksheet.move_to(),用于将工作表移动到同一电子表格中的另一个位置。该方法以目标工作表的索引作为参数。
以下示例显示了如何将工作表移动到工作簿内的另一个位置。
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(".") | |
InputPath = dataDir + "book1.xls" | |
# Open an existing excel file. | |
wb = Workbook(InputPath) | |
# Create a Worksheets object with reference to | |
# the sheets of the Workbook. | |
sheets = wb.worksheets | |
# Get the first worksheet. | |
worksheet = sheets[0] | |
# Move the first sheet to the third position in the workbook. | |
worksheet.move_to(2) | |
# Save the excel file. | |
wb.save(dataDir + "MoveWorksheet_out.xls") |