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
tờ 2
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 28
const sheets = file.SheetNames // Here the value of the sheets will be 29
const sheets = file.SheetNames // Here the value of the sheets will be 20____31
const sheets = file.SheetNames // Here the value of the sheets will be 22
const sheets = file.SheetNames // Here the value of the sheets will be 23
const sheets = file.SheetNames // Here the value of the sheets will be 20____11
const sheets = file.SheetNames // Here the value of the sheets will be 21
const sheets = file.SheetNames // Here the value of the sheets will be 22
const sheets = file.SheetNames // Here the value of the sheets will be 24
const sheets = file.SheetNames // Here the value of the sheets will be 22
const sheets = file.SheetNames // Here the value of the sheets will be 26
const sheets = file.SheetNames // Here the value of the sheets will be 22
const sheets = file.SheetNames // Here the value of the sheets will be 28
const sheets = file.SheetNames // Here the value of the sheets will be 29
node read.js0
node read.js1
node read.js2
node read.js3____74
node read.js1
node read.js6
node read.js7____78
node read.js1
const sheets = file.SheetNames // Here the value of the sheets will be 270
const sheets = file.SheetNames // Here the value of the sheets will be 271
const sheets = file.SheetNames // Here the value of the sheets will be 22
const sheets = file.SheetNames // Here the value of the sheets will be 273
const sheets = file.SheetNames // Here the value of the sheets will be 274
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
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 275
const sheets = file.SheetNames // Here the value of the sheets will be 29
const sheets = file.SheetNames // Here the value of the sheets will be 20____31
const sheets = file.SheetNames // Here the value of the sheets will be 22
const sheets = file.SheetNames // Here the value of the sheets will be 23
const sheets = file.SheetNames // Here the value of the sheets will be 20____11
const sheets = file.SheetNames // Here the value of the sheets will be 21
const sheets = file.SheetNames // Here the value of the sheets will be 22
const sheets = file.SheetNames // Here the value of the sheets will be 285
const sheets = file.SheetNames // Here the value of the sheets will be 286
const sheets = file.SheetNames // Here the value of the sheets will be 287
const sheets = file.SheetNames // Here the value of the sheets will be 288
const sheets = file.SheetNames // Here the value of the sheets will be 289
const sheets = file.SheetNames // Here the value of the sheets will be 290
const sheets = file.SheetNames // Here the value of the sheets will be 287
const sheets = file.SheetNames // Here the value of the sheets will be 292
const sheets = file.SheetNames // Here the value of the sheets will be 287
const sheets = file.SheetNames // Here the value of the sheets will be 294
const sheets = file.SheetNames // Here the value of the sheets will be 295
const sheets = file.SheetNames // Here the value of the sheets will be 290
const sheets = file.SheetNames // Here the value of the sheets will be 287
const sheets = file.SheetNames // Here the value of the sheets will be 298
const sheets = file.SheetNames // Here the value of the sheets will be 299
node read.js0
const sheets = file.SheetNames // Here the value of the sheets will be 287
const sheets = file.SheetNames // Here the value of the sheets will be 288
const sheets = file.SheetNames // Here the value of the sheets will be 203
const sheets = file.SheetNames // Here the value of the sheets will be 290
const sheets = file.SheetNames // Here the value of the sheets will be 287
const sheets = file.SheetNames // Here the value of the sheets will be 206
const sheets = file.SheetNames // Here the value of the sheets will be 287
const sheets = file.SheetNames // Here the value of the sheets will be 294____309
const sheets = file.SheetNames // Here the value of the sheets will be 290
const sheets = file.SheetNames // Here the value of the sheets will be 287
const sheets = file.SheetNames // Here the value of the sheets will be 212
const sheets = file.SheetNames // Here the value of the sheets will be 213
const sheets = file.SheetNames // Here the value of the sheets will be 22
const sheets = file.SheetNames // Here the value of the sheets will be 215
const sheets = file.SheetNames // Here the value of the sheets will be 22
const sheets = file.SheetNames // Here the value of the sheets will be 217
const sheets = file.SheetNames // Here the value of the sheets will be 218
const sheets = file.SheetNames // Here the value of the sheets will be 21
const sheets = file.SheetNames // Here the value of the sheets will be 22
const sheets = file.SheetNames // Here the value of the sheets will be 221
const sheets = file.SheetNames // Here the value of the sheets will be 222
const sheets = file.SheetNames // Here the value of the sheets will be 21
const sheets = file.SheetNames // Here the value of the sheets will be 21
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