传统的表格导出是直接获取数据,传表头,数据,自动下载表格。Blob.js 和 Export2Excel.js
如果要导出这样的表格,那么需要重新写一个方法,核心在于 { s: { r: 行开始, c:列开始 }, e: { r: 行结束, c: 列开始 } }
const mergesMap = reactive({ spankeys: ['department_name', 'role_name', 'user_name'] })配置需要合并的列
const getTableMerges = (params: Record<string, any>) => { const { mergesMap, dataList, column } = params const _merges = [] dataList.map((v: any, index: number) => { for (let _key in mergesMap) { if (v[_key]) { const spankeys = mergesMap[_key] let obj = {} for (let key in v) { spankeys.map((k: any) => { if (k === key) { const _rowIndex = index let rEnd = _rowIndex + v[_key] const _index = column.findIndex((v: any) => v.prop === key) obj = { e: { r: rEnd, c: _index }, s: { r: index + 1, c: _index } } _merges.push(obj) } }) } } } }) return _merges }
只要把 merges 传进去就可以了
export function export_json_to_excel(th: any, jsonData: any, defaultTitle: any, merges?: any) { /* original data */ var data = jsonData; data.unshift(th); var ws_name = "SheetJS"; const wb: any = { SheetNames: [], Sheets: {} } const ws: any = sheet_from_array_of_arrays(data); ws['!merges'] = merges || [] /* add worksheet to workbook */ wb.SheetNames.push(ws_name); wb.Sheets[ws_name] = ws; var wbout = XLSX.write(wb, { bookType: 'xlsx', bookSST: false, type: 'binary' }); (window as any).saveAs(new Blob([s2ab(wbout)], { type: "application/octet-stream" }), `${defaultTitle ? defaultTitle : '列表'}.xlsx`) }
标签:index,const,name,表格,merges,excel,导出,key,any From: https://www.cnblogs.com/ruose/p/16719393.html