Mở tệp excel trong trình duyệt bằng javascript

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

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____309
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 để mở tệp Excel trong trình duyệt bằng JavaScript?

Giải pháp 1. var newwindow=cửa sổ. mở ["url của. xls","window1",""];

Làm cách nào để mở tệp XLSX trong trình duyệt?

Chỉ cần cài đặt phần mềm XLS Editor dành cho Google Chrome của chúng tôi ™ Để bắt đầu xem và chỉnh sửa các tệp XLS/XLSX của bạn, chỉ cần làm như sau Cài đặt .

Bạn có thể xem tệp Excel của mình trong phiên bản Excel trực tuyến thông qua trình duyệt Web không?

Chỉnh sửa bảng tính trong trình duyệt . Excel dành cho web sẽ mở ở dạng xem Chỉnh sửa . Cập nhật dữ liệu, thêm công thức, chèn biểu đồ hoặc áp dụng định dạng. Excel cho web tự động lưu các thay đổi của bạn.

Làm cách nào để mở tệp Excel khi nhấp vào nút trong JavaScript?

Một trong những cách là đính kèm trình xử lý title vào nút. Và về phía máy khách sử dụng JavaScript, sử dụng cửa sổ. mở phương thức và truyền URL tệp cho nó . Nếu đây không phải là điều bạn muốn, bạn có thể cần bổ sung thêm chi tiết cho câu hỏi của mình bằng cách chọn liên kết 'cải thiện câu hỏi'.

Chủ Đề