将表格数据导出 Excel 格式的数据方法(2)

1、在工具类中创建一个脚本文件

/**
 * @param {*} tableValueData
 *      表格数据: []
 * @param {*} tableTitleData
 *      表格标题数据: ["", "", "", ""]
 * @param {*} fileName
 *      文件名
 * @param {*} tableHeader
 *      表格名
 */

// 引入插件
import XLSXS from "xlsx-js-style";
// 暴露方法
export function exportExcel(tableValueData, tableTitleData, fileName, tableHeader) {
  const header = [];
  const body = [];
  let strMaxNum = {};
  tableValueData.forEach((e, i) => {
    let styleArr = [];
    e.forEach((ele, ei) => {
      if (!ele) ele = 0;
      let styleObj = {
        v: ele,
        t: "s",
        s: {
          alignment: {
            vertical: "center",
            horizontal: "center",
          },
          border: {
            top: { style: "thin" },
            left: { style: "thin" },
            bottom: { style: "thin" },
            right: { style: "thin" },
          },
        },
      };
      styleArr.push(styleObj);
      strMaxNum[ei] = strMaxNum[ei]
        ? strMaxNum[ei] > ele.toString().length
          ? strMaxNum[ei]
          : ele.toString().length
        : ele.toString().length;
    });
    body.push(styleArr);
  });
  let cols = [];
  let title1 = [];
  tableTitleData.forEach((item, index) => {
    let obj = {
      v: item,
      t: "s",
      s: {
        alignment: {
          vertical: "center",
          horizontal: "center",
        },
        border: {
          top: { style: "thin" },
          left: { style: "thin" },
          bottom: { style: "thin" },
          right: { style: "thin" },
        },
      },
    };
    title1.push(obj);
    let resultMaxNum = Number(item.length) > strMaxNum[index] ? Number(item.length) : strMaxNum[index];
    let colsObj = { wch: resultMaxNum * 2 + 5 };
    cols.push(colsObj);
    // 设置标题
    if (index == 0) {
      header.push({
        v: tableHeader,
        t: "s",
        s: {
          font: {
            bold: true,
            sz: 14,
            name: "宋体",
          },
          alignment: {
            vertical: "center",
            horizontal: "center",
          },
          border: {
            top: { style: "thin" },
            left: { style: "thin" },
            bottom: { style: "thin" },
            right: { style: "thin" },
          },
        },
      });
    } else {
      header.push({
        v: '',
        t: "s",
        s: {
          font: {
            bold: true,
            sz: 14,
            name: "宋体",
          },
          alignment: {
            vertical: "center",
            horizontal: "center",
          },
          border: {
            top: { style: "thin" },
            left: { style: "thin" },
            bottom: { style: "thin" },
            right: { style: "thin" },
          },
        },
      });
    }
  });
  body.unshift(title1);
  body.unshift(header);
  const sheet = XLSXS.utils.aoa_to_sheet(body);

  sheet["!cols"] = cols;
  const rows = [{ hpx: 20 }, { hpx: 16 }, { hpx: 18 }];
  sheet["!rows"] = rows;

  let mergeNum = tableTitleData.length - 1;
  const merges = [{ s: { r: 0, c: 0 }, e: { r: 0, c: mergeNum } }];
  sheet["!merges"] = merges; // 将merges添加到sheet中

  // 导出Excel文件
  const workbook = XLSXS.utils.book_new(); // 创建虚拟的 workbook
  XLSXS.utils.book_append_sheet(workbook, sheet, tableHeader); // 向 workbook 中添加 sheet
  XLSXS.writeFile(workbook, `${fileName}.xlsx`); // 导出 workbook
}

2、在需要的地方引入使用

exportExcel(arr1,arr2,var1,var2)
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值