|
1 | | -export function arrayToExcel(data: any[], name: string) { |
2 | | - const a = document.createElement('a') |
3 | | - const blob = new Blob([data.map(v => Object.values(v).join('\t')).join('\n')], { type: 'application/vnd.ms-excel' }) |
4 | | - a.href = URL.createObjectURL(blob) |
5 | | - a.download = name |
6 | | - a.click() |
| 1 | +import { createElement } from './createElement' |
| 2 | +import { isStr } from './isStr' |
| 3 | +import { uuid } from './uuid' |
| 4 | + |
| 5 | +interface Target { |
| 6 | + style?: string |
| 7 | + background?: string |
| 8 | + color?: string |
| 9 | + width?: number |
| 10 | + height?: number |
| 11 | +} |
| 12 | + |
| 13 | +interface IArrayToExcel { |
| 14 | + data: Record<string, any>[] |
| 15 | + filename?: string |
| 16 | + title?: string[] |
| 17 | + filter?: string[] |
| 18 | +} |
| 19 | + |
| 20 | +export function arrayToExcel(options: IArrayToExcel) { |
| 21 | + const { data, filename = uuid(), title, filter } = options |
| 22 | + if (!data) |
| 23 | + return |
| 24 | + const arrData = typeof data != 'object' ? JSON.parse(data) : data |
| 25 | + let excel = `<table><tr>${title |
| 26 | + ? title.map(item => `<th align='center'>${item}</th>`).join('') |
| 27 | + : Object.keys(arrData[0]).map((key) => { |
| 28 | + if (filter && filter.includes(key)) |
| 29 | + return '' |
| 30 | + return `<th align='center'>${key}</th>` |
| 31 | + }).join('')}</tr>` |
| 32 | + for (let i = 0; i < arrData.length; i++) { |
| 33 | + let row = '<tr>' |
| 34 | + for (const index in arrData[i]) { |
| 35 | + if (filter) { |
| 36 | + if (!filter.includes(index)) { |
| 37 | + const target = arrData[i][index] == null ? '' : arrData[i][index] |
| 38 | + if (isStr(target)) { row += `<td>${target}</td>` } |
| 39 | + else { |
| 40 | + const { value, width, colspan = 0, rowspan = 0 } = target |
| 41 | + row += `<td width=${width} colspan=${colspan} rowspan=${rowspan} style="${generateStyle(target)}">${value}</td>` |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + else { |
| 46 | + const target = arrData[i][index] == null ? '' : arrData[i][index] |
| 47 | + if (isStr(target)) { row += `<td align='center'>${target}</td>` } |
| 48 | + else { |
| 49 | + const { value, width, colspan = 0, rowspan = 0 } = target |
| 50 | + row += `<td align='center' width=${width} colspan=${colspan} rowspan=${rowspan} style="${generateStyle(target)}">${value}</td>` |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + |
| 55 | + excel += `${row}</tr>` |
| 56 | + } |
| 57 | + excel += '</table>' |
| 58 | + createElement('a', { href: `data:application/vnd.ms-excel;charset=utf-8,${encodeURIComponent(generateHtml(excel))}`, style: 'visibility:hidden', download: `${filename}.xls` }).click() |
| 59 | +} |
| 60 | + |
| 61 | +function generateStyle(target: Target) { |
| 62 | + let result = '' |
| 63 | + const { style = '', background, color } = target |
| 64 | + if (background) |
| 65 | + result += `background:${background};` |
| 66 | + if (color) |
| 67 | + result += `color:${color};` |
| 68 | + return result += style |
| 69 | +} |
| 70 | + |
| 71 | +function generateHtml(excel: string) { |
| 72 | + return `<html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/TR/REC-html40'><meta http-equiv="content-type" content="application/vnd.ms-excel; charset=UTF-8"> |
| 73 | + <meta http-equiv="content-type" content="application/vnd.ms-excel';charset=UTF-8"><head><!--[if gte mso 9]><xml><x:ExcelWorkbook><x:ExcelWorksheets><x:ExcelWorksheet><x:Name>{worksheet}</x:Name><x:WorksheetOptions><x:DisplayGridlines/></x:WorksheetOptions></x:ExcelWorksheet></x:ExcelWorksheets></x:ExcelWorkbook></xml><![endif]--></head><body>${excel}</body></html>` |
7 | 74 | } |
0 commit comments