Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
Sometimes, you do need a number of 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 three times.
Aspose.Cells for Node.js via C++ 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.
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 entered some data into the worksheets.
Download and install Aspose.Cells:
Create a project:
Add references:
...\Program Files\Aspose\Aspose.Cells\Bin\NodeJs\Aspose.Cells.dll.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.”
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Open a file into the first workbook.
const excelWorkbook1 = new AsposeCells.Workbook(path.join(dataDir, "FirstWorkbook.xlsx"));
// Copy the first sheet of the first workbook within the workbook
excelWorkbook1.getWorksheets().get(2).copy(excelWorkbook1.getWorksheets().get("Copy"));
// Save the file.
excelWorkbook1.save(path.join(dataDir, "FirstWorkbookCopied_out.xlsx"));
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.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const filePath = path.join(dataDir, "FirstWorkbook.xlsx");
// Open a file into the first workbook.
const excelWorkbook2 = new AsposeCells.Workbook(filePath);
// Move the sheet
const worksheets = excelWorkbook2.getWorksheets();
const worksheet = worksheets.get(0);
worksheet.moveTo(1);
// Save the file.
excelWorkbook2.save(path.join(dataDir, "FirstWorkbookMoved_out.xlsx"));
Executing the code copies the worksheet named Copy into SecondWorkbook.xlsx with the name Sheet2.
const AsposeCells = require("aspose.cells.node");
const path = require("path");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
const excelWorkbook3 = new AsposeCells.Workbook();
const excelWorkbook4 = new AsposeCells.Workbook();
// Create source worksheet
excelWorkbook3.getWorksheets().add("Copy");
// Add a new worksheet into the second workbook
excelWorkbook4.getWorksheets().add();
// Copy the first sheet of the first workbook into the second workbook.
excelWorkbook4.getWorksheets().get(1).copy(excelWorkbook3.getWorksheets().get("Copy"));
// Save the file.
excelWorkbook4.save(path.join(dataDir, "CopyWorksheetsBetweenWorkbooks_out.xlsx"));
Executing the code moves the worksheet named Move from FirstWorkbook.xlsx to SecondWorkbook.xlsx with the name Sheet3.
const path = require("path");
const AsposeCells = require("aspose.cells.node");
// The path to the documents directory.
const dataDir = path.join(__dirname, "data");
// Create new workbooks instead of opening existing files
const excelWorkbook5 = new AsposeCells.Workbook();
const excelWorkbook6 = new AsposeCells.Workbook();
// Add a new worksheet
excelWorkbook6.getWorksheets().add();
// Copy the sheet from the first workbook into the second workbook.
excelWorkbook6.getWorksheets().get(0).copy(excelWorkbook5.getWorksheets().get(0));
// Remove the copied worksheet from the first workbook
excelWorkbook5.getWorksheets().removeAt(0);
// Save the files.
excelWorkbook5.save(path.join(dataDir, "FirstWorkbookWithMove_out.xlsx"));
excelWorkbook6.save(path.join(dataDir, "SecondWorkbookWithMove_out.xlsx"));
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.