vue纯前端导出excel表(file-saver 和 xlsx)

1.安装file-saver 和 xlsx
2.封装方法(重点)


import FileSaver from "file-saver";
import XLSX from "xlsx";

outputXLSX = (filename, selector, _this) => {
 
  selector = selector || "#mytable"; //mytable为默认的表格id名
  filename = filename // 文件名
  var wb = XLSX.utils.table_to_book(document.querySelector(selector), {
    raw: true
  });
  
  var wbout = XLSX.write(wb, {
    bookType: "xlsx",
    bookSST: false,
    type: "array"
  });
  try {
    FileSaver.saveAs(
      new Blob([wbout], { type: "application/octet-stream" }),
      filename + ".xlsx"
    );
   if(_this)  _this.$message.success("导出成功"); //导出成功的提示
  } catch (e) {
    if (typeof console !== "undefined") console.log(e, wbout);
  }
  return wbout;
};

3.vue文件里面写个table,exportList是要导出的数据列表


       
       
       
       
    

4.调用outputXLSX导出

exportData(){
	outputXLSX('文件名', '#mytable', this);
}

你可能感兴趣的:(前端,vue.js,javascript)