vue 使用 XLSX 导出 element-ui 的 el-table 到 Excel

import * as XLSX from "xlsx";

exportExcel() {
                var wb = XLSX.utils.table_to_book(document.querySelector('#data-table2'), { raw: true});
                var wbout = XLSX.write(wb, {
                    bookType: 'xls',
                    book: true,
                    type: 'array',
                })
                try {
                    FileSaver.saveAs(
                        new Blob([wbout], { type: 'appliction/octet-stream' }),
                        '检查记录明细.xls'
                    )
                } catch (e) {
                    if (typeof console != 'undefined')
                        console.log(e, wbout);
                }
                return wbout
            },
exportExcelAsHTML() {
                var html = "<html><head><meta charset='utf-8' /></head><body>" + $("#data-table").html() + "</body></html>";
                var now = new Date();
                var date = now.getFullYear() + '-' + (now.getMonth() + 1) + '-' + now.getDate() + ' ' + now.getHours() + ':' + now.getMinutes() + ':' + now.getSeconds();
                var fileName = $(".title").html();
                var exportFileName = fileName + date + ' ' + ".xls";//自定义导出excel表名称
                var blob = new Blob([html], { type: "application/vnd.ms-excel" });
                saveAs(blob, exportFileName);
            }

 

posted on 2023-03-31 18:36  空明流光  阅读(151)  评论(0编辑  收藏  举报

导航