插件:https://github.com/SheetJS/sheetjs
修改处:json导出表格时会把所有字段都导出,如果自定义的header不全,也会导出,修改完就不导出多余的了。h[d=h.length]=t 改为 return true ,就是xlsx.js里的sheet_add_json函数里的 hdr[C=hdr.length] = k
用处:这个插件其实还可以从数组导出、从页面里的table中导出
demo:
<script src="/static/js/sheetjs/xlsx.core.min.js"></script>
http.post('/admin/Order/getOrderList',{page: 0, search:that.search},function (res) { let rows=res.data.rows; // 定义标题字段及顺序 const header = ["id", "order_num","nickname","kindname","total_coins","due_time","pay_status_str","create_time"]; // 定义标题字段显示名称 const headerStr=["id","订单号","昵称","类别","积分","有效期止","付款状态","创建时间"]; let headerDisplay={}; headerStr.forEach(function(value,index){ headerDisplay[header[index]]=value; }); // const headerDisplay = {id:"id", order_num:"订单号", nickname:"昵称", kindname:"类别", total_coins:"积分", due_time:"有效期止", pay_status_str:"付款状态",create_time:"创建时间"}; const newData = [headerDisplay, ...rows]; const worksheet = XLSX.utils.json_to_sheet(newData, {header:header, skipHeader:true}); var workbook = XLSX.utils.book_new(); XLSX.utils.book_append_sheet(workbook, worksheet, "sheet1"); XLSX.writeFile(workbook, "订单列表.xlsx"); that.loading.table=false; })