Excel manipulation is a crucial aspect of many modern web applications, from data analysis to report generation. Node.js, with its non-blocking I/O model and rich ecosystem, is an excellent choice for backend development. When combined with Spire.XLS for JavaScript, you can unlock a world of possibilities for handling Excel files efficiently.
This guide will walk you through the process of integrating Spire.XLS for JavaScript into your Node.js projects, covering everything from initial setup to generating a simple Excel document.
- Benefits of Using Spire.XLS for JavaScript in Node.js Projects
- Set Up Your Environment
- Integrate Spire.XLS for JavaScript in Your Project
- Create and Save Excel Files Using JavaScript
Benefits of Using Spire.XLS for JavaScript in Node.js Projects
Node.js is a great option for developing scalable network applications. When paired with Spire.XLS for JavaScript, you get a robust solution for handling Excel files. Here are some key features and benefits of using Spire.XLS for JavaScript in Node.js projects:
- Comprehensive Excel Support: Spire.XLS for JavaScript supports a wide range of Excel features, including formulas, charts, pivot tables, and more.
- High Performance: The library is optimized for speed, ensuring that your application remains responsive even when handling large datasets.
- Cross-Platform Compatibility: With Node.js, you can run your application on any platform that supports JavaScript, including Windows, macOS, and Linux.
- Secure and Reliable: The library is designed to be secure, with features like data validation and encryption to protect your data.
By leveraging the strengths of both Node.js and Spire.XLS for JavaScript, you can build powerful applications that handle Excel files with ease.
Set Up Your Environment
Step 1
Download and install Node.js from the official website. Make sure to choose the version that matches your operating system.
After the installation is complete, you can verify that Node.js and npm are installed correctly, along with the version numbers, by entering the following commands in CMD:
node -v npm -v
Step 2
Create a Node.js project in your IntelliJ IDEA.
Install Jest in your project to write and run tests for your code, by running the following command in Terminal:
npm install --save-dev jest
Create a JavaScript file named "jest.config.js" in your project, and include the following configuration in it.
module.exports = { testTimeout: 20000, testEnvironment: 'node', transform: {}, testMatch: ['<rootDir>/*.js'], moduleFileExtensions: [ 'json', 'node', 'tsx', 'ts', 'js', 'jsx','mjs'], };
Add a "fonts" folder and a "lib" folder to your project.
Integrate Spire.XLS for JavaScript in Your Project
Download Spire.XLS for JavaScript and unzip it to a location on your disk. Inside the lib folder, you will find the Spire.Xls.Base.js and Spire.Xls.Base.wasm files.
Copy these two files into the "lib" folder in your Node.js project.
Place the font files you plan to use into the "fonts" folder in your project.
Create and Save Excel Files Using JavaScript
Add a JavaScript file in your project to generate a simple Excel document from JavaScript code.
Here is the entire JavaScript code:
- JavaScript
// Import the library const { Module, spirexls } = require("./lib/Spire.Xls.Base.js"); // Define a test case test('testCase', async () => { await new Promise((resolve) => { Module.onRuntimeInitialized = () => { createExcel(); resolve(); }; }); }); // Create a custom function function createExcel (){ // Load fonts spirexls.copyLocalPathToVFS("fonts/","/Library/Fonts/"); // Specify output file name and path const outFileName = "HelloWorld.xlsx"; const outputPath= "result/" + outFileName; // Create a workbook let workbook = Module.spirexls.Workbook.Create(); // Add a sheet let sheet = workbook.Worksheets.Add("MySheet"); // Write data to a specific cell sheet.Range.get("A1").Text = "Hello World"; // Auto-fit column width sheet.Range.get("A1").AutoFitColumns(); // Save the workbook to a file workbook.SaveToFile({fileName:outFileName, version:spirexls.ExcelVersion.Version2016}); spirexls.copyFileFromFSToLocalStorage(outFileName, outputPath); // Dispose resources workbook.Dispose(); }
Once you run the code, you will find the generated Excel file in the designated file path.
Apply for a Temporary License
If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.