vue exceljs 实现导出excel并设置网格线、背景色、 垂直居中、分页打印

一、 下载 exceljs

pnpm install exceljs

二、 页面中使用

    // 导出 exportExcel
    exportToExcel() {
      this.$confirm("此操作将导出excel文件, 是否继续?", "提示", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          const loading = this.$loading({
            lock: true,
            text: "导出中...",
            spinner: "el-icon-loading",
            background: "rgba(0, 0, 0, 0.7)",
          });
          const workbook = new ExcelJS.Workbook();
          const sheet = workbook.addWorksheet("销售人数明细表");
          // 设置网格线颜色和间距
          sheet.properties.gridLines = {
            color: { argb: "FFC0C0C0" }, // 设置网格线颜色为灰色
            style: "thin", // 设置网格线样式为细线
          };
          // 添加表头
          const headerRow = sheet.addRow([
            "项目",
            "签约日期",
            "签约销售",
            "客户姓名",
            "客户编号",
          ]);

          // 获取表头行对象并设置样式 文字居中,加粗
          headerRow.eachCell((cell) => {
            cell.alignment = { vertical: "middle", horizontal: "center" };
            cell.font = { bold: true };
            cell.fill = {
              type: "pattern",
              pattern: "solid",
              fgColor: { argb: "FF808080" },
            };
            // 设置表头单元格边框
            cell.border = {
              top: { style: "thin", color: { argb: "FF000000" } },
              left: { style: "thin", color: { argb: "FF000000" } },
              bottom: { style: "thin", color: { argb: "FF000000" } },
              right: { style: "thin", color: { argb: "FF000000" } },
            };
          });
          const headerRowHeight = 30; // 设置表头行的高度
          headerRow.height = headerRowHeight;

          const allData = []; // 用于存储来自不同分页的所有数据的数组

          const response = await getcheckinlist({
            ...this.formInline,
            page: 1,
            limit: 99999,
          });
          const { data } = response;
          allData.push(...data.rows);
          // 添加表格数据
          allData.forEach((item) => {
            const row = sheet.addRow([
              item.projectname,
              item.changedate,
              item.customername,
              item.username,
              item.customerno,
            ]);
            // 设置数据行样式 文字居中
            row.eachCell((cell) => {
              // 设置数据行样式 文字居中,设置行高
              row.eachCell((cell) => {
                cell.alignment = { vertical: "middle", horizontal: "center" };
                cell.fill = {
                  type: "pattern",
                  pattern: "solid",
                  fgColor: { argb: "f6f6f6f6" }, // 设置颜色
                };
                cell.border = {
                  top: { style: "thin", color: { argb: "FF000000" } },
                  left: { style: "thin", color: { argb: "FF000000" } },
                  bottom: { style: "thin", color: { argb: "FF000000" } },
                  right: { style: "thin", color: { argb: "FF000000" } },
                };
              });
              const dataRowHeight = 30; // 设置数据行的高度
              row.height = dataRowHeight;
            });
          });
          // 设置列宽
          sheet.columns.forEach((column) => {
            column.width = 15;
          });
          // 生成文件
          const buffer = await workbook.xlsx.writeBuffer();
          const excelData = new Blob([buffer], {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          });
          loading.close();
          // 下载文件
          saveAs(excelData, "人数明细表.xlsx");
        })
        .catch(() => {
          this.$message({
            type: "info",
            message: "导出失败",
          });
        });
    },
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
实现分页功能,在导出Excel表格之前,你需要先将数据按照每页显示的数据条数进行分割,然后将每一页的数据分别写入不同的工作簿中。 以下是一个简单的Vue导出Excel表格分页实现方法: 1. 安装依赖包 使用 `npm` 或 `yarn` 安装以下依赖包: ``` npm install file-saver xlsx ``` 2. 创建一个导出Excel表格的方法 在Vue的组件中创建一个方法,用于导出Excel表格。这个方法接收两个参数:`data` 和 `title`。其中,`data` 是要导出的数据,`title` 是导出Excel表格的标题。 ``` exportExcel(data, title) { const fileName = `${title}.xlsx`; const sheetSize = 10; // 每页显示的数据条数 const sheetCount = Math.ceil(data.length / sheetSize); // 工作簿数量 const workbook = { Sheets: {}, SheetNames: [] }; for (let i = 0; i < sheetCount; i++) { const sheetData = data.slice(i * sheetSize, (i + 1) * sheetSize); const sheetName = `Sheet${i + 1}`; const worksheet = XLSX.utils.json_to_sheet(sheetData); workbook.Sheets[sheetName] = worksheet; workbook.SheetNames.push(sheetName); } const excelBuffer = XLSX.write(workbook, { bookType: "xlsx", type: "array" }); const excelBlob = new Blob([excelBuffer], { type: "application/octet-stream" }); saveAs(excelBlob, fileName); } ``` 3. 调用导出Excel表格的方法 在需要导出Excel表格的地方,调用上面创建的方法即可。例如: ``` exportExcel(this.tableData, "表格数据"); ``` 这样就可以将 `tableData` 中的数据按照每页显示10条的方式分页,并导出为多个工作簿的Excel表格,每个工作簿显示10条数据。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值