vue 表格导出到Excel

导出到Excel,纠正fixed时,造成的列重复问题;
入参:表对象id,导出时的文件名称
需要引用xlsx插件;


function sexportExcel(sid, sname){
  // 设置当前日期
  let time = new Date();
  let year = time.getFullYear();
  let month = time.getMonth() + 1;
  let day = time.getDate();
  let name = sname + "  " + year + "" + month + "" + day;
  let xlsxParam = { raw: true };//转换成excel时,使用原始的格式

  let table = document.querySelector(sid);
  // 因为element-ui的表格的fixed属性导致多出一个table,会下载重复内容,这里删除掉
  var fix = table.querySelector('.el-table__fixed');

  let wb ;//mytable为表格的id名
  if (fix) {
    wb = XLSX.utils.table_to_book(table.removeChild(fix),xlsxParam);

    table.appendChild(fix);
  } else {
    wb = XLSX.utils.table_to_book(table,xlsxParam);
  }

  
  let wbout = XLSX.write(wb, {
    bookType: "xlsx",
    bookSST: true,
    type: "array",
  });
  try {
    FileSaver.saveAs(
      new Blob([wbout], { type: "application/octet-stream" }),
      name+".xlsx"
    );
  } catch (e) {
    if (typeof console !== "undefined") console.log(e, wbout);
  }
  return wbout;
}

你可能感兴趣的:(vue 表格导出到Excel)