当我们在使用Vue的v-for指令来动态渲染表格时,如何将表格导出成Excel或CSV文件呢?下面就是详细的攻略。
步骤一:安装依赖库
在Vue项目中,我们需要使用js-xlsx和file-saver这两个库来实现导出表格功能。
npm install file-saver xlsx -S
步骤二:定义数据和表头
在Vue组件中,我们需要定义要导出的数据和表头。
<template>
<div>
<button @click="exportTable">导出表格</button>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>职业</th>
</tr>
</thead>
<tbody>
<tr v-for="item in tableData" :key="item.id">
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
<td>{{ item.job }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import { saveAs } from 'file-saver'
import XLSX from 'xlsx'
export default {
data () {
return {
tableData: [
{ id: 1, name: '张三', age: 20, job: '工程师' },
{ id: 2, name: '李四', age: 22, job: '设计师' },
{ id: 3, name: '王五', age: 24, job: '销售员' },
{ id: 4, name: '赵六', age: 26, job: '产品经理' },
{ id: 5, name: '钱七', age: 28, job: '运营总监' }
]
}
},
methods: {
exportTable () {
const tHeader = ['姓名', '年龄', '职业']
const filterVal = ['name', 'age', 'job']
const data = this.formatJson(filterVal, this.tableData)
const fileName = '表格导出'
this.exportJsonToExcel(tHeader, data, fileName)
},
formatJson (filterVal, jsonData) {
return jsonData.map(v => filterVal.map(j => v[j]))
},
exportJsonToExcel (tHeader, data, fileName) {
const sheetName = '表格1'
const wb = { SheetNames: [], Sheets: {} }
const ws = XLSX.utils.aoa_to_sheet([tHeader, ...data])
wb.SheetNames.push(sheetName)
wb.Sheets[sheetName] = ws
const excelBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' })
this.saveAsExcel(excelBuffer, fileName)
},
saveAsExcel (buffer, fileName) {
const fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
const excelBlob = new Blob([buffer], { type: fileType })
saveAs(excelBlob, `${fileName}.xlsx`)
}
}
}
</script>
上述代码中,我们定义了一个tableData数组保存表格数据,定义了tHeader数组用于保存表头信息,然后定义了三个方法:formatJson用于将表格数据格式化成数组形式、exportJsonToExcel用于将数据导出为Excel文件格式、saveAsExcel用于将Excel文件进行保存。其中,XLSX.utils.aoa_to_sheet方法用于将数组转换为Sheet格式。
步骤三:点击“导出表格”按钮进行导出
在Vue组件中,我们可以使用@click事件监听按钮的点击,然后调用exportTable方法即可。
以上就是Vue如何将v-for中的表格导出来的完整攻略。下面给出另外一种示例。
示例一:带有动态过滤和排序的表格
<template>
<div>
<button @click="exportTable">导出表格</button>
<table>
<thead>
<tr>
<th>姓名</th>
<th>年龄</th>
<th>职业</th>
</tr>
</thead>
<tbody>
<tr v-for="item in filteredData" :key="item.id">
<td>{{ item.name }}</td>
<td>{{ item.age }}</td>
<td>{{ item.job }}</td>
</tr>
</tbody>
</table>
</div>
</template>
<script>
import { saveAs } from 'file-saver'
import XLSX from 'xlsx'
export default {
data () {
return {
tableData: [
{ id: 1, name: '张三', age: 20, job: '工程师' },
{ id: 2, name: '李四', age: 22, job: '设计师' },
{ id: 3, name: '王五', age: 24, job: '销售员' },
{ id: 4, name: '赵六', age: 26, job: '产品经理' },
{ id: 5, name: '钱七', age: 28, job: '运营总监' }
],
filterKey: '',
sortKey: '',
sortOrders: {
name: 1,
age: 1,
job: 1
}
}
},
computed: {
filteredData () {
const sortKey = this.sortKey
const filterKey = this.filterKey && this.filterKey.toLowerCase()
const order = this.sortOrders[sortKey] || 1
let data = this.tableData
if (filterKey) {
data = data.filter((row) => {
return Object.keys(row).some((key) => {
return String(row[key]).toLowerCase().indexOf(filterKey) > -1
})
})
}
if (sortKey) {
data = data.slice().sort((a, b) => {
a = a[sortKey]
b = b[sortKey]
return (a === b ? 0 : a > b ? 1 : -1) * order
})
}
return data
}
},
methods: {
exportTable () {
const tHeader = ['姓名', '年龄', '职业']
const filterVal = ['name', 'age', 'job']
const data = this.formatJson(filterVal, this.filteredData)
const fileName = '表格导出'
this.exportJsonToExcel(tHeader, data, fileName)
},
formatJson (filterVal, jsonData) {
return jsonData.map(v => filterVal.map(j => v[j]))
},
exportJsonToExcel (tHeader, data, fileName) {
const sheetName = '表格1'
const wb = { SheetNames: [], Sheets: {} }
const ws = XLSX.utils.aoa_to_sheet([tHeader, ...data])
wb.SheetNames.push(sheetName)
wb.Sheets[sheetName] = ws
const excelBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' })
this.saveAsExcel(excelBuffer, fileName)
},
saveAsExcel (buffer, fileName) {
const fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
const excelBlob = new Blob([buffer], { type: fileType })
saveAs(excelBlob, `${fileName}.xlsx`)
},
sortBy (key) {
this.sortKey = key
this.sortOrders[key] = this.sortOrders[key] * -1
}
}
}
</script>
示例一是一个带有动态过滤和排序功能的表格组件,我们可以将过滤后的数据导出为Excel文件。
示例二:使用element-ui组件库实现的表格
<template>
<div>
<el-button type="primary" @click="exportTable">导出表格</el-button>
<el-table :data="tableData" border style="width: 100%">
<el-table-column label="姓名" prop="name"></el-table-column>
<el-table-column label="年龄" prop="age"></el-table-column>
<el-table-column label="职业" prop="job"></el-table-column>
</el-table>
</div>
</template>
<script>
import { saveAs } from 'file-saver'
import XLSX from 'xlsx'
export default {
data () {
return {
tableData: [
{ id: 1, name: '张三', age: 20, job: '工程师' },
{ id: 2, name: '李四', age: 22, job: '设计师' },
{ id: 3, name: '王五', age: 24, job: '销售员' },
{ id: 4, name: '赵六', age: 26, job: '产品经理' },
{ id: 5, name: '钱七', age: 28, job: '运营总监' }
]
}
},
methods: {
exportTable () {
const tHeader = ['姓名', '年龄', '职业']
const filterVal = ['name', 'age', 'job']
const data = this.formatJson(filterVal, this.tableData)
const fileName = '表格导出'
this.exportJsonToExcel(tHeader, data, fileName)
},
formatJson (filterVal, jsonData) {
return jsonData.map(v => filterVal.map(j => v[j]))
},
exportJsonToExcel (tHeader, data, fileName) {
const sheetName = '表格1'
const wb = { SheetNames: [], Sheets: {} }
const ws = XLSX.utils.aoa_to_sheet([tHeader, ...data])
wb.SheetNames.push(sheetName)
wb.Sheets[sheetName] = ws
const excelBuffer = XLSX.write(wb, { bookType: 'xlsx', type: 'array' })
this.saveAsExcel(excelBuffer, fileName)
},
saveAsExcel (buffer, fileName) {
const fileType = 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet;charset=UTF-8'
const excelBlob = new Blob([buffer], { type: fileType })
saveAs(excelBlob, `${fileName}.xlsx`)
}
}
}
</script>
示例二是使用element-ui组件库实现的表格组件,同样可以将数据导出为Excel文件,使用方法和示例一类似。
总体来说,Vue将v-for中的表格导出来的方法并不难,只需要对数据进行格式化、实现导出功能、使用Blob进行文件下载等简单操作即可。
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:vue如何将v-for中的表格导出来 - Python技术站