前端實現excel導出

網頁中的實現方式

1.引入所需的依賴文件
https://github.com/SheetJS/sh...
https://github.com/eligrey/Fi...javascript

<script src="https://cdn.bootcss.com/FileSaver.js/2014-11-29/FileSaver.min.js"></script>
<script lang="javascript" src="dist/xlsx.full.min.js"></script>

2.s2ab函數css

function s2ab(s) {
    const buf = new ArrayBuffer(s.length);
    const view = new Uint8Array(buf);
    for (let i = 0; i !== s.length; ++i) {
        view[i] = s.charCodeAt(i) & 0xFF;
    };
    return buf;
}

3.所須要的數據結構java

[
    ["列A", "列B"], // 這將做爲excel文件的標題
    [1,2]
]

4.一個例子git

let data = [
    ["列A", "列B"],
    [3,4]
];

let wopts = { bookType:'xlsx', type:'binary' };
let fileName = "bill.xlsx";

const ws = XLSX.utils.aoa_to_sheet(data);
const wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(wb, ws, 'Sheet1');

const wbout = XLSX.write(wb, wopts);
saveAs(new Blob([s2ab(wbout)]), fileName); // 保存爲文件
相關文章
相關標籤/搜索