( ExcelJS: any, props: TableProps, toolbar: ExportExcelToolbar, withoutData: boolean = false )
| 252 | * @param withoutData 如果为 true 就不导出数据,只导出表头 |
| 253 | */ |
| 254 | export async function exportExcel( |
| 255 | ExcelJS: any, |
| 256 | props: TableProps, |
| 257 | toolbar: ExportExcelToolbar, |
| 258 | withoutData: boolean = false |
| 259 | ) { |
| 260 | const { |
| 261 | store, |
| 262 | env, |
| 263 | classnames: cx, |
| 264 | translate: __, |
| 265 | data, |
| 266 | prefixRow, |
| 267 | affixRow |
| 268 | } = props; |
| 269 | let columns = store.exportColumns || []; |
| 270 | |
| 271 | let rows = []; |
| 272 | let tmpStore; |
| 273 | let filename = 'data'; |
| 274 | // 支持配置 api 远程获取 |
| 275 | if (typeof toolbar === 'object' && toolbar.api) { |
| 276 | const pageField = toolbar.pageField || 'page'; |
| 277 | const perPageField = toolbar.perPageField || 'perPage'; |
| 278 | const ctx: any = createObject(data, { |
| 279 | ...props.query, |
| 280 | [pageField]: data.page || 1, |
| 281 | [perPageField]: data.perPage || 10 |
| 282 | }); |
| 283 | |
| 284 | const res = await env.fetcher(toolbar.api, ctx, { |
| 285 | autoAppend: true, |
| 286 | pageField, |
| 287 | perPageField |
| 288 | }); |
| 289 | if (!res.data) { |
| 290 | env.notify('warning', __('placeholder.noData')); |
| 291 | return; |
| 292 | } |
| 293 | /** |
| 294 | * 优先找items和rows,找不到就拿第一个值为数组的字段 |
| 295 | * 和CRUD中的处理逻辑保持一致,避免能渲染和导出的不一致 |
| 296 | */ |
| 297 | if (Array.isArray(res.data)) { |
| 298 | rows = res.data; |
| 299 | } else if (Array.isArray(res.data?.rows)) { |
| 300 | rows = res.data.rows; |
| 301 | } else if (Array.isArray(res.data?.items)) { |
| 302 | rows = res.data.items; |
| 303 | } else { |
| 304 | for (const key of Object.keys(res.data)) { |
| 305 | if (res.data.hasOwnProperty(key) && Array.isArray(res.data[key])) { |
| 306 | rows = res.data[key]; |
| 307 | break; |
| 308 | } |
| 309 | } |
| 310 | } |
| 311 |
no test coverage detected