- 安装依赖
npm install xlsx
- Example
import * as XLSX from "xlsx";
export const Sheet1 = "Sheet1";
export const Sheet2 = "Sheet2";
document.querySelector("#download")?.addEventListener("click", () => {
// 创建一个新的工作簿
const workbook = XLSX.utils.book_new();
// 标准工作簿属性
workbook.Props = {
Title: "AA",
Subject: "js to excel",
Author: "Ajanuw",
CreatedDate: new Date(),
};
// 创建工作表名
workbook.SheetNames.push(Sheet1);
// 将JS数据数组的数组转换为工作表。
workbook.Sheets[Sheet1] = XLSX.utils.aoa_to_sheet(
// js 表数据
[
["a1", "b1"],
["a2", "b2"],
]
);
// 创建工作表名
workbook.SheetNames.push(Sheet2);
// 将JS数据数组的数组转换为工作表。
workbook.Sheets[Sheet2] = XLSX.utils.aoa_to_sheet(
// js 表数据
[
["a1", "b1"],
["a2", "b2"],
]
);
XLSX.writeFile(workbook, "out.xlsx", {
type: "string",
bookType: "xlsx",
});
});
js 生成Excel