Hướng dẫn upload and read excel file in node js - tải lên và đọc tệp excel trong nút js

Tôi đang sử dụng Multer để tải lên tệp và XLSX để xử lý nó và MongoDB làm cơ sở dữ liệu (Mongoose cho mô hình):

Lead là mô hình dữ liệu, bạn phải thêm tên cột Excel. Xem tài liệu Mongoose để biết thêm thông tin

const express = require("express");
const multer = require("multer");
const connectDB = require("./config/db");
const Lead = require("./models/Lead");

connectDB();

const uploadXLSX = async (req, res, next) => {
  try {
    let path = req.file.path;
    var workbook = XLSX.readFile(path);
    var sheet_name_list = workbook.SheetNames;
    let jsonData = XLSX.utils.sheet_to_json(
      workbook.Sheets[sheet_name_list[0]]
    );
    if (jsonData.length === 0) {
      return res.status(400).json({
        success: false,
        message: "xml sheet has no data",
      });
    }
    let savedData = await Lead.create(jsonData);

    return res.status(201).json({
      success: true,
      message: savedData.length + " rows added to the database",
    });
  } catch (err) {
    return res.status(500).json({ success: false, message: err.message });
  }
};

var storage = multer.diskStorage({
  destination: function (req, file, cb) {
    cb(null, "uploads");
  },
  filename: function (req, file, cb) {
    cb(null, Date.now() + "-" + file.originalname);
  },
});

const upload = multer({ storage: storage });

app.post("/upload", upload.single("xlsx"), uploadXLSX);

const port = process.env.PORT || 5000;

const server = app.listen(port, () => {
  console.log("app running on port", port);
});

Vì vậy, từ đây khi bạn gọi điện đến localhost:5000/upload với người đưa thư xem hình bên dưới

Hướng dẫn upload and read excel file in node js - tải lên và đọc tệp excel trong nút js

Node.js là một môi trường thời gian chạy JavaScript nguồn mở và đa nền tảng cũng có thể được sử dụng để đọc từ một tệp và ghi vào một tệp có thể có trong định dạng TXT, ODS, XLSX, DOCX, v.v.

Ví dụ sau đây bao gồm cách tệp tệp Excel (.xlsx) được đọc từ tệp Excel và sau đó được chuyển đổi thành JSON và cũng để ghi vào nó. Nó có thể đạt được bằng cách sử dụng một gói gọi là XLSX để đạt được mục tiêu của chúng tôi.xlsx to achieve our goal.

Cài đặt mô -đun: Bạn có thể cài đặt mô -đun XLSX bằng lệnh sau:You can install xlsx module using the following command:

npm install xlsx

Lưu ý: Đối với ví dụ sau, Text.xlsx là một tệp dữ liệu giả đã được sử dụng.For the following example, text.xlsx is a dummy data file that has been used.

Tên tệp: test.xlsx & nbsp;

Tờ 1:

Hướng dẫn upload and read excel file in node js - tải lên và đọc tệp excel trong nút js

Tờ 2:

Hướng dẫn upload and read excel file in node js - tải lên và đọc tệp excel trong nút js

Vì vậy, bài kiểm tra tệp Excel.xlsx có 2 tờ, một tờ có chi tiết sinh viên và một trang khác có chi tiết giảng viên.

Đọc Hoạt độngFileName: read.js & nbsp; Filename: read.js 

JavaScript

const reader = require('xlsx')

npm install xlsx
0
npm install xlsx
1)

npm install xlsx
3

npm install xlsx
4

npm install xlsx
5
npm install xlsx
6

npm install xlsx
7

npm install xlsx
8
npm install xlsx
9

const sheets = file.SheetNames  // Here the value of the sheets will be 2
0
const sheets = file.SheetNames  // Here the value of the sheets will be 2
1

npm install xlsx
8
const sheets = file.SheetNames  // Here the value of the sheets will be 2
3

const sheets = file.SheetNames  // Here the value of the sheets will be 2
4
const sheets = file.SheetNames  // Here the value of the sheets will be 2
5

npm install xlsx
8
const sheets = file.SheetNames  // Here the value of the sheets will be 2
7

const sheets = file.SheetNames  // Here the value of the sheets will be 2
8

const sheets = file.SheetNames  // Here the value of the sheets will be 2
9

Giải thích: Đầu tiên, mô -đun NPM được bao gồm trong tệp read.js và sau đó tệp excel được đọc vào một sổ làm việc, tức là tệp hằng số trong chương trình trên.First, the npm module is included in the read.js file and then the excel file is read into a workbook i.e constant file in the above program.

Số lượng tệp trong tệp Excel cụ thể đó có sẵn trong thuộc tính SheetNames của sổ làm việc. Nó có thể được truy cập như sau:

const sheets = file.SheetNames  // Here the value of the sheets will be 2

Một vòng lặp được chạy cho đến khi kết thúc tệp Excel bắt đầu từ trang đầu tiên. Một trong những hàm quan trọng nhất được sử dụng trong mã trên là hàm sheet_to_json () có trong mô -đun UTILS của gói XLSX. Nó chấp nhận một đối tượng bảng tính như một tham số và trả về một mảng các đối tượng JSON.

Có một vòng lặp foreach lặp đi lặp lại thông qua mọi đối tượng JSON có trong temp mảng và đẩy nó vào một dữ liệu biến có chứa tất cả dữ liệu ở định dạng JSON.temp and pushes it into a variable data which would contain all the data in JSON format.

Cuối cùng, dữ liệu được in hoặc bất kỳ sửa đổi nào khác có thể được thực hiện trên mảng các đối tượng JSON.

Bước để chạy ứng dụng:

Chạy tệp read.js bằng lệnh sau:read.js file using the following command:

node read.js

Output:

Hướng dẫn upload and read excel file in node js - tải lên và đọc tệp excel trong nút js

Viết hoạt động trong ví dụ sau, chúng tôi sẽ chuyển đổi một mảng các đối tượng JSON thành một tờ Excel và nối nó vào tệp. In the following example, we will convert an array of JSON objects into an excel sheet and append it to the file.

Tên tệp: Viết.js

JavaScript

const reader = require('xlsx')

npm install xlsx
0
npm install xlsx
1)

node read.js
6

node read.js
7
node read.js
8
node read.js
9
node write.js
0

node read.js
7
node write.js
2

node read.js
7
node write.js
4
node write.js
5
node write.js
0

node read.js
7
node write.js
8

node write.js
9

npm install xlsx
7

node read.js
7
node read.js
8Lead3
node write.js
0

node read.js
7Lead6

node read.js
7
node write.js
4Lead9
node write.js
0

node read.js
7localhost:5000/upload2

npm install xlsx
8
npm install xlsx
9

localhost:5000/upload4

localhost:5000/upload5localhost:5000/upload6)

localhost:5000/upload8

npm install xlsx
1)

Giải thích: Đầu tiên, mô -đun NPM được bao gồm trong tệp read.js và sau đó tệp excel được đọc vào một sổ làm việc, tức là tệp hằng số trong chương trình trên.Here we have an array of JSON objects called student_data. We use two main functions in this program i.e json_to_sheet() which accepts an array of objects and converts them into a worksheet and another function is the book_append_sheet() to append the worksheet into the workbook.

Số lượng tệp trong tệp Excel cụ thể đó có sẵn trong thuộc tính SheetNames của sổ làm việc. Nó có thể được truy cập như sau:

Bước để chạy ứng dụng:

Chạy tệp read.js bằng lệnh sau:read.js file using the following command:

node write.js

Viết hoạt động trong ví dụ sau, chúng tôi sẽ chuyển đổi một mảng các đối tượng JSON thành một tờ Excel và nối nó vào tệp.The final test.xlsx file would look something like this: 

Tờ 1:

Hướng dẫn upload and read excel file in node js - tải lên và đọc tệp excel trong nút js

Tờ 2:

Hướng dẫn upload and read excel file in node js - tải lên và đọc tệp excel trong nút js

Vì vậy, bài kiểm tra tệp Excel.xlsx có 2 tờ, một tờ có chi tiết sinh viên và một trang khác có chi tiết giảng viên.We can see sheet 3 is appended into the test.xlsx as shown below:

Hướng dẫn upload and read excel file in node js - tải lên và đọc tệp excel trong nút js


Làm thế nào để bạn tải một tệp excel lên nút js?

Cách nhập bản ghi tệp Excel trong cơ sở dữ liệu MySQL thông qua Node JS bằng cách sử dụng Multer..
Bước 1: Tạo thư mục dự án ..
Bước 2: Thiết lập gói JSON ..
Bước 2: Cài đặt các gói NPM ..
Bước 3: Tạo bảng MySQL ..
Bước 4: Tạo biểu mẫu tải lên bootstrap ..
Bước 5: Tạo tệp máy chủ ..
Bước 6: Tải lên Excel lên MySQL ..

Làm cách nào để đọc một tệp excel trong nút js?

Đọc tập tin..
Mở cửa sổ thiết bị đầu cuối của bạn và gõ, npm init.Bạn chỉ có thể chấp nhận các mặc định sẽ tạo tệp chính ** của chúng tôi.....
Trong cửa sổ thiết bị đầu cuối của bạn, nhập phần sau để cài đặt gói làm cho tất cả hoạt động.NPM Cài đặt READ-EXCLE-FILE ..
Tạo một tệp có tên chỉ mục.JS và mở nó ..

Làm cách nào để đọc một tệp excel lớn trong nút?

Làm cách nào để đọc một tệp excel lớn trong nút ?..
Bước 1: Thiết lập dự án nút ..
Bước 2: Tạo tệp ứng dụng ..
Bước 3: Cài đặt Gói đọc Excel ..
Bước 4: Đọc tệp excel trong nút ..
Bước 5: Chạy ứng dụng nút ..

Làm thế nào để bạn đọc và viết tệp excel trong javascript?

Cách đọc và viết tệp excel trong nút.JS?.
JavaScript |Chương trình ghi dữ liệu trong một tệp văn bản ..
Node.js fs.readDir () Phương thức ..
Node.js fs.readDirsync () Phương thức ..
Node.js fs.readFilesYnc () Phương thức ..
Node.js |Phương thức fs.writeFilesync () ..
Node.js fs.writefile () Phương thức ..
Node.js fs.readFile () Phương thức ..