This article mainly introduces the download function implemented by the front-end based on the existing data in the table. It does not need to call the interface, but a function of the pure front-end; by the way, introduce the printing function.
1. Print
Get the form data, call the print method of the window; refresh the page after printing and re-assign values to the form.
<template>
<div>
<el-button @click="printTable">打印表格</el-button>
<el-table id="myTable" :data="tableData">
<el-table-column prop="date" label="日期"></el-table-column>
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
</div>
</template>
export default {
data(){
return {
tableData:[{
date:'xxxx',name:'aaaa',address:'ssss'}]
}
},
methods:{
//打印页面内容
printTable(){
let wpt = document.querySelector('#myTable');
let newContent = wpt.innerHTML;
let oldContent = document.body.innerHTML;
document.body.innerHTML = newContent;
window.print(); //打印方法
// window.localtion.reload();
history.go(0)
document.body.innerHTML = oldContent;
}
}
}
2. Download
Need to use plug-ins xlsx and file-saver;
install plug-ins:
//各种电子表格格式的解析器和编写器
npm install xlsx
npm install file-saver --save
Implementation:
<template>
<div>
<el-button @click="downloadExcel">下载表格</el-button>
<el-table id="myTable" :data="tableData">
<el-table-column prop="date" label="日期"></el-table-column>
<el-table-column prop="name" label="姓名"></el-table-column>
<el-table-column prop="address" label="地址"></el-table-column>
</el-table>
</div>
</template>
import FileSaver from 'file-saver';
import XLSX from 'xlsx';
export default {
data(){
return {
tableData:[{
date:'xxxx',name:'aaaa',address:'ssss'}]
}
},
methods:{
//导出Excel
exportToExcel () {
//获取 table 表格数据
let et = XLSX.utils.table_to_book(document.querySelector('#myTable')); //此处传入table的DOM节点
let etout = XLSX.write(et, {
bookType: 'xlsx',
bookSST: true,
type: 'array'
});
try {
FileSaver.saveAs(new Blob([etout], {
type: 'application/octet-stream'
}), 'trade-publish.xlsx'); //trade-publish.xlsx 为导出的文件名
} catch (e) {
console.log(e, etout) ;
}
return etout;
}
}
}
The download achieved by this method can only download the data displayed in the current table; if there is paging,