js-xlxs导出excel

    s2ab(s) {
          if(typeof ArrayBuffer !== 'undefined') {
            var buf = new ArrayBuffer(s.length);
            var view = new Uint8Array(buf);
            for(var i = 0; i != s.length; ++i) view[i] = s.charCodeAt(i) & 0xFF;
            return buf;
          } else {
            var buf = new Array(s.length);
            for(var i = 0; i != s.length; ++i) buf[i] = s.charCodeAt(i) & 0xFF;
            return buf;
          }
      },
      createXlsx(){
        const data=[{"teacher_name":"","mechanism_id":"","teacher_phone":""}]
        const wopts = {
          bookType: 'xlsx',
          bookSST: false,
          type: 'binary'
        }
        const wb = {
          SheetNames: ['Sheet1'],
          Sheets: {},
          Props: {}
        };
        wb.Sheets['Sheet1'] = this.XLSX.utils.json_to_sheet(data); //通过json_to_sheet转成单页(Sheet)数据
        var blob = new Blob([this.s2ab(this.XLSX.write(wb, wopts))], {type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=utf-8'});
        var downloadElement = document.createElement('a');
        var href = window.URL.createObjectURL(blob); //创建下载的链接
        downloadElement.href = href;
        downloadElement.download = 'test123.xlsx'; //下载后文件名
        document.body.appendChild(downloadElement);
        downloadElement.click(); //点击下载
        document.body.removeChild(downloadElement); //下载完成移除元素
      }

猜你喜欢

转载自blog.csdn.net/weixin_39168678/article/details/83546339