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 Show 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 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 Javascriptconst 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 Javascriptconst 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 const sheets = file.SheetNames // Here the value of the sheets will be 209 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 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 |