1
0
mirror of synced 2025-12-07 22:08:15 +08:00
This commit is contained in:
xuliangzhan
2024-01-21 11:04:48 +08:00
parent 063c3ad85b
commit ef1e72cc0f
4 changed files with 26 additions and 12 deletions

View File

@@ -14,16 +14,23 @@
## Installing ## Installing
```shell ```shell
npm install xe-utils vxe-table@next vxe-table-plugin-export-xlsx@next exceljs npm install vxe-table@next vxe-table-plugin-export-xlsx@next exceljs
``` ```
```javascript ```javascript
// ... // ...
import VXETable from 'vxe-table' import VXETable from 'vxe-table'
import VXETablePluginExportXLSX from 'vxe-table-plugin-export-xlsx' import VXETablePluginExportXLSX from 'vxe-table-plugin-export-xlsx'
import ExcelJS from 'exceljs'
// ... // ...
VXETable.use(VXETablePluginExportXLSX) // 方式1NPM 安装,注入 ExcelJS 对象
VXETable.use(VXETablePluginExportXLSX, {
ExcelJS
})
// 方式2CDN 安装,只要确保 window.ExcelJS 存在即可
// VXETable.use(VXETablePluginExportXLSX)
``` ```
## Demo ## Demo

View File

@@ -1,8 +1,9 @@
import XEUtils from 'xe-utils' import XEUtils from 'xe-utils'
import { VXETableCore, VxeTableConstructor, VxeTablePropTypes, VxeTableDefines, VxeGlobalInterceptorHandles } from 'vxe-table' import { VXETableCore, VxeTableConstructor, VxeTablePropTypes, VxeTableDefines, VxeGlobalInterceptorHandles } from 'vxe-table'
import ExcelJS from 'exceljs' import type ExcelJS from 'exceljs'
let vxetable:VXETableCore let vxetable: VXETableCore
let globalExcelJS: any
declare module 'vxe-table' { declare module 'vxe-table' {
export namespace VxeTableDefines { export namespace VxeTableDefines {
@@ -213,7 +214,7 @@ function exportXLSX (params: VxeGlobalInterceptorHandles.InterceptorExportParams
}) })
} }
const exportMethod = () => { const exportMethod = () => {
const workbook = new ExcelJS.Workbook() const workbook: ExcelJS.Workbook = new (globalExcelJS || (window as any).ExcelJS).Workbook()
const sheet = workbook.addWorksheet(sheetName) const sheet = workbook.addWorksheet(sheetName)
workbook.creator = 'vxe-table' workbook.creator = 'vxe-table'
sheet.columns = sheetCols sheet.columns = sheetCols
@@ -380,7 +381,7 @@ function importXLSX (params: VxeGlobalInterceptorHandles.InterceptorImportParams
tableFields.push(field) tableFields.push(field)
} }
}) })
const workbook = new ExcelJS.Workbook() const workbook: ExcelJS.Workbook = new (globalExcelJS || (window as any).ExcelJS).Workbook()
const readerTarget = evnt.target const readerTarget = evnt.target
if (readerTarget) { if (readerTarget) {
workbook.xlsx.load(readerTarget.result as ArrayBuffer).then(wb => { workbook.xlsx.load(readerTarget.result as ArrayBuffer).then(wb => {
@@ -451,13 +452,17 @@ function handleExportEvent (params: VxeGlobalInterceptorHandles.InterceptorExpor
* 基于 vxe-table 表格的扩展插件,支持导出 xlsx 格式 * 基于 vxe-table 表格的扩展插件,支持导出 xlsx 格式
*/ */
export const VXETablePluginExportXLSX = { export const VXETablePluginExportXLSX = {
install (vxetable: VXETableCore) { install (vxetable: VXETableCore, options?: {
ExcelJS?: any
}) {
// 检查版本 // 检查版本
if (!/^(4)\./.test(vxetable.version)) { if (!/^(4)\./.test(vxetable.version)) {
console.error('[vxe-table-plugin-export-pdf] Version vxe-table 4.x is required') console.error('[vxe-table-plugin-export-pdf] Version vxe-table 4.x is required')
} }
vxetable.setup({ globalExcelJS = options ? options.ExcelJS : null
vxetable.config({
export: { export: {
types: { types: {
xlsx: 0 xlsx: 0

View File

@@ -1,6 +1,6 @@
{ {
"name": "vxe-table-plugin-export-xlsx", "name": "vxe-table-plugin-export-xlsx",
"version": "3.1.0", "version": "4.0.0",
"description": "基于 vxe-table 表格的扩展插件,支持导出 xlsx 格式", "description": "基于 vxe-table 表格的扩展插件,支持导出 xlsx 格式",
"scripts": { "scripts": {
"lib": "gulp build" "lib": "gulp build"
@@ -51,8 +51,8 @@
"sass": "^1.55.0", "sass": "^1.55.0",
"typescript": "^4.6.4", "typescript": "^4.6.4",
"vue": "^3.3.4", "vue": "^3.3.4",
"vxe-table": "^4.5.10", "vxe-table": "^4.5.18",
"xe-utils": "^3.5.13" "xe-utils": "^3.5.18"
}, },
"peerDependencies": { "peerDependencies": {
"vxe-table": "^4.5.0" "vxe-table": "^4.5.0"

4
types/index.d.ts vendored
View File

@@ -4,7 +4,9 @@ import { VXETableCore } from 'vxe-table'
* 基于 vxe-table 表格的扩展插件,支持导出 xlsx 格式 * 基于 vxe-table 表格的扩展插件,支持导出 xlsx 格式
*/ */
export declare const VXETablePluginExportXLSX: { export declare const VXETablePluginExportXLSX: {
install (vxetable: VXETableCore): void install (vxetable: VXETableCore, options?: {
ExcelJS?: any
}): void
} }
export default VXETablePluginExportXLSX export default VXETablePluginExportXLSX