Copia degli intervalli di Excel

Introduzione

In Excel, è possibile selezionare un intervallo, copiare l’intervallo, quindi incollarlo con opzioni specifiche nello stesso foglio di lavoro, in altri fogli di lavoro o in altri file.

Copia intervalli utilizzando la libreria Aspose.Cells per Python Excel

Aspose.Cells for Python via .NET fornisce alcuni metodi di sovraccarico Range.copy per copiare l’intervallo. E Range.copy_style solo lo stile di copia dell’intervallo; Range.copy_data solo il valore di copia dell’intervallo

Copia Intervallo

Creazione di due intervalli: intervallo di origine, intervallo di destinazione, quindi copia dell’intervallo di origine all’intervallo di destinazione con il metodo Range.copy.

Vedere il codice seguente:

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)

Incolla l’intervallo con opzioni

Aspose.Cells for Python via .NET supporta l’incollaggio dell’intervallo con un tipo specifico.

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)

Copia solo i dati dell’intervallo

È anche possibile copiare i dati con il metodo Range.copy_data come nei seguenti codici:

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)

Argomenti avanzati