element UI + excel导出

      
        
        
        
        
         
      

      tableData: [
        {
          id: 1,
          date: "2016-05-03",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        },
        {
          date: "2016-05-02",
          name: "王小虎",
          address: "上海市普陀区金沙江路 1518 弄",
        }
      ],


    import htmlToExcel from "@/views/util/htmlToExcel";
    handleDownload() {
      const dom = document.getElementById("tables");
      htmlToExcel.getExcel(dom, "标题名称");
    },
import FileSaver from "file-saver";
const XLSX = require('xlsx');
const htmlToExcel = {
    getExcel(dom, title){
        var excelTitle = title;
        console.log(dom)
        var wb = XLSX.utils.table_to_book(dom)
        // console.log({wb})
        var wbout = XLSX.write(wb,{bookType:'xlsx',bookSST:true, type:'array'});
        // console.log({wbout})
        try{
            FileSaver.saveAs(
                new Blob([wbout],{type:'application?octet-stream'}),
                excelTitle + '.xlsx'
            )
        }catch(e){
            if(typeof console !== 'undefined') console.log(e,wbout)
        }
        return wbout;
        }
}
export default htmlToExcel;

用到了file-saver和xlsx

npm install xlsx, npm install file-saver

你可能感兴趣的:(#,第一次见你,ui,excel,vue.js)