Creating an Excel Viewer in JavaScript can be a daunting task, but with the SpreadJS JavaScript spreadsheet, the process for creating it is much simpler. In this tutorial blog, we will show you how you can use the power of SpreadJS to create a Viewer that allows you to open and save Excel files in the web browser, as well as protect sheets from being edited and add a password. To follow along with this blog, be sure to download the sample.
Getting Started with SpreadJS
This project will be made with three files: an HTML, a JavaScript file, and a CSS file. We can start by incorporating SpreadJS into our project. You can do this in a few different ways:
Reference Local Files
SpreadJS can be downloaded from our website and imported into an application: GrapeCity, Inc. Once downloaded, we can extract that ZIP file and copy the JS and CSS files to our application, specifically these files:
- gc.spread.sheets.all.xx.x.x.min.js
- gc.spread.sheets.io.xx.x.x.min.js
- gc.spread.sheets.excel2013white.xx.x.x.css
Once we put them in our application’s folder, we can reference them in the code:
Reference NPM
The other way to reference SpreadJS is via the NPM files. These can be added to the application with the following command:
Then, we can reference the files in our code:
Create HTML content
Once those files are referenced, we can assemble the HTML page and the CSS styles. For the styles, I have created the styles ahead of time:
We can then add all of the buttons and UI we’ll need for this application, which includes:
- SpreadJS Instance
- Status Bar
- Import Section
- Password Textbox
- File Select Button
- Import Button
- Export Section
- Password Textbox
- Export Button
We can use the appropriate styles for each element when we add them in the HTML body section:
Initialize SpreadJS
Now that we have our files referenced and the HTML content set up, we can initialize the SpreadJS instance and prepare it for adding Excel import code in the app.js file. We can put this inside of the window’s onload function:
Add Buttons and Functionality
For the purposes of this application, we can also make things easier to write by creating some variables we can utilize for the UI we created before the window.onload function:
Now, we can create variables to reference the different HTML elements inside the window.onload function more easily:
We can now add event listeners and functions for the file select button and password textbox, as well as a handler for an incorrect password message:
Importing an Excel File into SpreadJS
Now, we can add the code to import an Excel file into our SpreadJS instance. Since we are potentially importing password-protected files, we need to account for that when calling the SpreadJS import function. We can also add the event handler once we write the function:
Exporting an Excel File from SpreadJS
Similar to importing, we can support the user entering a password to add to an Excel file when we export, so we just need to pass in the password in the SpreadJS export function. We’ll add an event handler for this as well:
Protecting the Data
We can also protect the data to prevent users from changing it. To accomplish this, we can add a button that will protect the current sheet of the workbook. This can be changed to fit any sort of requirement but will be the active sheet for the purposes of this sample. Similar to the other buttons, we need to add a handler for clicking on it, but with SpreadJS, we can also specify the protection options:
Running the Application
All that is left now is to run the application. Since we have made this with PureJS and HTML, we can simply open the HTML file in a web browser:
We can click on the “Select” button to select an Excel file to load, and then we can click on the “Import” button to import it into SpreadJS:
Now we can add a password by typing it into the Password text box under export and click on the “Export” button:
You have now made your very own Excel Viewer with SpreadJS! With this, you can open, protect, and add a password to Excel files before exporting them, all in a few easy steps.
To try this out for yourself, be sure to download a trial of SpreadJS today: Download JavaScript Spreadsheet | SpreadJS
For more information, check out our demos and documentation.