vue exporter excel télécharger et ne pas télécharger

Télécharger les dépendances :

npm exécuter installer xlsx

npm installer l'économiseur de fichiers -S

Introduire:

  importer * au format XLSX à partir de 'xlsx' ;

  importer FileSaver depuis 'fichier-économiseur' ;

normal:

 Exporter le téléchargement :

    const exData = (dom, title) => {
      let wb = XLSX.utils.table_to_book(dom, { raw: true });
      let wbout = XLSX.write(wb, {
        bookType: 'xlsx',
        bookSST: true,
        type: 'array',
      });
      try {
        FileSaver.saveAs(new Blob([wbout], { type: 'application/octet-stream' }), title + `.xlsx`);
      } catch (e) {
        if (typeof console !== 'undefined') {
          console.log(e, wbout);
        }
      }
      return wbout;
    };
    exData(document.querySelector('#idsssx'), '薪资表');

Exporter sans télécharger :

let datasses = datass.value + '-' + datas.value;
      const getExcel = (tableRef, excelTitle) => {
        const wb = XLSX.utils.table_to_book(tableRef, { raw: true });
        const wbout = XLSX.write(wb, {
          bookType: 'xlsx',
          bookSST: true,
          type: 'array',
        });
        return new File([wbout], excelTitle + '.xlsx');
      };
      const excel = getExcel(document.getElementById('baga'), '薪资表');
      console.log(excel);
      const obj = new FormData();
      obj.append('excel', excel);
      obj.append('nowdate', datasses);
      obj.append('projectid', 6);
      obj.append('companyid', 2);
      axios({
        method: 'POST',
        url: '/wage/index/batch_add',
        data: obj,
      }).then((res) => {
        console.log(res);
        if (res.data.code == 2) {
          ElMessage({
            message: '添加失败,请检查未填写的地方',
            type: 'warning',
          });
        } else {
          ElMessage({
            showClose: true,
            message: '添加成功',
            type: 'success',
          });
          addition.value = 0;
        }
      });

おすすめ

転載: blog.csdn.net/m0_72196169/article/details/130994768
おすすめ