Copiare e Spostare Fogli di Lavoro

Come spostare o copiare fogli usando Microsoft Excel

Di seguito sono riportati i passaggi coinvolti nella copia e nel trasferimento dei fogli di lavoro all’interno o tra i fogli di lavoro in Microsoft Excel.

  1. Per spostare o copiare i fogli in un altro libro, aprire il libro che riceverà i fogli.
  2. Passare al libro che contiene i fogli da spostare o copiare, e quindi selezionare i fogli.
  3. Nel menu Modifica, fare clic su Sposta o Copia Foglio.
  4. Nella finestra di dialogo Al libro, fare clic sul workbook per ricevere i fogli.
  5. Per spostare o copiare i fogli selezionati in un nuovo workbook, fare clic su Nuovo libro.
  6. Nella casella Prima del foglio, fare clic sul foglio prima del quale si desidera inserire i fogli spostati o copiati.
  7. Per copiare i fogli anziché spostarli, selezionare la casella Crea copia.

Come copiare i fogli all’interno di un foglio di lavoro con la libreria Excel Aspose.Cells per Python

Aspose.Cells per Python via .NET fornisce un metodo sovraccaricato, Aspose.Cells.WorksheetCollection.add_copy(), che viene utilizzato per aggiungere un foglio al set e copiare dati da un foglio esistente. Una versione del metodo prende l’indice del foglio di lavoro di origine come parametro. L’altra versione prende il nome del foglio di lavoro di origine.

Nell’esempio seguente viene mostrato come copiare un foglio di lavoro esistente all’interno di un libro.

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")

Come copiare i fogli tra i fogli di lavoro

Aspose.Cells per Python via .NET fornisce un metodo, Aspose.Cells.Worksheet.copy() utilizzato per copiare dati e formattazione da un foglio di lavoro di origine a un altro foglio di lavoro all’interno o tra fogli di lavoro. Il metodo prende l’oggetto del foglio di lavoro di origine come parametro.

L’esempio seguente mostra come copiare un foglio di lavoro da un libro di lavoro a un altro libro di lavoro.

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")

L’esempio seguente mostra come copiare un foglio di lavoro da un libro di lavoro a un altro.

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")

Come spostare i fogli di lavoro all’interno del foglio di lavoro

Aspose.Cells per Python via .NET fornisce un metodo Aspose.Cells.Worksheet.move_to() che viene utilizzato per spostare un foglio di lavoro in un’altra posizione nello stesso foglio di calcolo. Il metodo prende l’indice del foglio di lavoro di destinazione come parametro.

L’esempio seguente mostra come spostare un foglio di lavoro in un’altra posizione all’interno del libro di lavoro.

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")