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

Tôi có nhiệm vụ phân tích một tập dữ liệu khổng lồ gồm các tệp nhật ký. Khi bạn mở tệp trong Excel, nó sẽ đóng băng máy tính xách tay của bạn. Do giới hạn của các công cụ có sẵn, tôi cố gắng phân tích cú pháp tệp bằng nút. tập lệnh js

Vấn đề. Để đọc một tệp nhỏ, bạn có thể sử dụng tập lệnh bên dưới

    var fs = require['fs'];

    fs.readFile['path/mySmallFile.txt', 'utf-8', [err, data] => {
      if [err] {
        throw err;
      }
      console.log[data];
    }]

Sau đó, bạn sẽ có thể đọc nội dung tệp nhỏ này. Tuy nhiên, khi kích thước tệp lớn, bạn sẽ gặp lỗi với bộ đệm. Chẳng hạn như RangeError. Cố gắng phân bổ Bộ đệm lớn hơn kích thước tối đa. Quá trình thực thi sẽ dừng lại khi có lỗi;

    Error: "toString" failed
      at stringSlice [buffer.js]
      at Buffer.toString[buffer.js]
      at FSReqWrap.readFileAfterClose [as oncomplete]

Dung dịch. Để đọc tệp lớn, bạn có thể nhập thư viện gốc cho readline

    var fs = require['fs'];
    var readline = require['readline'];

    const rl = readline.createInterface[{
      input: fs.createReadStream['path/largeFile.csv'],
      output: process.stdout,
      terminal:false
    }]

    rl.on['line', [line] => {
      console.log[line];
    }]

    rl.on['pause', [] => {
      console.log['Done!'];
    }]

Thay thế đường dẫn tệp bằng đường dẫn của bạn đến tệp lớn để xử lý. Bạn có thể xử lý từng dòng tệp bên trong hàm on[‘line’], chẳng hạn như phân tích cú pháp thành json và tăng bộ đếm. Tổng cuối cùng có thể được hiển thị tại chức năng bật ['tạm dừng'] sau khi đọc xong tệp

Bây giờ bạn sẽ có thể xử lý tập dữ liệu lớn với nodejs. Để biết thêm thông tin, vui lòng đọc tài liệu chính thức. https. //nodejs. org/api/readline. html

Có kinh nghiệm phát triển phần mềm, kiến ​​trúc ứng dụng và triển khai các giải pháp đám mây cho khách hàng doanh nghiệp. Kỹ năng thực hành vững chắc với bằng Thạc sĩ Khoa học Máy tính và sự nhạy bén trong kinh doanh với bằng thạc sĩ quản trị kinh doanh [MBA] về Tài chính. Được chứng nhận về Amazon Web Services [AWS], Google Cloud Platform [GCP], Microsoft Azure, Kubernetes [CKA, CKAD, CKS, KCNA] và Scrum [PSM, PSPO] với kinh nghiệm xây dựng các sản phẩm ngân hàng từ đầu. Kết nối trên Linkedin

Nút. js là môi trường thời gian chạy JavaScript mã nguồn mở và đa nền tảng, cũng có thể được sử dụng để đọc từ tệp và ghi vào 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




npm install xlsx
1

npm install xlsx
2______33
npm install xlsx
4

npm install xlsx
5

npm install xlsx
6

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
4

npm install xlsx
5

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

npm install xlsx
5

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

npm install xlsx
5

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

    Error: "toString" failed
      at stringSlice [buffer.js]
      at Buffer.toString[buffer.js]
      at FSReqWrap.readFileAfterClose [as oncomplete]
00

    Error: "toString" failed
      at stringSlice [buffer.js]
      at Buffer.toString[buffer.js]
      at FSReqWrap.readFileAfterClose [as oncomplete]
01
    Error: "toString" failed
      at stringSlice [buffer.js]
      at Buffer.toString[buffer.js]
      at FSReqWrap.readFileAfterClose [as oncomplete]
02

    Error: "toString" failed
      at stringSlice [buffer.js]
      at Buffer.toString[buffer.js]
      at FSReqWrap.readFileAfterClose [as oncomplete]
03
    Error: "toString" failed
      at stringSlice [buffer.js]
      at Buffer.toString[buffer.js]
      at FSReqWrap.readFileAfterClose [as oncomplete]
04

    Error: "toString" failed
      at stringSlice [buffer.js]
      at Buffer.toString[buffer.js]
      at FSReqWrap.readFileAfterClose [as oncomplete]
01
    Error: "toString" failed
      at stringSlice [buffer.js]
      at Buffer.toString[buffer.js]
      at FSReqWrap.readFileAfterClose [as oncomplete]
06

    Error: "toString" failed
      at stringSlice [buffer.js]
      at Buffer.toString[buffer.js]
      at FSReqWrap.readFileAfterClose [as oncomplete]
07
    Error: "toString" failed
      at stringSlice [buffer.js]
      at Buffer.toString[buffer.js]
      at FSReqWrap.readFileAfterClose [as oncomplete]
08

    Error: "toString" failed
      at stringSlice [buffer.js]
      at Buffer.toString[buffer.js]
      at FSReqWrap.readFileAfterClose [as oncomplete]
01
    var fs = require['fs'];
    var readline = require['readline'];

    const rl = readline.createInterface[{
      input: fs.createReadStream['path/largeFile.csv'],
      output: process.stdout,
      terminal:false
    }]

    rl.on['line', [line] => {
      console.log[line];
    }]

    rl.on['pause', [] => {
      console.log['Done!'];
    }]
00

    var fs = require['fs'];
    var readline = require['readline'];

    const rl = readline.createInterface[{
      input: fs.createReadStream['path/largeFile.csv'],
      output: process.stdout,
      terminal:false
    }]

    rl.on['line', [line] => {
      console.log[line];
    }]

    rl.on['pause', [] => {
      console.log['Done!'];
    }]
01

npm install xlsx
5

    var fs = require['fs'];
    var readline = require['readline'];

    const rl = readline.createInterface[{
      input: fs.createReadStream['path/largeFile.csv'],
      output: process.stdout,
      terminal:false
    }]

    rl.on['line', [line] => {
      console.log[line];
    }]

    rl.on['pause', [] => {
      console.log['Done!'];
    }]
03

    var fs = require['fs'];
    var readline = require['readline'];

    const rl = readline.createInterface[{
      input: fs.createReadStream['path/largeFile.csv'],
      output: process.stdout,
      terminal:false
    }]

    rl.on['line', [line] => {
      console.log[line];
    }]

    rl.on['pause', [] => {
      console.log['Done!'];
    }]
04

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

    Error: "toString" failed
      at stringSlice [buffer.js]
      at Buffer.toString[buffer.js]
      at FSReqWrap.readFileAfterClose [as oncomplete]
0

đầ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




    var fs = require['fs'];
    var readline = require['readline'];

    const rl = readline.createInterface[{
      input: fs.createReadStream['path/largeFile.csv'],
      output: process.stdout,
      terminal:false
    }]

    rl.on['line', [line] => {
      console.log[line];
    }]

    rl.on['pause', [] => {
      console.log['Done!'];
    }]
05

npm install xlsx
2______33
npm install xlsx
4

npm install xlsx
5

npm install xlsx
6

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
4

npm install xlsx
5

npm install xlsx
15

npm install xlsx
16

npm install xlsx
17
npm install xlsx
18
npm install xlsx
19
npm install xlsx
20

npm install xlsx
17
npm install xlsx
22

npm install xlsx
17
npm install xlsx
24
npm install xlsx
25
npm install xlsx
20

npm install xlsx
17
npm install xlsx
28

npm install xlsx
29

    Error: "toString" failed
      at stringSlice [buffer.js]
      at Buffer.toString[buffer.js]
      at FSReqWrap.readFileAfterClose [as oncomplete]
00

npm install xlsx
17
npm install xlsx
18
npm install xlsx
33
npm install xlsx
20

npm install xlsx
17
npm install xlsx
36

npm install xlsx
17
npm install xlsx
24
npm install xlsx
39
npm install xlsx
20

npm install xlsx
17
npm install xlsx
42

npm install xlsx
43

npm install xlsx
5

npm install xlsx
45

npm install xlsx
5

npm install xlsx
47
npm install xlsx
48
npm install xlsx
4

npm install xlsx
5

npm install xlsx
51

_______352____41____34

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 hàm 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 để mở tệp 300 MB trong Excel?

Mở sổ làm việc trống trong Excel. Chuyển đến tab Dữ liệu > Từ Văn bản/CSV > tìm tệp và chọn Nhập. .
Các ngôn ngữ lập trình như R và Python có thể thực hiện công việc cho bạn
Bạn có thể sử dụng một trình soạn thảo văn bản
Bạn có thể đặt nó trong cơ sở dữ liệu
Bạn có thể tải xuống một trình chia tệp
Tableau cũng có thể thực hiện công việc

Làm cách nào để mở tệp 700 MB trong Excel?

Chủ đề. Mở một tệp Excel lớn [700 MB, có được không?] .
Sử dụng Get & Transform để kết nối với sổ làm việc và truy vấn dữ liệu. .
Sử dụng mã VBA ADODB để truy vấn sổ làm việc. .
Sao chép sổ làm việc, sau đó thay đổi phần mở rộng thành "zip", sau đó trích xuất tệp xml của trang tính thích hợp và đọc dữ liệu từ đó

Kích thước bao nhiêu là quá lớn đối với một tệp Excel?

tổng giới hạn kích thước tệp là 250 megabyte [MB] .

Chủ Đề