From 4a79f1372ba22a394b76108eba1cba111b98959a Mon Sep 17 00:00:00 2001 From: xuliangzhan Date: Wed, 31 Jan 2024 08:22:02 +0800 Subject: [PATCH] fix --- index.ts | 34 ++++++++++++++++++++-------------- package.json | 8 ++++---- tsconfig.json | 1 + types/index.d.ts | 4 +++- 4 files changed, 28 insertions(+), 19 deletions(-) diff --git a/index.ts b/index.ts index ee26123..3ab8e17 100644 --- a/index.ts +++ b/index.ts @@ -8,7 +8,9 @@ import { TableExportConfig, ColumnAlign } from 'vxe-table' -import * as ExcelJS from 'exceljs' +import type ExcelJS from 'exceljs' + +let globalExcelJS: any const defaultHeaderBackgroundColor = 'f8f8f9' const defaultCellFontColor = '606266' @@ -49,7 +51,7 @@ function getFooterCellValue ($table: Table, opts: TableExportConfig, rows: any[] } declare module 'vxe-table' { - interface ColumnInfo { + export interface ColumnInfo { _row: any; _colSpan: number; _rowSpan: number; @@ -209,16 +211,16 @@ function exportXLSX (params: InterceptorExportParams) { }) } const exportMethod = () => { - const workbook = new ExcelJS.Workbook() + const workbook: ExcelJS.Workbook = new (globalExcelJS || (window as any).ExcelJS).Workbook() const sheet = workbook.addWorksheet(sheetName) workbook.creator = 'vxe-table' sheet.columns = sheetCols if (isHeader) { - sheet.addRows(colList).forEach(excelRow => { + sheet.addRows(colList).forEach((excelRow: any) => { if (useStyle) { setExcelRowHeight(excelRow, rowHeight) } - excelRow.eachCell(excelCell => { + excelRow.eachCell((excelCell: any) => { const excelCol = sheet.getColumn(excelCell.col) const column: any = $table.getColumnById(excelCol.key as string) const { headerAlign, align } = column @@ -244,11 +246,11 @@ function exportXLSX (params: InterceptorExportParams) { }) }) } - sheet.addRows(rowList).forEach(excelRow => { + sheet.addRows(rowList).forEach((excelRow) => { if (useStyle) { setExcelRowHeight(excelRow, rowHeight) } - excelRow.eachCell(excelCell => { + excelRow.eachCell((excelCell) => { const excelCol = sheet.getColumn(excelCell.col) const column: any = $table.getColumnById(excelCol.key as string) const { align } = column @@ -296,7 +298,7 @@ function exportXLSX (params: InterceptorExportParams) { sheetMerges.forEach(({ s, e }) => { sheet.mergeCells(s.r + 1, s.c + 1, e.r + 1, e.c + 1) }) - workbook.xlsx.writeBuffer().then(buffer => { + workbook.xlsx.writeBuffer().then((buffer: any) => { const blob = new Blob([buffer], { type: 'application/octet-stream' }) // 导出 xlsx downloadFile(params, blob, options) @@ -344,7 +346,7 @@ function checkImportData (tableFields: string[], fields: string[]) { } declare module 'vxe-table' { - interface Table { + export interface Table { _importResolve?: Function | null; _importReject?: Function | null; } @@ -379,7 +381,7 @@ function importXLSX (params: InterceptorImportParams) { tableFields.push(field) } }) - const workbook = new ExcelJS.Workbook() + const workbook: ExcelJS.Workbook = new (globalExcelJS || (window as any).ExcelJS).Workbook() const readerTarget = evnt.target if (readerTarget) { workbook.xlsx.load(readerTarget.result as ArrayBuffer).then(wb => { @@ -450,13 +452,17 @@ function handleExportEvent (params: InterceptorExportParams) { * 基于 vxe-table 表格的增强插件,支持导出 xlsx 格式 */ export const VXETablePluginExportXLSX = { - install (vxetable: VXETableCore) { + install (vxetable: VXETableCore, options?: { + ExcelJS?: any + }) { // 检查版本 - if (!/^(2|3)\./.test(vxetable.version)) { - console.error('[vxe-table-plugin-export-xlsx] Version vxe-table 3.x is required') + if (!/^(3)\./.test(vxetable.version)) { + console.error('[vxe-table-plugin-export-xlsx 3.x] Version vxe-table 3.x is required') } - vxetable.setup({ + globalExcelJS = options ? options.ExcelJS : null + + vxetable.config({ export: { types: { xlsx: 0 diff --git a/package.json b/package.json index 9b72e95..b918c64 100644 --- a/package.json +++ b/package.json @@ -1,7 +1,7 @@ { "name": "vxe-table-plugin-export-xlsx", - "version": "2.3.1", - "description": "基于 vxe-table 的表格插件,支持导出 xlsx 格式", + "version": "3.3.0", + "description": "基于 vxe-table 表格的扩展插件,支持导出 xlsx 格式", "scripts": { "lib": "gulp build" }, @@ -34,7 +34,7 @@ "eslint-plugin-promise": "^4.2.1", "eslint-plugin-standard": "^4.0.2", "eslint-plugin-typescript": "^0.14.0", - "exceljs": "^4.2.0", + "exceljs": "^4.4.0", "gulp": "^4.0.2", "gulp-autoprefixer": "^7.0.1", "gulp-babel": "^8.0.0", @@ -52,7 +52,7 @@ "typescript": "^4.6.4", "vue": "^2.6.14", "vxe-table": "^3.7.2", - "xe-utils": "^3.5.13" + "xe-utils": "^3.5.19" }, "peerDependencies": { "vxe-table": "^3.7.0" diff --git a/tsconfig.json b/tsconfig.json index c2a9e75..2b4f3f7 100644 --- a/tsconfig.json +++ b/tsconfig.json @@ -6,6 +6,7 @@ "strict": true, "moduleResolution": "node", "noImplicitAny": true, + "allowSyntheticDefaultImports": true, "target": "esnext", "lib": [ "esnext", diff --git a/types/index.d.ts b/types/index.d.ts index 4bb7991..5705c31 100644 --- a/types/index.d.ts +++ b/types/index.d.ts @@ -4,7 +4,9 @@ import { VXETableCore } from 'vxe-table' * 基于 vxe-table 表格的扩展插件,支持导出 xlsx 格式 */ export declare const VXETablePluginExportXLSX: { - install (vxetable: VXETableCore): void + install (vxetable: VXETableCore, options?: { + ExcelJS?: any + }): void } export default VXETablePluginExportXLSX