支持表尾合并导出
This commit is contained in:
@@ -5,7 +5,7 @@
|
||||
[](http://npm-stat.com/charts.html?package=vxe-table-plugin-export-xlsx)
|
||||
[](LICENSE)
|
||||
|
||||
基于 [vxe-table](https://www.npmjs.com/package/vxe-table) 的表格插件,支持导出 xlsx 格式,依赖 [exceljs](https://github.com/exceljs/exceljs) 库
|
||||
基于 [vxe-table](https://www.npmjs.com/package/vxe-table) 的表格插件,支持导出 xlsx 格式,基于 [exceljs](https://github.com/exceljs/exceljs) 实现
|
||||
|
||||
## Installing
|
||||
|
||||
|
||||
74
index.ts
74
index.ts
@@ -31,21 +31,11 @@ const defaultCellFontColor = '606266'
|
||||
const defaultCellBorderStyle = 'thin'
|
||||
const defaultCellBorderColor = 'e8eaec'
|
||||
|
||||
function getFooterCellValue ($table: VxeTableConstructor, opts: VxeTablePropTypes.ExportConfig, rows: any[], column: VxeTableDefines.ColumnInfo) {
|
||||
const cellValue = rows[$table.getVMColumnIndex(column)]
|
||||
return cellValue
|
||||
}
|
||||
|
||||
function getFooterData (opts: VxeTablePropTypes.ExportConfig, footerData: any[][]) {
|
||||
const { footerFilterMethod } = opts
|
||||
return footerFilterMethod ? footerData.filter((items, index) => footerFilterMethod({ items, $rowIndex: index })) : footerData
|
||||
}
|
||||
|
||||
function getCellLabel (column: VxeTableDefines.ColumnInfo, cellValue: any) {
|
||||
if (cellValue) {
|
||||
switch (column.cellType) {
|
||||
case 'string':
|
||||
break
|
||||
return XEUtils.toString(cellValue)
|
||||
case 'number':
|
||||
if (!isNaN(cellValue)) {
|
||||
return Number(cellValue)
|
||||
@@ -61,6 +51,16 @@ function getCellLabel (column: VxeTableDefines.ColumnInfo, cellValue: any) {
|
||||
return cellValue
|
||||
}
|
||||
|
||||
function getFooterData (opts: VxeTablePropTypes.ExportConfig, footerData: any[][]) {
|
||||
const { footerFilterMethod } = opts
|
||||
return footerFilterMethod ? footerData.filter((items, index) => footerFilterMethod({ items, $rowIndex: index })) : footerData
|
||||
}
|
||||
|
||||
function getFooterCellValue ($table: VxeTableConstructor, opts: VxeTablePropTypes.ExportConfig, rows: any[], column: VxeTableDefines.ColumnInfo) {
|
||||
const cellValue = getCellLabel(column, rows[$table.getVMColumnIndex(column)])
|
||||
return cellValue
|
||||
}
|
||||
|
||||
function getValidColumn (column: VxeTableDefines.ColumnInfo): VxeTableDefines.ColumnInfo {
|
||||
const { childNodes } = column
|
||||
const isColGroup = childNodes && childNodes.length
|
||||
@@ -72,7 +72,7 @@ function getValidColumn (column: VxeTableDefines.ColumnInfo): VxeTableDefines.Co
|
||||
|
||||
function setExcelRowHeight (excelRow: ExcelJS.Row, height: number) {
|
||||
if (height) {
|
||||
excelRow.height = XEUtils.floor(height * 0.8, 1)
|
||||
excelRow.height = XEUtils.floor(height * 0.75, 12)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -129,17 +129,19 @@ function exportXLSX (params: VxeGlobalInterceptorHandles.InterceptorExportParams
|
||||
const footList: any[] = []
|
||||
const sheetCols: any[] = []
|
||||
const sheetMerges: { s: { r: number, c: number }, e: { r: number, c: number } }[] = []
|
||||
let beforeRowCount = 0
|
||||
const colHead: any = {}
|
||||
columns.forEach((column) => {
|
||||
const { id, property, renderWidth } = column
|
||||
colHead[id] = original ? property : column.getTitle()
|
||||
sheetCols.push({
|
||||
key: id,
|
||||
width: XEUtils.ceil(renderWidth / 8, 1)
|
||||
})
|
||||
})
|
||||
// 处理表头
|
||||
if (isHeader) {
|
||||
const colHead: any = {}
|
||||
columns.forEach((column) => {
|
||||
const { id, property, renderWidth } = column
|
||||
colHead[id] = original ? property : column.getTitle()
|
||||
sheetCols.push({
|
||||
key: id,
|
||||
width: XEUtils.ceil(renderWidth / 8, 1)
|
||||
})
|
||||
})
|
||||
// 处理分组
|
||||
if (isColgroup && !original && colgroups) {
|
||||
colgroups.forEach((cols, rIndex) => {
|
||||
let groupHead: any = {}
|
||||
@@ -163,24 +165,16 @@ function exportXLSX (params: VxeGlobalInterceptorHandles.InterceptorExportParams
|
||||
} else {
|
||||
colList.push(colHead)
|
||||
}
|
||||
beforeRowCount += colList.length
|
||||
}
|
||||
// 处理合并
|
||||
if (isMerge && !original) {
|
||||
mergeCells.forEach(mergeItem => {
|
||||
let { row: mergeRowIndex, rowspan: mergeRowspan, col: mergeColIndex, colspan: mergeColspan } = mergeItem
|
||||
for (let rIndex = 0; rIndex < datas.length; rIndex++) {
|
||||
let rowIndex = $table.getVTRowIndex(datas[rIndex]._row)
|
||||
if (rowIndex === mergeRowIndex) {
|
||||
if (isHeader && colgroups) {
|
||||
rowIndex = rIndex + colgroups.length
|
||||
}
|
||||
sheetMerges.push({
|
||||
s: { r: rowIndex, c: mergeColIndex },
|
||||
e: { r: rowIndex + mergeRowspan - 1, c: mergeColIndex + mergeColspan - 1 }
|
||||
})
|
||||
break
|
||||
}
|
||||
}
|
||||
sheetMerges.push({
|
||||
s: { r: mergeRowIndex + beforeRowCount, c: mergeColIndex },
|
||||
e: { r: mergeRowIndex + beforeRowCount + mergeRowspan - 1, c: mergeColIndex + mergeColspan - 1 }
|
||||
})
|
||||
})
|
||||
}
|
||||
const rowList = datas.map(item => {
|
||||
@@ -190,10 +184,22 @@ function exportXLSX (params: VxeGlobalInterceptorHandles.InterceptorExportParams
|
||||
})
|
||||
return rest
|
||||
})
|
||||
beforeRowCount += rowList.length
|
||||
// 处理表尾
|
||||
if (isFooter) {
|
||||
const { footerData } = $table.getTableData()
|
||||
const footers = getFooterData(options, footerData)
|
||||
const mergeFooterItems = $table.getMergeFooterItems()
|
||||
// 处理合并
|
||||
if (isMerge && !original) {
|
||||
mergeFooterItems.forEach(mergeItem => {
|
||||
let { row: mergeRowIndex, rowspan: mergeRowspan, col: mergeColIndex, colspan: mergeColspan } = mergeItem
|
||||
sheetMerges.push({
|
||||
s: { r: mergeRowIndex + beforeRowCount, c: mergeColIndex },
|
||||
e: { r: mergeRowIndex + beforeRowCount + mergeRowspan - 1, c: mergeColIndex + mergeColspan - 1 }
|
||||
})
|
||||
})
|
||||
}
|
||||
footers.forEach((rows) => {
|
||||
const item: any = {}
|
||||
columns.forEach((column) => {
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "vxe-table-plugin-export-xlsx",
|
||||
"version": "3.0.0-alpha.1",
|
||||
"version": "3.0.0-alpha.2",
|
||||
"description": "基于 vxe-table 的表格插件,支持导出 xlsx 格式",
|
||||
"scripts": {
|
||||
"lib": "gulp build"
|
||||
@@ -48,9 +48,9 @@
|
||||
"markdown-doctest": "^1.1.0",
|
||||
"prettier": "^2.1.2",
|
||||
"typescript": "^4.0.5",
|
||||
"vue": "^3.0.2",
|
||||
"vxe-table": "^4.0.0-beta.6",
|
||||
"xe-utils": "^3.0.4"
|
||||
"vue": "^3.0.4",
|
||||
"vxe-table": "^4.0.0-beta.12",
|
||||
"xe-utils": "^3.1.8"
|
||||
},
|
||||
"peerDependencies": {
|
||||
"vxe-table": ">= 4"
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
{
|
||||
"files": [
|
||||
"index.d.ts",
|
||||
"index.ts",
|
||||
"depend.ts"
|
||||
],
|
||||
|
||||
Reference in New Issue
Block a user