复制Excel的范围
Contents
[
Hide
]
介绍
在Excel中,您可以选择一个范围,复制该范围,然后以特定选项粘贴到同一工作表、其他工作表或其他文件。
使用Aspose.Cells for Python Excel库复制范围
Aspose.Cells for Python via .NET提供了一些重载的Range.copy方法来复制范围。 和Range.copy_style仅复制范围的样式;Range.copy_data仅复制范围的值
复制范围
创建两个范围:源范围、目标范围,然后使用Range.copy方法将源范围复制到目标范围。
查看以下代码:
This file contains 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 | |
# Instantiate a new Workbook. | |
workbook = Workbook() | |
# Get all the worksheets in the book. | |
worksheets = workbook.worksheets | |
# Get the first worksheet in the worksheets collection. | |
worksheet = workbook.worksheets[0] | |
# Create a range of cells. | |
sourceRange = worksheet.cells.create_range("A1", "A2") | |
# Input some data with some formattings into | |
# A few cells in the range. | |
sourceRange.get(0, 0).put_value("Test") | |
sourceRange.get(1, 0).put_value("123") | |
# Create target range of cells. | |
targetRange = worksheet.cells.create_range("B1", "B2") | |
# Copy source range to target range in the same workhseet | |
targetRange.copy(sourceRange) | |
# Create target range of cells. | |
workbook.worksheets.add() | |
worksheet = workbook.worksheets[1] | |
targetRange = worksheet.cells.create_range("A1", "A2") | |
# Copy source range to target range in another workhseet | |
targetRange.copy(sourceRange) | |
# Copy to another workbook | |
anotherWorkbook = Workbook() | |
worksheet = workbook.worksheets[0] | |
targetRange = worksheet.cells.create_range("A1", "A2") | |
# Copy source range to target range in another workbook | |
targetRange.copy(sourceRange) |
使用选项粘贴范围
Aspose.Cells for Python via .NET支持粘贴具有特定类型的范围。
This file contains 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 PasteOptions, PasteType, Workbook | |
# Instantiate a new Workbook. | |
workbook = Workbook() | |
# Get all the worksheets in the book. | |
worksheets = workbook.worksheets | |
# Get the first worksheet in the worksheets collection. | |
worksheet = workbook.worksheets[0] | |
# Create a range of cells. | |
sourceRange = worksheet.cells.create_range("A1", "A2") | |
# Input some data with some formattings into | |
# A few cells in the range. | |
sourceRange.get(0, 0).put_value("Test") | |
sourceRange.get(1, 0).put_value("123") | |
# Create target range of cells. | |
targetRange = worksheet.cells.create_range("B1", "B2") | |
# Init paste options. | |
options = PasteOptions() | |
# Set paste type. | |
options.paste_type = PasteType.VALUES_AND_FORMATS | |
options.skip_blanks = True | |
# Copy source range to target range | |
targetRange.copy(sourceRange, options) |
仅复制数据的范围
您还可以使用Range.copy_data方法复制数据,如下面的代码所示:
This file contains 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 | |
# Instantiate a new Workbook. | |
workbook = Workbook() | |
# Get all the worksheets in the book. | |
worksheets = workbook.worksheets | |
# Get the first worksheet in the worksheets collection. | |
worksheet = workbook.worksheets[0] | |
# Create a range of cells. | |
sourceRange = worksheet.cells.create_range("A1", "A2") | |
# Input some data with some formattings into | |
# A few cells in the range. | |
sourceRange.get(0, 0).put_value("Test") | |
sourceRange.get(1, 0).put_value("123") | |
# Create target range of cells. | |
targetRange = worksheet.cells.create_range("B1", "B2") | |
# Copy the data of source range to target range | |
targetRange.copy_data(sourceRange) |