Copy Excel Worksheets with JavaScript in React
Copying worksheets is one of the most common and efficient operations in everyday Excel document processing — whether you are quickly creating similar reports from a template or consolidating data across multiple documents. Spire.XLS for JavaScript handles this entirely in the browser via WebAssembly, using a virtual file system (VFS) to manage input and output files — no backend server required.
This article covers three core features:
- Copy a worksheet within the same workbook
- Copy a worksheet across workbooks
- Copy a selected cell range
For installation and project setup, refer to Integrating Spire.XLS for JavaScript in a React Project. The examples below assume Spire.XLS is installed and the WebAssembly module is initialized.
Copy a Worksheet Within the Same Workbook
Duplicating a worksheet within the same workbook is a frequent development task — for example, quickly creating next month's report copy from a monthly template. Spire.XLS for JavaScript provides the CopyFrom method to duplicate a worksheet. The copied sheet retains all content from the source worksheet, including data, styles, fonts, colors, borders, column widths, and row heights.
function App() {
const sheetToSVG = async () => {
// Get the Spire.XLS WASM module
const xlsModule = window.wasmModule?.spirexls;
// Check if the module is ready
if (!xlsModule) {
alert('Spire.Xls is not ready yet');
return;
}
// Load fonts and the Excel file into VFS
await window.spire.FetchFileToVFS('ARIAL.TTF', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
const inputFileName = 'Sample.xlsx';
await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}data/`);
// Load the workbook
const workbook = new xlsModule.Workbook();
workbook.LoadFromFile({ fileName: inputFileName });
// Get the first worksheet
let sheet = workbook.Worksheets.get(0);
// Add a new worksheet
let sheet1 = workbook.Worksheets.Add("MySheet");
// Copy the first worksheet into the newly added sheet
sheet1.CopyFrom(sheet);
const outputFileName = "CopySheetWithinWorkbook_output.xlsx";
workbook.SaveToFile({ fileName: outputFileName });
// Release the workbook object to free resources
workbook.Dispose();
// Read the converted file from VFS and trigger download
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>Copy Worksheet Within Workbook</h1>
<button onClick={sheetToSVG}>
Start
</button>
</div>
);
}
export default App;
When using CopyFrom, data, styles, fonts, colors, borders, and column widths from the source worksheet are fully preserved in the new sheet.

Copy a Worksheet Across Workbooks
In real-world scenarios, data from multiple Excel files often needs to be consolidated into a single workbook — for example, extracting specific sheets from departmental reports and merging them into a master sheet. The AddCopy method lets you copy a worksheet from the source workbook into the target workbook with all its content intact.
function App() {
const sheetToSVG = async () => {
// Get the Spire.XLS WASM module
const xlsModule = window.wasmModule?.spirexls;
// Check if the module is ready
if (!xlsModule) {
alert('Spire.Xls is not ready yet');
return;
}
// Load fonts and the Excel files into VFS
await window.spire.FetchFileToVFS('arial.ttf', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
const sourceFileName = 'ReadImages.xlsx';
const targetFileName = 'Sample.xlsx';
await window.spire.FetchFileToVFS(sourceFileName, '', `${process.env.PUBLIC_URL}data/`);
await window.spire.FetchFileToVFS(targetFileName, '', `${process.env.PUBLIC_URL}data/`);
// Load the source workbook
const sourceWorkbook = new xlsModule.Workbook();
sourceWorkbook.LoadFromFile({ fileName: sourceFileName });
// Get the first worksheet of the source workbook
const srcWorksheet = sourceWorkbook.Worksheets.get(0);
// Load the target workbook
const targetWorkbook = new xlsModule.Workbook();
targetWorkbook.LoadFromFile({ fileName: targetFileName });
// Add a new worksheet in the target workbook and copy the source sheet into it
targetWorkbook.Worksheets.AddCopy({ sheet: srcWorksheet });
// Save the target workbook
const outputFileName = "CopyAcrossWorkbooks_output.xlsx";
targetWorkbook.SaveToFile({ fileName: outputFileName });
// Release resources
sourceWorkbook.Dispose();
targetWorkbook.Dispose();
// Read the converted file from VFS and trigger download
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>Copy Worksheet Across Workbooks</h1>
<button onClick={sheetToSVG}>
Start
</button>
</div>
);
}
export default App;
When copying across workbooks, all data and styles from the source worksheet are preserved — AddCopy copies the complete worksheet content into the target workbook.

Copy a Selected Cell Range
Sometimes you do not need to copy an entire worksheet — you only need to copy a specific cell range (such as a particular data table or summary result) to a target location. Spire.XLS for JavaScript provides the Copy method, which copies data, styles, and formatting from the source range to the starting position of the target range.
function App() {
const sheetToSVG = async () => {
// Get the Spire.XLS WASM module
const xlsModule = window.wasmModule?.spirexls;
// Check if the module is ready
if (!xlsModule) {
alert('Spire.Xls is not ready yet');
return;
}
// Load the source Excel file into VFS
await window.spire.FetchFileToVFS('ARIAL.TTF', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
const inputFileName = 'Sample.xlsx';
await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}data/`);
// Load the workbook
const workbook = new xlsModule.Workbook();
workbook.LoadFromFile({ fileName: inputFileName });
// Get the first row of the first worksheet as the source range
const sheet = workbook.Worksheets.get(0);
const sourceRange = sheet.Range.get("A1:E1");
// Add a new worksheet
let sheet1 = workbook.Worksheets.Add("AddSheet");
// Copy the source range to the starting position of the target worksheet
sheet.Copy(sourceRange, sheet1, sheet.FirstRow, sheet.FirstColumn, true);
// Save the workbook
const outputFileName = "CopyRange_output22.xlsx";
workbook.SaveToFile({ fileName: outputFileName });
// Release resources
workbook.Dispose();
// Read the converted file from VFS and trigger download
const fileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([fileArray], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>Copy Range</h1>
<button onClick={sheetToSVG}>
Start
</button>
</div>
);
}
export default App;
The Copy method transfers data, styles, and formatting from the source range to the target range — ideal for lightweight scenarios where only partial data extraction is needed.

FAQ
Column width differs after copying
Cause: Font mismatch between the source and target workbooks.
Solution: Ensure all required font files are loaded into the VFS environment of the target workbook before copying across workbooks:
await window.spire.FetchFileToVFS(
'arial.ttf', '/Library/Fonts/', '/'
);
Range content is pasted at the wrong position
Cause: Incorrect destRow and destColumn parameters in the Copy method, causing data to be pasted at an unexpected location.
Solution: Confirm that the destination row and column indices start from 1 (not 0), and verify the row and column range of the target worksheet before copying.
Get a Free License
Spire.XLS for JavaScript offers a 30-day full-featured free trial license with no functional limitations. Apply here to evaluate before purchasing.
Add, Remove, and Move Excel Worksheets with JavaScript in React
Managing worksheets — adding, removing, and reordering them — is one of the most fundamental and frequently used operations in Excel document processing. Spire.XLS for JavaScript handles these operations entirely in the browser via WebAssembly, using a virtual file system (VFS) to manage input and output files — no backend server required.
This article covers three core features:
For installation and project setup, refer to Integrating Spire.XLS for JavaScript in a React Project. The examples below assume Spire.XLS is installed and the WebAssembly module is initialized.
Add Worksheet
Adding new worksheets to a workbook is a common requirement in daily development. Spire.XLS for JavaScript provides the Add method to create a new worksheet and give it a name. After adding, you can write data to the new sheet's cells and save the workbook.
function App() {
const startProcessing = async () => {
// Get the Spire.XLS WASM module
const xlsModule = window.wasmModule?.spirexls;
if (!xlsModule) {
alert('Spire.Xls is not ready yet');
return;
}
// Load fonts and the Excel file into VFS
await window.spire.FetchFileToVFS('ARIAL.TTF', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
const inputFileName = 'AddWorksheet.xlsx';
await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}/data/`);
// Create a workbook instance and load the file
const workbook = new xlsModule.Workbook();
workbook.LoadFromFile({ fileName: inputFileName });
// Add a new worksheet named "NewSheet"
const sheet = workbook.Worksheets.Add("NewSheet");
sheet.Range.get("C5").Text = "This is an inserted sheet.";
// Auto-fit columns
sheet.AllocatedRange.AutoFitColumns();
// Save the workbook
const outputFileName = "AddWorksheet_output.xlsx";
workbook.SaveToFile({ fileName: outputFileName });
// Release resources
workbook.Dispose();
// Read the output file from VFS, wrap it as a Blob, and trigger download
const modifiedFileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([modifiedFileArray], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>Add Worksheet</h1>
<button onClick={startProcessing}>
Start
</button>
</div>
);
}
export default App;
Adding a new worksheet after the existing ones via the Add method.

Remove Worksheet
When you need to clean up unwanted worksheets from a workbook, you can remove them directly by name. Spire.XLS for JavaScript's Remove method precisely locates and removes the target worksheet.
function App() {
const startProcessing = async () => {
// Get the Spire.XLS WASM module
const xlsModule = window.wasmModule?.spirexls;
if (!xlsModule) {
alert('Spire.Xls is not ready yet');
return;
}
// Load fonts and the Excel file into VFS
await window.spire.FetchFileToVFS('ARIAL.TTF', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
const inputFileName = 'RemoveWorksheet.xlsx';
await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}/data/`);
// Create a workbook instance and load the file
const workbook = new xlsModule.Workbook();
workbook.LoadFromFile({ fileName: inputFileName });
// Remove a worksheet by name
const sheet = workbook.Worksheets.get("Sheet2");
workbook.Worksheets.Remove(sheet);
// Remove by index
//workbook.Worksheets.RemoveAt(1);
// Save the workbook
const outputFileName = "RemoveWorksheet_output.xlsx";
workbook.SaveToFile({ fileName: outputFileName });
// Release resources
workbook.Dispose();
// Read the output file from VFS, wrap it as a Blob, and trigger download
const modifiedFileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([modifiedFileArray], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>Remove Worksheet</h1>
<button onClick={startProcessing}>
Start
</button>
</div>
);
}
export default App;
Using Remove to delete a worksheet by name.

Move and Reorder Worksheets
Reordering worksheets is a common task when organizing an Excel document. With Spire.XLS for JavaScript's MoveWorksheet method, you can move a worksheet to a target index position, effectively reordering the sheets within the workbook.
function App() {
const startProcessing = async () => {
// Get the Spire.XLS WASM module
const xlsModule = window.wasmModule?.spirexls;
if (!xlsModule) {
alert('Spire.Xls is not ready yet');
return;
}
// Load fonts and the Excel file into VFS
await window.spire.FetchFileToVFS('ARIAL.TTF', '/Library/Fonts/', `${process.env.PUBLIC_URL}/font/`);
const inputFileName = 'Sample.xlsx';
await window.spire.FetchFileToVFS(inputFileName, '', `${process.env.PUBLIC_URL}/data/`);
// Create a workbook instance and load the file
const workbook = new xlsModule.Workbook();
workbook.LoadFromFile({ fileName: inputFileName });
// Get the first worksheet and move it to index 1
const sheet = workbook.Worksheets.get(0);
sheet.MoveWorksheet(1);
// Save the workbook
const outputFileName = "MoveWorksheet_output.xlsx";
workbook.SaveToFile({ fileName: outputFileName });
// Release resources
workbook.Dispose();
// Read the output file from VFS, wrap it as a Blob, and trigger download
const modifiedFileArray = window.dotnetRuntime.Module.FS.readFile(outputFileName);
const blob = new Blob([modifiedFileArray], { type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" });
const url = URL.createObjectURL(blob);
const a = document.createElement('a');
a.href = url;
a.download = outputFileName;
a.click();
URL.revokeObjectURL(url);
};
return (
<div style={{ textAlign: 'center', height: '300px' }}>
<h1>Move Worksheet</h1>
<button onClick={startProcessing}>
Start
</button>
</div>
);
}
export default App;
Moving the first worksheet to the second sheet position via the MoveWorksheet method.

FAQ
Index out of range when operating on worksheets
Cause: The index parameter is outside the range of the current worksheet collection in the workbook.
Solution: Verify the total number of worksheets before performing the operation, ensuring the index is within 0 to worksheets.Count-1. Use workbook.Worksheets.Count to get the current total:
const count = workbook.Worksheets.Count;
Worksheet not found when removing by name
Cause: The specified worksheet name does not exactly match the actual name in the workbook.
Solution: Iterate through the worksheet names to confirm before removal:
for (let i = 0; i < workbook.Worksheets.Count; i++) {
let name = workbook.Worksheets.get(i).Name;
console.log(name);
}
Get a Free License
Spire.XLS for JavaScript offers a 30-day full-featured free trial license with no functional limitations. Apply here to evaluate before purchasing.