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

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

王林
王林Original
2023-11-08 10:35:181590browse

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')

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 {
  //文件不存在
}

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

Then, introduce the module:

const xlsx = require('xlsx')

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)

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

Then, introduce the module:

const csv = require('fast-csv')

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)
  })

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

Then, introduce the module:

const xlsx = require('xlsx')

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')

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

Then, introduce the module:

const csv = require('fast-csv')

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

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

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!

Statement:
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