vue 实现(导出)excel表格 xlsx格式

1丶npm install  file-saver
2丶npm install xlsx

import XLSX from 'xlsx';
import saveAs from 'file-saver';

//使用ref实现获取元素


   s2ab(s) {
                if (typeof ArrayBuffer !== 'undefind') {
                    let buf = new ArrayBuffer(s.length);
                    let view = new Uint8Array(buf);
                    for (let i = 0; i != s.length; ++i) {
                    view[i] = s.charCodeAt(i) & 0xFF;
                    }
                    return buf;
                } else {
                    let buf = new Array(s.length);
                    for (let i = 0; i != s.length; ++i) {
                    buf[i] = s.charCodeAt(i) & 0xFF;
                    }
                    return buf;
                }
            },


downloadTable(data) {
              let wb = XLSX.utils.table_to_book(this.$refs[xxxxx][0]);
                    let wopts = {
                        bookType: 'xlsx',
                        bookSST: false,
                        type: 'binary'
                    };
                    let wbout = XLSX.write(wb, wopts);

                saveAs(new Blob([this.s2ab(wbout)], {
                    type: 'application/octet-stream;charset=utf-8'
                }), data.title + '.xlsx');
 },

你可能感兴趣的:(vue 实现(导出)excel表格 xlsx格式)