一、 下载 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: "导出失败",
          });
        });
    },
GitHub 加速计划 / vu / vue
106
18
下载
vuejs/vue: 是一个用于构建用户界面的 JavaScript 框架,具有简洁的语法和丰富的组件库,可以用于开发单页面应用程序和多页面应用程序。
最近提交(Master分支:2 个月前 )
9e887079 [skip ci] 1 年前
73486cb5 * chore: fix link broken Signed-off-by: snoppy <michaleli@foxmail.com> * Update packages/template-compiler/README.md [skip ci] --------- Signed-off-by: snoppy <michaleli@foxmail.com> Co-authored-by: Eduardo San Martin Morote <posva@users.noreply.github.com> 1 年前
Logo

新一代开源开发者平台 GitCode,通过集成代码托管服务、代码仓库以及可信赖的开源组件库,让开发者可以在云端进行代码托管和开发。旨在为数千万中国开发者提供一个无缝且高效的云端环境,以支持学习、使用和贡献开源项目。

更多推荐