Copy and Move Worksheets Within and Between Workbooks with C++
Sometimes, you need multiple worksheets with common formatting and data entry. For example, if you work with quarterly budgets, you might want to create a workbook with sheets that contain the same column headings, row headings, and formulas. There is a way to do this: by creating one sheet and then copying it multiple times.
Aspose.Cells supports copying or moving worksheets within or between workbooks. Worksheets including data, formatting, tables, matrices, charts, images and other objects are copied with the highest degree of precision.
Copying and Moving Worksheets
Copying a Worksheet within a Workbook
The initial steps are the same for all examples:
-
Create two workbooks with some data in Microsoft Excel. For the purposes of this example, we created two new workbooks in Microsoft Excel and input some data into the worksheets:
- FirstWorkbook.xlsx (3 worksheets)
- SecondWorkbook.xlsx (1 worksheet)
-
Download and install Aspose.Cells:
- Download Aspose.Cells for C++
- Install it on your development computer
-
Create a project:
- Create a new C++ project in your preferred IDE
-
Add references:
- Add Aspose.Cells for C++ library to your project
-
Copy the worksheet within a workbook The first example copies the first worksheet (Copy) within FirstWorkbook.xlsx.
When executing the code, the worksheet named Copy is copied within FirstWorkbook.xlsx with the name Last Sheet.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source and output directory paths
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create workbook from input file
Workbook excelWorkbook1(srcDir + u"FirstWorkbook.xlsx");
// Get worksheet collection reference
WorksheetCollection worksheets = excelWorkbook1.GetWorksheets();
// Copy third worksheet (index 2) within the workbook
worksheets.AddCopy(worksheets.Get(2).GetName());
// Save modified workbook
excelWorkbook1.Save(outDir + u"FirstWorkbookCopied_out.xlsx");
std::cout << "Worksheet copied successfully." << std::endl;
Aspose::Cells::Cleanup();
}
Moving a Worksheet within a Workbook
The code below shows how to move a worksheet from one position in a workbook to another. Executing the code moves the worksheet called Move from index 1 to index 2 in FirstWorkbook.xlsx.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source and output directory paths
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Create input and output file paths
U16String inputFilePath = srcDir + u"FirstWorkbook.xlsx";
U16String outputFilePath = outDir + u"FirstWorkbookMoved_out.xlsx";
// Load source workbook
Workbook excelWorkbook2(inputFilePath);
// Access worksheet collection and move target sheet
WorksheetCollection sheets = excelWorkbook2.GetWorksheets();
sheets.Get(u"Move").MoveTo(2);
// Save modified workbook
excelWorkbook2.Save(outputFilePath);
std::cout << "Worksheet moved successfully." << std::endl;
Aspose::Cells::Cleanup();
}
Copying a Worksheet between Workbooks
Executing the code copies the worksheet named Copy to SecondWorkbook.xlsx with the name Sheet2.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source and output directories
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Open source workbooks
Workbook excelWorkbook3(srcDir + u"FirstWorkbook.xlsx");
Workbook excelWorkbook4(srcDir + u"SecondWorkbook.xlsx");
// Access worksheets collection from second workbook
WorksheetCollection sheets4 = excelWorkbook4.GetWorksheets();
// Add new worksheet to destination workbook
sheets4.Add();
// Copy specified worksheet from source to destination
Worksheet sourceSheet = excelWorkbook3.GetWorksheets().Get(u"Copy");
sheets4.Get(1).Copy(sourceSheet);
// Save modified workbook
excelWorkbook4.Save(outDir + u"CopyWorksheetsBetweenWorkbooks_out.xlsx");
std::cout << "Worksheets copied successfully between workbooks." << std::endl;
Aspose::Cells::Cleanup();
}
Moving a Worksheet between Workbooks
Executing the code moves the worksheet named Move from FirstWorkbook.xlsx to SecondWorkbook.xlsx with the name Sheet3.
#include <iostream>
#include "Aspose.Cells.h"
using namespace Aspose::Cells;
int main()
{
Aspose::Cells::Startup();
// Source and output directories
U16String srcDir(u"..\\Data\\01_SourceDirectory\\");
U16String outDir(u"..\\Data\\02_OutputDirectory\\");
// Open first workbook
Workbook excelWorkbook5(srcDir + u"FirstWorkbook.xlsx");
// Open second workbook and add new worksheet
Workbook excelWorkbook6(srcDir + u"SecondWorkbook.xlsx");
excelWorkbook6.GetWorksheets().Add();
// Copy third worksheet from first workbook to third position in second workbook
WorksheetCollection sheets5 = excelWorkbook5.GetWorksheets();
WorksheetCollection sheets6 = excelWorkbook6.GetWorksheets();
sheets6.Get(2).Copy(sheets5.Get(2));
// Remove copied worksheet from source workbook
sheets5.RemoveAt(2);
// Save modified workbooks
excelWorkbook5.Save(outDir + u"FirstWorkbookWithMove_out.xlsx");
excelWorkbook6.Save(outDir + u"SecondWorkbookWithMove_out.xlsx");
std::cout << "Worksheets moved successfully between workbooks." << std::endl;
Aspose::Cells::Cleanup();
}