Copiare e Spostare Fogli di Lavoro

Spostare o Copiare Fogli Usando Microsoft Excel

Di seguito sono elencati i passaggi coinvolti nella copia e nello spostamento dei fogli di lavoro all’interno o tra i workbook 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.

Copiare Fogli di Lavoro all’interno di un Workbook con Aspose.Cells

Aspose.Cells fornisce un metodo sovraccaricato AddCopy() che viene utilizzato per aggiungere un foglio di lavoro alla raccolta e copiare i dati da un foglio di lavoro 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. L’esempio seguente mostra come copiare un foglio di lavoro esistente all’interno di un workbook.

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
//Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
//Path of input excel file
U16String sampleCopyingAndMovingWorksheets = srcDir + u"sampleCopyingAndMovingWorksheets.xlsx";
//Path of output excel file
U16String outputCopyingAndMovingWorksheets = outDir + u"outputCopyingAndMovingWorksheets.xlsx";
//Create workbook
Workbook workbook(sampleCopyingAndMovingWorksheets);
//Create worksheets object with reference to the sheets of the workbook.
WorksheetCollection sheets = workbook.GetWorksheets();
//Copy data to a new sheet from an existing sheet within the workbook.
sheets.AddCopy(u"Sheet1");
//Save the Excel file.
workbook.Save(outputCopyingAndMovingWorksheets);
std::cout << "Worksheet copied successfully with in a workbook!" << std::endl;
Aspose::Cells::Cleanup();

Sposta i fogli di lavoro all’interno del libro di lavoro

Aspose.Cells fornisce un metodo MoveTo() che viene utilizzato per spostare un foglio di lavoro in un’altra posizione nella stessa cartella di lavoro. 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 workbook.

Aspose::Cells::Startup();
//For complete examples and data files, please go to https://github.com/aspose-cells/Aspose.Cells-for-C
//Source directory path
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
//Output directory path
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
//Path of input excel file
U16String sampleCopyingAndMovingWorksheets = srcDir + u"sampleCopyingAndMovingWorksheets.xlsx";
//Path of output excel file
U16String outputCopyingAndMovingWorksheets = outDir + u"outputCopyingAndMovingWorksheets.xlsx";
//Create workbook
Workbook workbook(sampleCopyingAndMovingWorksheets);
//Create worksheets object with reference to the sheets of the workbook.
WorksheetCollection sheets = workbook.GetWorksheets();
//Access the first sheet
Worksheet sheet = sheets.Get(0);
//Move the first sheet to the third position in the workbook.
sheet.MoveTo(2);
//Save the Excel file.
workbook.Save(outputCopyingAndMovingWorksheets);
std::cout << "Worksheet moved successfully with in a workbook!" << std::endl;
Aspose::Cells::Cleanup();