You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
/* external references: - https://rawgit.com/SheetJS/js-xlsx/master/dist/xlsx.full.min.js - https://rawgit.com/eligrey/FileSaver.js/master/FileSaver.js*//* original data */vardata=[{"name":"John","city": "Seattle"},{"name":"Mike","city": "Los Angeles"},{"name":"Zach","city": "New York"}];/* this line is only needed if you are not adding a script tag reference */if(typeofXLSX=='undefined')XLSX=require('xlsx');/* make the worksheet */varws=XLSX.utils.json_to_sheet(data);/* add to workbook */varwb=XLSX.utils.book_new();XLSX.utils.book_append_sheet(wb,ws,"People");/* write workbook (use type 'binary') */varwbout=XLSX.write(wb,{bookType:'xlsx',type:'binary'});/* generate a download */functions2ab(s){varbuf=newArrayBuffer(s.length);varview=newUint8Array(buf);for(vari=0;i!=s.length;++i)view[i]=s.charCodeAt(i)&0xFF;returnbuf;}saveAs(newBlob([s2ab(wbout)],{type:"application/octet-stream"}),"sheetjs.xlsx");
需求描述
将一组数据导出到 Excel 中,xlsx 或者 CSV 格式均可,数据中包含数字,保证导出的结果正确即可。
因为 Excel 默认会将数字显示为科学计数法的形式,即数字
10000000000000
会显示为1E+13
,所以要想办法解决这个问题。方案调研
调研过程
先 Google
js export to excel
,在第一个链接 JS导出excel插件总结 中列出了三种方案,三个项目 excellentexport、exceljs 和 js-xlsx 的 Star 数量级分别在 600、2500 和 10000,那就用 js-xlsx 这个库吧。入选方案
排除方案
应用过程
接着用
js-xlsx save array
作为关键字 Google,第一个结果指向 js-xlsx 在 GitHub 的官网,第二个结果则是该项目在 GitHub 上的 issue:How to simply export a Worksheet to xlsx? · Issue #817 · SheetJS/js-xlsx,很可能会看到示例代码,点进去看看。第一条回复 就给出了导出数组到 Excel 中的方法:
自己根据业务需求修改后的代码如下:
要点总结
The text was updated successfully, but these errors were encountered: