Copying and Moving Worksheets

Moving or Copying Sheets using Microsoft Excel

The following are the steps involved in copying and moving worksheets within or between workbooks in Microsoft Excel.

  1. To move or copy sheets to another workbook, open the workbook that will receive the sheets.
  2. Switch to the workbook that contains the sheets you want to move or copy, and then select the sheets.
  3. On the Edit menu, click Move or Copy Sheet.
  4. In the To book dialog, click the workbook to receive the sheets.
  5. To move or copy the selected sheets to a new workbook, click New Book.
  6. In the Before sheet box, click the sheet before which you want to insert the moved or copied sheets.
  7. To copy the sheets instead of moving them, select the Create a copy checkbox.

Copy Worksheets within a Workbook with Aspose.Cells

Aspose.Cells provides an overloaded method AddCopy() that is used to add a worksheet to the collection and copy data from an existing worksheet. One version of the method takes the index of the source worksheet as a parameter. The other version takes the name of the source worksheet. The following example shows how to copy an existing worksheet within a 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();

Move Worksheets within Workbook

Aspose.Cells provides a method MoveTo() that is used to move a worksheet to another location in the same spreadsheet. The method takes the target worksheet index as a parameter. The following example shows how to move a worksheet to another location within the 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();