纯前端excel数据导出
前言
为了便于用户更好的对数据进行筛选操作统计。
基于Vue框架、iView UI组件开发
一、需求
- 将表格中的所有数据导出
- 有数据则导出,无数据则提示导出失败
二、需求开发
1.需要用到的组件
代码如下(示例):
<Row type="flex" justify="end" align="bottom" class="code-row-bg">
<Button type="success" @click="tableExport">导出</Button>
</Row>
<Table border :columns="tableInfo" :data="recordList.rows"></Table>
<Page :total="recordList.total" :page-size="recordList.limit" :current="recordList.page" @on-change="changePageHandler" show-elevator show-total></Page>
表格展示如下(示例):
2.定义数据
代码如下(示例):
data () {
return {
tableInfo: [
{
title: 'Name',
key: 'name'
},
{
title: 'Age',
key: 'age'
},
{
title: 'Num',
key: 'num',
render: (h, params) => {
return h('span', this.showNum(params.row.num))
}
},
{
title: 'Address',
key: 'address'
},
{
title: 'Date',
key: 'date'
}
],
listData:{
page:1,
size:10,
....
}
recordList: [],
exportTotal:0,
}
},
3.获取表格数据
1、接口请求
mounted () {
this.list()
},
methods: {
list () {
let data = {page:1,size:20}
getRecordList(data).then((res) => {
if (res.status === 200) {
let recordList = res.data || []
let {total} = recordList
this.exportTotal=total
} else {
this.$Message.error(res.msg)
}
})
}
},
showNum (data) {
let showNum = ''
switch (data) {
case 'one':
showNum = '数字一'
break
case 'two':
showNum = '数字二'
break
case 'three':
showNum = '数字三'
break
case 'four':
showNum = '数字四'
break
default:
showNum = 'ss'
}
return showNum
}
}
}
2、接口返回的数据结构
this.recordList = {
total: 8,
page: 1,
size: 10,
rows: [
{
name: 'John Brown',
age: 18,
num: 'one',
address: 'New York No. 1 Lake Park',
date: '2016-10-03'
},
{
name: 'Jim Green',
age: 24,
num: 'two',
address: 'London No. 1 Lake Park',
date: '2016-10-01'
},
{
name: 'Joe Black',
age: 30,
num: 'three',
address: 'Sydney No. 1 Lake Park',
date: '2016-10-02'
},
{
name: 'Jon Snow',
age: 26,
num: 'four',
address: 'Ottawa No. 2 Lake Park',
date: '2016-10-04'
},
{
name: 'John Brown',
age: 18,
num: 'one',
address: 'New York No. 1 Lake Park',
date: '2016-10-03'
},
{
name: 'Jim Green',
age: 24,
num: 'two',
address: 'London No. 1 Lake Park',
date: '2016-10-01'
},
{
name: 'Joe Black',
age: 30,
num: 'three',
address: 'Sydney No. 1 Lake Park',
date: '2016-10-02'
},
{
name: 'Jon Snow',
age: 26,
num: 'four',
address: 'Ottawa No. 2 Lake Park',
date: '2016-10-04'
}
]
}
4.导出
1、导出方法一,后端接收数据值时,可能存在限制,size过大时,接口挂了😂
tableExport () {
if (this.exportTotal > 0) {
this.listData = {page:1,size:this.exportTotal}
let data = JSON.parse(JSON.stringify(this.listData))
getRecordList(data).then((res) => {
if (res.status === 200) {
let exportList = res.data || []
let {rows} = exportList
rows.forEach((item) => {
Object.assign(item, {
num:this.showNum(item.num)
})
})
this.exportExcelFile(rows)
} else {
this.$Message.error(res.msg)
}
})
} else {
this.$Message.warning('数据为空,导出失败')
}
},
exportExcelFile (excelData) {
const w = [50, 133, 130, 130, 130]
const th = [
'Name', 'Age', 'Num', 'Address', 'Date',
]
const filterVal = [
'name',
'age',
'num',
'address',
'date',
]
const data = excelData.map(v => filterVal.map(k => v[k]))
const [fileName, fileType, sheetName] = ['列表', 'xlsx', '列表信息']
toExcel({ th, data, fileName, fileType, sheetName, w })
}
2、导出方法二,当方法一size过大时,那就只能多请求几次接口咯🉑
tableExport () {
if (this.exportTotal > 0) {
let data = JSON.parse(JSON.stringify(this.listData))
let queryList = []
const queryData = (exportPage) => {
data.pageSize = 1000
if (exportPage) {
data.page = exportPage
}
getRecordList(data.pageSize,data.page).then((res) => {
this.loading = false
if (res.status === 200) {
let exportList = res.data || []
let {rows, total} = exportList
rows.forEach((item) => {
Object.assign(item, {
num:this.showNum(item.num)
})
})
queryList = queryList.concat(rows)
if (total === queryList.length) {
this.exportExcelFile(queryList)
this.$Message.info('导出成功')
} else {
let exportPage = exportList['page'] += 1
queryData(exportPage)
}
} else {
this.$Message.error(res.msg)
}
})
}
queryData()
} else {
this.$Message.warning('数据为空,导出失败')
}
},
总结
请求表格数据的接口和导出请求的是同一个接口,复制的时候记得改改接收接口返回数据的变量名,避免表格数据出现问题 数据量较少时,可采用第一种方法,数据量较多时,建议第二种
|