This commit is contained in:
xuliangzhan
2024-01-31 08:22:02 +08:00
parent a70c2a8c48
commit 4a79f1372b
4 changed files with 28 additions and 19 deletions

View File

@@ -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

View File

@@ -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"

View File

@@ -6,6 +6,7 @@
"strict": true,
"moduleResolution": "node",
"noImplicitAny": true,
"allowSyntheticDefaultImports": true,
"target": "esnext",
"lib": [
"esnext",

4
types/index.d.ts vendored
View File

@@ -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