exceljs代码

本文介绍了如何在前端项目中使用exceljs库进行Excel文件的读取、写入和操作,详细阐述了关键API的使用方法及实战示例,帮助开发者高效地处理Excel数据。
testExcel(data) {
      let userinfo = this.getuserName();
      const workbook = new ExcelJS.Workbook();
      // 创建带有红色标签颜色的工作表
      // const sheet = workbook.addWorksheet("My Sheet", {
      //   properties: { tabColor: { argb: "FFC0000" } },
      // });
      // 创建一个第一行和列冻结的工作表
      const sheet = workbook.addWorksheet("My Sheet");
      sheet.properties.defaultColWidth = 30;
      // 在表格底部追加新行

      let wopts = { bookType: "xlsx", bookSST: false, type: "binary" };
      let row = 1;
      sheet.getRow(row).values = ["操作用户:", userinfo];
      row += 1;
      let before = this.excel_statusList[this.implementLeftData]
      sheet.getRow(row).values = ["操作前状态:", before];
      if (this.implementLeftData) {
        sheet.getRow(row).getCell(2).fill = {
          type: "pattern",
          pattern: "solid",
          fgColor: {
            argb: `${
              this.Excel_Status_cclor[
                this.implementLeftData
              ]
            }`,
          },
          
        };
      }
      row += 1;
      let after = this.excel_statusList[this.implementRightData]
      sheet.getRow(row).values = ["操作后状态:", after];
      if (this.implementRightData) {
        sheet.getRow(row).getCell(2).fill = {
          type: "pattern",
          pattern: "solid",
          fgColor: {
            argb: `${
              this.Excel_Status_cclor[
                this.implementRightData
              ]
            }`,
          },
        };
      }
      row += 1;
      sheet.getRow(row).values = ["操作时间:", new Date()];
      row += 1;
      sheet.getRow(row).values = [
        "操作前检查内容",
        "参考值",
        "识别结果",
        "操作后检查内容",
        "参考值",
        "识别结果",
      ];
      for (let i = 0; i < data.length; i++) {
        row += 1;
        sheet.getRow(row).values = data[i];
      }
      // for (let i = 0; i < data[2].length; i++) {}
      // 给单个单元格设置样式
      // sheet.getCell("A1").fill = {
      //   type: "pattern",
      //   pattern: "solid",
      //   fgColor: { argb: "FFFF0000" },
      // };
      //  插入图片
      // this.$axios
      //   .get("/static/img/bg1.png", { responseType: "arraybuffer" })
      //   .then((res) => {
      //     console.log(res);
      //     let buffer = res.data;
      //     const imageId2 = workbook.addImage({
      //       buffer: buffer,
      //       extension: "png",
      //     });
      //     sheet.addImage(imageId2, {
      //       tl: { col: 0, row: 3 },
      //       ext: { width: 50, height: 10 },
      //       editAs: "absolute",
      //     });

      //   })
      //   .catch((err) => {
      //     console.log(err);
      //   });
      workbook.xlsx
        .writeBuffer(wopts)
        .then((res) => {
          window.test = res;
          common.saveAs(res, "test.xlsx", {
            type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
          });
        })
        .catch((err) => {
          this.$Tips({ content: "生成文件失败", tyep: "error" });
        });
      // 通过 buffer 将图像添加到工作簿

      // const row = sheet.getRow(5);
    },
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Dnhz-w

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值