* 解析 Excel 数据 * 支持解析所有 sheet 或单个 sheet * 支持解析图片和富文本
(excelData: ArrayBuffer | string, fileState: ExcelFile)
| 524 | * 支持解析图片和富文本 |
| 525 | */ |
| 526 | async parseExcelData(excelData: ArrayBuffer | string, fileState: ExcelFile) { |
| 527 | const {allSheets, parseImage, plainText, translate: __} = this.props; |
| 528 | try { |
| 529 | const ExcelJS = (await import('exceljs')).default; |
| 530 | this.ExcelJS = ExcelJS; |
| 531 | const workbook = new ExcelJS.Workbook(); |
| 532 | |
| 533 | const data = |
| 534 | typeof excelData === 'string' |
| 535 | ? new Uint8Array(excelData.split('').map(c => c.charCodeAt(0))).buffer |
| 536 | : excelData; |
| 537 | |
| 538 | await workbook.xlsx.load(data); |
| 539 | |
| 540 | let sheetsResult: any; |
| 541 | if (allSheets) { |
| 542 | sheetsResult = this.parseAllSheets(workbook, parseImage, plainText); |
| 543 | } else { |
| 544 | const worksheet = workbook.worksheets.find( |
| 545 | (sheet: any) => sheet.state !== 'hidden' |
| 546 | ); |
| 547 | sheetsResult = parseImage |
| 548 | ? { |
| 549 | data: this.readWorksheet(worksheet, plainText), |
| 550 | images: this.readImages(worksheet, workbook) |
| 551 | } |
| 552 | : this.readWorksheet(worksheet, plainText); |
| 553 | } |
| 554 | |
| 555 | this.updateFileState(fileState.id, { |
| 556 | state: 'parsed', |
| 557 | data: sheetsResult |
| 558 | }); |
| 559 | } catch (error) { |
| 560 | console.error('Excel parsing error:', error); |
| 561 | this.updateFileState(fileState.id, { |
| 562 | state: 'error', |
| 563 | error: error.message || __('Excel.parseError') |
| 564 | }); |
| 565 | } |
| 566 | } |
| 567 | |
| 568 | /** |
| 569 | * 解析所有可见的 sheet |
no test coverage detected