MS Excel is a powerful tool that allows you to perform various spreadsheet manipulation operations. You can organize data, apply computations, generate charts, define your custom logic using VBA modules, and so on. An important feature that MS Excel provides is encryption and decryption of the Excel files. In accordance with that, this article covers how to encrypt/decrypt and password-protect Excel files in Node.js applications.
- Node.js API to Encrypt and Decrypt Excel Files
- Encrypt Excel Files in Node.js
- Decrypt Excel Files in Node.js
- Get Free API License
Node.js API to Encrypt and Decrypt Excel Files
For encryption and decryption of Excel files, we will use Aspose.Cells for Node.js via Java. It is a feature-rich API that allows you to encrypt and decrypt the workbooks within a few lines of code. In addition, you can password-protect an Excel file seamlessly. You can either download the API or install it using the following npm command.
> npm install aspose.cells
Encrypt Excel XLSX or XLS Files in Node.js
Aspose.Cells for Node.js via Java provides the following encryption types for Excel 2003 files.
- COMPATIBLE (Office 97/2000 compatible)
- ENHANCED CRYPTOGRAPHIC PROVIDER V1
- STRONG CRYPTOGRAPHIC PROVIDER
Whereas, SHA and AES encryption techniques are used for 2007/2010 workbooks. The following are the steps to encrypt an Excel file in Node.js.
- Load the Excel file using Workbook class.
- Set password using Workbook.getSettings().setPassword(string) method.
- Encrypt Excel file using Workbook.setEncryptionOptions(EncryptionType, int) method.
- Save the encrypted workbook using Workbook.save(string) method.
The following code sample shows how to encrypt an Excel XLSX file in Node.js.
Decrypt an Excel File in Node.js
To decrypt an encrypted Excel file, you will need to specify its password. The following are the steps to decrypt an Excel file in Node.js.
- Create an instance of LoadOptions class.
- Specify the password using LoadOptions.setPassword(string) method.
- Create an instance of the Workbook class and pass the file’s path and LoadOptions object to its constructor.
- Set password to null using Workbook.getSettings().setPassword(string) method.
- Once done, save the decrypted workbook using Workbook.save(string) method.
The following code sample shows how to decrypt a password-protected Excel file in Node.js.
Get a Free API License
In case you want to try the API without evaluation limitations, you can get a free temporary license.
In this article, you have learned how to encrypt or decrypt Excel files in Node.js applications. You can simply integrate the provided code into your Node.js applications to encrypt/decrypt XLSX/XLS files. In case you want to explore more about the Node.js spreadsheet manipulation API, visit the documentation. Also, you can ask your questions via our forum.