Home > Web Front-end > JS Tutorial > Node.js development: How to implement data import and export functions

Node.js development: How to implement data import and export functions

王林
Release: 2023-11-08 10:35:18
Original
1692 people have browsed it

Node.js development: How to implement data import and export functions

Node.js development: How to implement data import and export functions, specific code examples are required

With the continuous development of Internet technology, data has become our daily work important parts of. In actual work, we often need to import data into the system or export data from the system. How to implement this function? This article will introduce how to use Node.js to implement data import and export functions, and provide specific code examples.

1. The fs module of Node.js

The fs module of Node.js is a file system module that can read and write files. You need to import it before use:

const fs = require('fs')
Copy after login

Before using the fs module, you need to first determine whether the file to be operated exists. You can use the following code to determine:

if (fs.existsSync('filename')) {
  //文件存在,可以进行操作
} else {
  //文件不存在
}
Copy after login

2. Data import

  1. Use Node.js to read Excel files

In actual work, we often need to import data from Excel files into the system. Node.js can use the xlsx module to read data in Excel and convert it into JSON format. First, you need to use npm to install the xlsx module:

npm install xlsx --save
Copy after login
Copy after login

Then, introduce the module:

const xlsx = require('xlsx')
Copy after login
Copy after login

Read the Excel file and convert it into JSON format:

const workbook = xlsx.readFile('filename')
const sheet = workbook.Sheets['Sheet1']
const data = xlsx.utils.sheet_to_json(sheet)
Copy after login

In this way, Data in Excel files can be read and converted into JSON format.

  1. Read CSV files using Node.js

Like Excel files, CSV files are also a common data import format. Node.js can use the fast-csv module to read CSV files. First, you need to use npm to install the fast-csv module:

npm install fast-csv --save
Copy after login
Copy after login

Then, introduce the module:

const csv = require('fast-csv')
Copy after login
Copy after login

Read the CSV file and convert it into JSON format:

const dataArray = []
fs.createReadStream('filename')
  .pipe(csv.parse({ headers: true }))
  .on('data', row => {
    dataArray.push(row)
  })
  .on('end', () => {
    console.log(dataArray)
  })
Copy after login

Like this , you can read and convert the data in the CSV file into JSON format.

3. Data export

  1. Export JSON format data to Excel file

In actual work, we often need to export data in the system In Excel format. Using Node.js, you can use the xlsx module to convert JSON format into Excel format and output it to a file. First, you need to use npm to install the xlsx module:

npm install xlsx --save
Copy after login
Copy after login

Then, introduce the module:

const xlsx = require('xlsx')
Copy after login
Copy after login

Convert JSON format data to Excel and output it to a file:

const worksheet = xlsx.utils.json_to_sheet(data)
const workbook = xlsx.utils.book_new()
xlsx.utils.book_append_sheet(workbook, worksheet, 'Sheet1')
xlsx.writeFile(workbook, 'filename')
Copy after login

In this way, JSON format data can be converted into Excel and output to a file.

  1. Export JSON format data to CSV file

Like Excel files, CSV files are also a common data export format. Node.js can use the fast-csv module to convert JSON format data into CSV format and output it to a file. First, you need to use npm to install the fast-csv module:

npm install fast-csv --save
Copy after login
Copy after login

Then, introduce the module:

const csv = require('fast-csv')
Copy after login
Copy after login

Convert JSON format data to CSV and output it to a file:

csv.writeToPath('filename', data, { headers: true })
  .on('finish', () => {
    console.log('done')
  })
Copy after login

Like this , you can convert JSON format data into CSV and output it to a file.

Summary

This article introduces how to use Node.js to implement data import and export functions, and provides specific code examples. In actual work, this function is very common. Through the introduction of this article, I believe readers can already use Node.js to implement this function.

The above is the detailed content of Node.js development: How to implement data import and export functions. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template