Xlsx writefile nodejs

SheetJS trình bày một giao diện JS đơn giản hoạt động với "Mảng của các mảng" và "Mảng của các đối tượng JS". Các hàm API là các khối xây dựng nên được kết hợp với các API JS khác để giải quyết vấn đề

Cuộc thảo luận tập trung vào tư duy giải quyết vấn đề. Chi tiết API được đề cập trong các phần khác của tài liệu

Mục tiêu của ví dụ này là tạo sổ làm việc XLSX có tên và ngày sinh của Tổng thống Hoa Kỳ. Nhấn vào đây để chuyển sang bản demo trực tiếp

Thu thập dữ liệu

Dữ liệu thô

Dữ liệu thô có sẵn ở dạng JSON. Để thuận tiện, nó đã được nhân đôi ở đây

Thu thập dữ liệu đơn giản với

{
"id": { /* (data omitted) */ },
"name": {
"first": "John", // <-- first name
"last": "Adams" // <-- last name
},
"bio": {
"birthday": "1735-10-19", // <-- birthday
"gender": "M"
},
"terms": [
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "prez", /* (other fields omitted) */ }
]
}
9

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();

Dữ liệu thô là một mảng các đối tượng. Đây là dữ liệu của John Adams

{
"id": { /* (data omitted) */ },
"name": {
"first": "John", // <-- first name
"last": "Adams" // <-- last name
},
"bio": {
"birthday": "1735-10-19", // <-- birthday
"gender": "M"
},
"terms": [
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "prez", /* (other fields omitted) */ }
]
}

Lọc cho Chủ tịch

Bộ dữ liệu bao gồm Aaron Burr, một Phó Tổng thống chưa bao giờ là Tổng thống

const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
0 tạo một mảng mới với các hàng mong muốn. Tổng thống đã phục vụ ít nhất một nhiệm kỳ với
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
1 được đặt thành
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
2. Để kiểm tra xem một hàng cụ thể có ít nhất một thuật ngữ
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
2 hay không,
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
4 là một hàm JS gốc khác. Bộ lọc hoàn chỉnh sẽ là

const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));

Định hình lại mảng​

Đối với ví dụ này, tên sẽ là tên kết hợp với họ (

const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
5) và ngày sinh sẽ có tại
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
6. Sử dụng
const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
7, bộ dữ liệu có thể được xoa bóp trong một cuộc gọi

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
2

Kết quả là một mảng các đối tượng "đơn giản" không lồng vào nhau

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
3

Tạo sổ làm việc​

Với tập dữ liệu đã được làm sạch,

const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
8 tạo một trang tính

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
5

const prez = raw_data.filter(row => row.terms.some(term => term.type === "prez"));
9 tạo một sổ làm việc mới và
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
20 nối thêm một trang tính vào sổ làm việc. Bảng tính mới sẽ được gọi là "Ngày"

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
8

Dọn dẹp Sổ làm việc​

Dữ liệu nằm trong sổ làm việc và có thể được xuất

Xlsx writefile nodejs

Có nhiều cơ hội để cải thiện. các tiêu đề có thể được đổi tên và độ rộng cột có thể được điều chỉnh. SheetJS Pro cung cấp các tùy chọn kiểu dáng bổ sung như kiểu dáng ô và hàng cố định

Thay đổi tên tiêu đề (bấm để hiển thị)

Theo mặc định,

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
21 tạo trang tính có hàng tiêu đề. Trong trường hợp này, các tiêu đề đến từ các khóa đối tượng JS. "tên" và "sinh nhật"

Các tiêu đề nằm trong các ô

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
22 và
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
23.
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
24 có thể ghi các giá trị văn bản vào trang tính hiện có bắt đầu từ ô
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
22

{
"id": { /* (data omitted) */ },
"name": {
"first": "John", // <-- first name
"last": "Adams" // <-- last name
},
"bio": {
"birthday": "1735-10-19", // <-- birthday
"gender": "M"
},
"terms": [
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "prez", /* (other fields omitted) */ }
]
}
4

Thay đổi độ rộng cột (bấm để hiển thị)

Một số tên dài hơn chiều rộng cột mặc định. Độ rộng cột được đặt bằng cách đặt thuộc tính trang tính

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
26

Dòng sau đặt chiều rộng của cột A thành khoảng 10 ký tự

{
"id": { /* (data omitted) */ },
"name": {
"first": "John", // <-- first name
"last": "Adams" // <-- last name
},
"bio": {
"birthday": "1735-10-19", // <-- birthday
"gender": "M"
},
"terms": [
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "prez", /* (other fields omitted) */ }
]
}
6

Một cuộc gọi

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
27 qua
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
28 có thể tính chiều rộng tối đa

{
"id": { /* (data omitted) */ },
"name": {
"first": "John", // <-- first name
"last": "Adams" // <-- last name
},
"bio": {
"birthday": "1735-10-19", // <-- birthday
"gender": "M"
},
"terms": [
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "prez", /* (other fields omitted) */ }
]
}
9

Xuất một tập tin​

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
29 tạo một tệp bảng tính và cố gắng ghi nó vào hệ thống. Trong trình duyệt, nó sẽ cố gắng nhắc người dùng tải xuống tệp. Trong NodeJS, nó sẽ ghi vào thư mục cục bộ

Ghi chú

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
30 chỉ ghi các tệp XLSX và được khuyến nghị khi xuất sẽ luôn ở định dạng
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
31.
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
32 dễ bị rung cây hơn. Ví dụ này sử dụng
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
29 vì
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
32 không hỗ trợ các định dạng xuất phổ biến khác như
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
35 hoặc
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
36 hoặc
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
37

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
38 cho phép nén ZIP cho XLSX và các định dạng khác

{
"id": { /* (data omitted) */ },
"name": {
"first": "John", // <-- first name
"last": "Adams" // <-- last name
},
"bio": {
"birthday": "1735-10-19", // <-- birthday
"gender": "M"
},
"terms": [
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "prez", /* (other fields omitted) */ }
]
}
0

Xlsx writefile nodejs

Bản trình diễn trực tiếp

Bản demo này chạy trong trình duyệt web. Nhấp vào "Nhấp để tạo tệp. " và trình duyệt sẽ tạo tệp XLSX

Kết quả

Đang tải

Trình chỉnh sửa trực tiếp

{
"id": { /* (data omitted) */ },
"name": {
"first": "John", // <-- first name
"last": "Adams" // <-- last name
},
"bio": {
"birthday": "1735-10-19", // <-- birthday
"gender": "M"
},
"terms": [
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "prez", /* (other fields omitted) */ }
]
}
1

Chạy bản demo cục bộ

  • trình duyệt
  • NodeJS
  • Deno
  • bún

Lưu tập lệnh sau vào

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
39 và mở trang. Trang phải được lưu trữ (không có quyền truy cập
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
50)

https. //sheetjs. com/pres. html là phiên bản được lưu trữ của trang

{
"id": { /* (data omitted) */ },
"name": {
"first": "John", // <-- first name
"last": "Adams" // <-- last name
},
"bio": {
"birthday": "1735-10-19", // <-- birthday
"gender": "M"
},
"terms": [
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "prez", /* (other fields omitted) */ }
]
}
2

Cài đặt các phụ thuộc

{
"id": { /* (data omitted) */ },
"name": {
"first": "John", // <-- first name
"last": "Adams" // <-- last name
},
"bio": {
"birthday": "1735-10-19", // <-- birthday
"gender": "M"
},
"terms": [
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "prez", /* (other fields omitted) */ }
]
}
3

Lưu tập lệnh sau vào

const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
51 và chạy
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
52

đoạn trích. js

{
"id": { /* (data omitted) */ },
"name": {
"first": "John", // <-- first name
"last": "Adams" // <-- last name
},
"bio": {
"birthday": "1735-10-19", // <-- birthday
"gender": "M"
},
"terms": [
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "prez", /* (other fields omitted) */ }
]
}
4

Hỗ trợ

{
"id": { /* (data omitted) */ },
"name": {
"first": "John", // <-- first name
"last": "Adams" // <-- last name
},
"bio": {
"birthday": "1735-10-19", // <-- birthday
"gender": "M"
},
"terms": [
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "viceprez", /* (other fields omitted) */ },
{ "type": "prez", /* (other fields omitted) */ }
]
}
9 gốc đã được thêm vào trong NodeJS 18. Đối với các phiên bản NodeJS cũ hơn, tập lệnh sẽ báo lỗi
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
54. Thư viện của bên thứ ba như
const url = "https://sheetjs.com/data/executive.json";
const raw_data = await (await fetch(url)).json();
55 trình bày một API tương tự để tìm nạp dữ liệu