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.
