Quick Start Guide | |
---|---|
Tutorial Concept |
Learn how to integrate a secure client-side Excel .XLSX viewer using a JavaScript spreadsheet component. |
What You Will Need |
NPM Packages: |
Controls Referenced |
Many businesses rely on spreadsheets like Microsoft Excel to manage operations, making it essential to handle these files in modern JavaScript applications. SpreadJS, a JavaScript spreadsheet component, lets you load, view, edit, and save complex XLSX files in the browser. In this blog, we'll show you how to build an XLSX Viewer using SpreadJS, including importing Excel files and protecting sheets to control user edits.
Add Spreadsheets to Your JavaScript Enterprise Web Apps. Download SpreadJS Now!
How to Add an Excel XLSX Viewer to JavaScript Web Applications
- Create a JavaScript Spreadsheet Application
- Add Excel Import Code - Import Method
- Password Protect the JS Excel Viewer - Protect/Unprotect Methods
Download a Sample JavaScript Application to Follow Along with the Tutorial.
Create a JavaScript Spreadsheet Application
Create a simple HTML file, including a DOM element for the JavaScript spreadsheet viewer and a side panel with options to choose from, and input an existing XLSX file. Then, set a password and protect or unprotect the worksheet.
Install the necessary JavaScript Spreadsheet Component NPM packages.
Alternatively, download and install the associated SpreadJS release script files.
Be sure to add a reference to these packages in the HTML file.
Lastly, initialize the JavaScript XLSX viewer using the SpreadJS APIs Workbook constructor and passing the DOM element previously created by its ID.
The JavaScript application will now show the spreadsheet instance. Next, let's add some functionality to the side panel.
See our Quick Start Demo or Documentation for additional information on getting started with this JavaScript spreadsheet control.
Add Excel Import Code to a JavaScript Application
Depending on the application, you may want to allow end users to import an existing .xlsx file into the JavaScript Excel viewer application. To accomplish this, we will implement event listeners to capture a selected .xlsx file, and then invoke the JS spreadsheet API's import method on button click.
Notice the XLSX file can now be imported into the JS Excel Viewer application:
See our Online Import/Export Demo Explorer or Documentation to learn more about the import and exporting capabilities of SpreadJS.
Password Protect the JavaScript Excel Viewer
Once the end user imports the file, they may want to protect the workbook using a password to prevent specific users from making changes. Using the JS spreadsheet API, developers can add this capability by invoking the Worksheet's protect and unprotect methods.
In this setProtectSheet() function, get the active worksheet, check to see if it is already protected, and if not, then invoke the protect method with a password if one is applied.
In this setUnprotectSheet() function, allow authorized users to unprotect a worksheet to make changes by providing a password and invoking the unprotect method.
Notice that users can now protect and unprotect the JavaScript spreadsheet using a set password.
Check out the online Protect Worksheet Demo or the Documentation and learn more about the protection and cell locking/unlocking capabilities.
Looking for an Enterprise JavaScript Spreadsheet? Try SpreadJS Today!
JavaScript Spreadsheet Component
This blog tutorial only scratches the surface of the full capabilities of the JavaScript spreadsheet component, SpreadJS. Check out our online demo explorer, documentation, blogs, and videos to see all the features and use cases SpreadJS can fulfill.
Looking for a different framework or platform? Explore the rest of our blog series to learn how to build Excel .xlsx Viewer apps across various environments: