Nodejs đọc tệp excel thành JSON

Xin chào tất cả, Trong khi ứng dụng web, chúng tôi có thể cần chuyển đổi dữ liệu từ định dạng này sang định dạng khác. Nên hôm nay chúng ta sẽ chuyển dữ liệu từ file excel sang file json. Cuối cùng, chúng tôi sẽ có thể thay đổi dữ liệu bảng excel phù hợp thành định dạng json

Vì vậy, hãy cố gắng hoàn thành từng bước,

1. tạo ứng dụng nodejs với express

2. Tải lên trang tính Excel

3. Đọc tệp excel đã tải lên và chuyển đổi nó thành json bằng gói nodejs

Vì vậy, hãy xem phần nodejs của chúng tôi (máy chủ. js)

hãy thể hiện = yêu cầu ('bày tỏ'),

bodyParser = require('body-parser'),

bộ trộn = yêu cầu ('bộ trộn'),

tiền điện tử = yêu cầu ('tiền điện tử'),

xlsxtojson = yêu cầu('xlsx-to-json'),

xlstojson = yêu cầu("xls-to-json");

let fileExtension = require('file-extension');

ứng dụng. sử dụng (bodyParser. json());

để lưu trữ = multer. diskStorage({ // nhiều cài đặt lưu trữ đĩa

điểm đến. hàm (yêu cầu, tệp, cb) {

tên tập tin. hàm (yêu cầu, tệp, cb) {

tiền điện tử. pseudoRandomBytes(16, function (err, raw) {

cb(null, thô. toString('hex') + Ngày. bây giờ() + '. ' + phần mở rộng tệp (tệp. mimetype));

hãy tải lên = multer({lưu trữ. kho}). duy nhất ('tệp');

/** Phương thức xử lý biểu mẫu gửi */

ứng dụng. post('/sendFile', function(req, res) {

tải lên (req, res, function (err) {

độ phân giải. json({mã_lỗi. 401,err_desc. sai});

độ phân giải. json({mã_lỗi. 404,err_desc. "Không tìm thấy tập tin. "});

nếu (yêu cầu. tập tin. tên khai sinh. tách ra('. ')[yêu cầu. tập tin. tên khai sinh. tách ra('. '). độ dài-1] === 'xlsx'){

//  mã để chuyển đổi dữ liệu excel sang định dạng json

đầu ra. "đầu ra/"+Ngày. bây giờ()+". json", // xuất json

// tải tệp chỉ mục để tải tệp lên trên http. //máy chủ cục bộ. 3000/

ứng dụng. get('/',function(req,res){

độ phân giải. sendFile(__dirname + "/index. html");

ứng dụng. lắng nghe ('3000', chức năng () {

bảng điều khiển. log('Máy chủ chạy trên cổng 3000');

Trong tệp trên, chúng tôi đã tạo một ứng dụng nodejs và sau đó trên http. //máy chủ cục bộ. 3000, phục vụ một chỉ mục. trang html để tải lên tệp excel. Và sau khi tải lên tệp excel của chúng tôi, gói excel-to-json của nodejs thực hiện nhiệm vụ chính là chuyển đổi dữ liệu từ định dạng excel sang json

Bây giờ chúng ta hãy xem phần xem của bản trình diễn của chúng tôi (chỉ mục. html).  

   

Excel sang Json trong nodejs. thế giới json

   

   

   

   

 

   

Excel sang Json trong nodejs

   

nguồn. thế giới json

 

       

           

           

           

 

Trong tệp trên, chúng tôi chỉ có một biểu mẫu có tệp loại đầu vào để tải tệp lên và nút gửi

Tuyệt đấy. Cuối cùng, nhiệm vụ của chúng tôi hoàn thành ở đây

Đó là tất cả cho bây giờ. Cảm ơn bạn đã đọc và tôi hy vọng bài đăng này sẽ rất hữu ích

Hãy cho tôi biết suy nghĩ của bạn về bản demo email. jsonworld @ gmail. com. Tôi rất thích nghe chúng và Nếu bạn thích bài viết này, hãy chia sẻ với bạn bè của bạn

Nút. js là một môi trường thời gian chạy JavaScript đa nền tảng và mã nguồn mở cũng có thể được sử dụng để đọc từ một tệp và ghi vào một tệp có thể ở định dạng txt, ods, xlsx, docx, v.v.

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

Cài đặt mô-đun. Bạn có thể cài đặt mô-đun xlsx bằng lệnh sau

npm install xlsx

Ghi chú. Ví dụ sau, văn bản. xlsx là một tệp dữ liệu giả đã được sử dụng

tên tệp. kiểm tra. xlsx

tờ 1

Nodejs đọc tệp excel thành JSON

tờ 2

Nodejs đọc tệp excel thành JSON

Vậy file excel test. xlsx có 2 sheet, một sheet có thông tin Sinh viên và một sheet có thông tin giảng viên

Đọc tên tệp hoạt động. đọc. js

Javascript




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
const sheets = file.SheetNames  // Here the value of the sheets will be 2
0____31

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

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
0____11
const sheets = file.SheetNames  // Here the value of the sheets will be 2
1

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

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
2

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

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

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

node read.js
0

node read.js
1
node read.js
2

node read.js
3____74

node read.js
1
node read.js
6

node read.js
7____78

node read.js
1
const sheets = file.SheetNames  // Here the value of the sheets will be 2
70

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

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

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

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

Giải trình. Đầu tiên, mô-đun npm được bao gồm trong phần đọc. js và sau đó tệp excel được đọc vào sổ làm việc tôi. tệp hằng số e trong chương trình trên

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 for đượ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 đoạn 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 trang tính làm 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 qua mọi đối tượng JSON có trong mảng tạm thời và đẩy nó vào một biến data chứa tất cả dữ liệu ở định dạng JSON

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 đọc. js bằng lệnh sau

node read.js

đầu ra

Nodejs đọc tệp excel thành JSON

Thao tác ghi Trong ví dụ sau, chúng ta sẽ chuyển đổi một mảng các đối tượng JSON thành một trang tính excel và nối nó vào tệp

tên tệp. viết. js

Javascript




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

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

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

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
0____11
const sheets = file.SheetNames  // Here the value of the sheets will be 2
1

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

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

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

const sheets = file.SheetNames  // Here the value of the sheets will be 2
87
const sheets = file.SheetNames  // Here the value of the sheets will be 2
88
const sheets = file.SheetNames  // Here the value of the sheets will be 2
89
const sheets = file.SheetNames  // Here the value of the sheets will be 2
90

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

const sheets = file.SheetNames  // Here the value of the sheets will be 2
87
const sheets = file.SheetNames  // Here the value of the sheets will be 2
94
const sheets = file.SheetNames  // Here the value of the sheets will be 2
95
const sheets = file.SheetNames  // Here the value of the sheets will be 2
90

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

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

node read.js
0

const sheets = file.SheetNames  // Here the value of the sheets will be 2
87
const sheets = file.SheetNames  // Here the value of the sheets will be 2
88
const sheets = file.SheetNames  // Here the value of the sheets will be 2
03
const sheets = file.SheetNames  // Here the value of the sheets will be 2
90

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

const sheets = file.SheetNames  // Here the value of the sheets will be 2
87
const sheets = file.SheetNames  // Here the value of the sheets will be 2
94
const sheets = file.SheetNames  // Here the value of the sheets will be 2
09
const sheets = file.SheetNames  // Here the value of the sheets will be 2
90

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

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

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

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

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

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

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

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

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

Giải trình. Ở đây chúng ta có một mảng các đối tượng JSON được gọi là student_data. Chúng tôi sử dụng hai chức năng chính trong chương trình này i. e json_to_sheet() chấp nhận một mảng đối tượng và chuyển đổi chúng thành một trang tính và một chức năng khác là book_append_sheet() để nối thêm trang tính vào sổ làm việc

Cuối cùng, tất cả các thay đổi được ghi vào test. xlsx sử dụng hàm writeFile() lấy sổ làm việc và tệp excel làm tham số đầu vào

Làm cách nào để chuyển đổi dữ liệu excel sang JSON trong NodeJs?

Chuyển đổi dữ liệu từ trang tính excel trong tệp xlsx sang đối tượng JSON hoặc JavaScript trong NodeJs. .
npm cài đặt xlsx -to- json
Gói này giúp chuyển đổi dữ liệu của bảng excel sang JSON/đối tượng thực sự dễ dàng
Tất cả những gì bạn cần là một tệp excel đầu vào và tệp js nút
Tệp JavaScript
Một ví dụ mẫu để hiểu rõ hơn

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

Trong Phiên bản Excel Mới hơn . Hộp thoại Nhập dữ liệu xuất hiện. Tìm kiếm tệp JSON, rồi chọn Mở. Select Data > Get Data > From File > From JSON. The Import Data dialog box appears. Search the JSON file, and then select Open.

Làm cách nào để chuyển đổi tệp excel thành tệp JSON?

Cách chuyển đổi EXCEL sang JSON .
Tải lên tệp EXCEL của bạn để chuyển đổi
Nhấn nút "CHUYỂN ĐỔI"
Tải xuống các tệp JSON đã chuyển đổi ngay lập tức hoặc gửi liên kết tải xuống tới email

Làm cách nào để chuyển đổi dữ liệu excel sang JSON trong js?

Làm theo các bước đơn giản dưới đây. .
Define HTML. We have defined an HTML tag with type=”file” to choose an excel file. .. .
Include the library in HTML file. Include the CDN link of library xlsx.min.js in the tag of an HTML file as follows: .. .
Viết logic Javascript