vxeTable 表格使用 打印、导出文件 自定义
打印样式调节、自定义数据源(全量打印)、导出xlsx(全量数据)文件 使用实例
vxe使用版本
"@vxe-ui/plugin-export-xlsx": "4.0.8",
"vxe-pc-ui": "^4.3.66",
"vxe-table": "^4.10.2",
"xlsx": "^0.18.5",
[xlsx]([Vxe UI v4 Plugins](https:
按需导入 或者 全量导入 根据个人需求 配置[官方文档]([Vxe Table v4](https:
使用案例
<vxe-table border show-overflow ref="tableRef" :data="tableData" :print-config="{ style: printStyle }">
<vxe-column field="codeTitle" title="指标名称" align="center"></vxe-column>
......
</vxe-table>
const printStyle = ref(`
table tbody tr td {
white-space: normal !important;
word-break: break-all !important;
}
`)
const getExportData = async () => {
printPagination.page = 1
let isEnd = true
let data = []
while (isEnd) {
const res = await getPrintData()
if (res?.status === 1 && res?.data?.data?.length) {
data = [...data, ...res.data.data]
if (res.data.data.length < printPagination.limit) {
isEnd = false
} else {
printPagination.page++
}
} else {
isEnd = false
}
}
data.forEach((item) => {
item.remark = item.remark || '-'
})
return data
}
const exportData = ref('')
const printEvent = async () => {
const $table = tableRef.value
if (!$table) return
try {
exportData.value = await getExportData()
exportData.value.forEach((item) => {
if (Number(item.statisticsValue) > Number(item.statisticsValueOld)) {
item.statisticsValue = `${item.statisticsValue} <span> ${item.unit || ''}</span> <span style='color: #28c345'>↑</span>`
} else if (Number(item.statisticsValue) < Number(item.statisticsValueOld)) {
item.statisticsValue = `${item.statisticsValue}<span> ${item.unit || ''}</span> <span style='color: #f2503b'>↓</span>`
}
item.statisticsValue = item.statisticsValue || '-'
item.statisticsValueOld = item.statisticsValueOld ? item.statisticsValueOld + (item.unit || '') : '-'
})
$table.print({
data: exportData.value
})
} catch (error) {
console.error('打印失败:', error)
ElMessage.error('打印失败')
}
})
const exportEvent = async () => {
const $table = tableRef.value
if (!$table) return
if (!tableData.value?.length) {
ElMessage.warning('暂无数据导出')
return
}
try {
exportData.value = await getExportData()
exportData.value.forEach((item) => {
})
$table.exportData({
filename: '标准修订记录',
type: 'xlsx',
data: exportData.value
})
} catch (error) {
console.error('导出失败:', error)
ElMessage.error('导出失败')
}
}
代码写的不好,可自行纠正,谢谢