js 生成Excel

  1. 安装依赖
npm install xlsx
  1. 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",
  });
});


posted @ 2020-07-01 12:56  Ajanuw  阅读(484)  评论(0编辑  收藏  举报