1:安装插件
npm i -S file-saver xlsx
npm i -D script-loader
2:main.js引入xlsx并注册
import XLSX from 'xlsx'
Vue.use(XLSX);
3:src目录下创建vendor文件夹放Blob.js 和Export2Excel.js两个js文件
4:vue.config.js的alias中引入vendor
4.5:该页面要引入xlsx
import FileSaver from 'file-saver'
import XLSX from 'xlsx'
5:点击下载模板也就是导出table数据到excel
只要这两个方法就够导出了
downloadtp(){
require.ensure([], () => {
const { export_json_to_excel } = require('../../vendor/Export2Excel');
const tHeader = ['图书编号', '图书名称', '图书作者', '图书出版社', '图书类别','图书上架时间']; //对应表格输出的title
const filterVal = ['bookNumber', 'bookName', 'author', 'press', 'bookCategory','publishTime']; // 对应表格输出的数据
const list = this.outlist;
console.log('this.bll :>> ', this.outlist);
const data = this.formatJson(filterVal, list);
export_json_to_excel(tHeader, data, '图书列表excel'); //对应下载文件的名字
})
},
formatJson(filterVal, jsonData) {
return jsonData.map(v => filterVal.map(j => v[j]))
},
6:使用elementui el-upload组件上传导出的excel文件到表格
<el-upload
class="upload-demo"
action=""
:on-change="handleChange"
:on-remove="handleRemove"
:on-exceed="handleExceed"
:limit="limitUpload"
accept=".xlsx,application/vnd.openxmlformats-
officedocument.spreadsheetml.sheet,application/vnd.ms-excel"
:auto-upload="false">
<el-button type="success" @click="upfile" icon="el-icon-upload">上传文件</el-button>
</el-upload>
7:methods方法
handleChange(file, fileList){
this.fileTemp = file.raw;
if(this.fileTemp){
if((this.fileTemp.type == 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet')
|| (this.fileTemp.type == 'application/vnd.ms-excel')){
this.importfxx(this.fileTemp);
} else {
this.$message({
type:'warning',
message:'附件格式错误,请删除后重新上传!'
})
}
} else {
this.$message({
type:'warning',
message:'请上传附件!'
})
}
},
//超出最大上传文件数量时的处理方法
handleExceed(){
this.$message({
type:'warning',
message:'超出最大上传文件数量的限制!'
})
return;
},
//移除文件的操作方法
handleRemove(file,fileList){
this.fileTemp = null
},
importfxx(obj) {
let _this = this;
let inputDOM = this.$refs.inputer;
// 通过DOM取文件数据
this.file = event.currentTarget.files[0];
var rABS = false; //是否将文件读取为二进制字符串
var f = this.file;
var reader = new FileReader();
//if (!FileReader.prototype.readAsBinaryString) {
FileReader.prototype.readAsBinaryString = function(f) {
var binary = "";
var rABS = false; //是否将文件读取为二进制字符串
var pt = this;
var wb; //读取完成的数据
var outdata;
var reader = new FileReader();
reader.onload = function(e) {
var bytes = new Uint8Array(reader.result);
var length = bytes.byteLength;
for (var i = 0; i < length; i++) {
binary += String.fromCharCode(bytes[i]);
}
//此处引入,用于解析excel
var XLSX = require("xlsx");
if (rABS) {
wb = XLSX.read(btoa(fixdata(binary)), {
//手动转化
type: "base64"
});
} else {
wb = XLSX.read(binary, {
type: "binary"
});
}
outdata = XLSX.utils.sheet_to_json(wb.Sheets[wb.SheetNames[0]]);
console.log('outdata :>> ', outdata);
//outdata就是读取的数据(不包含标题行即表头,表头会作为对象的下标)
//此处可对数据进行处理
let arr = [];
outdata.map(v => {
let obj = {};
obj.bookNumber = v['图书编号'];
obj.bookName = v['图书名称'];
obj.author = v['图书作者'];
obj.press = v['图书出版社'];
obj.bookCategory = v['图书类别'];
obj.publishTime = v['图书上架时间']
arr.push(obj);
});
_this.inputList=arr;
_this.inputListLen=arr.length;
console.log('arr :>> ', arr);
return arr;
};
reader.readAsArrayBuffer(f);
};
if (rABS) {
reader.readAsArrayBuffer(f);
} else {
reader.readAsBinaryString(f);
}
},
8:data定义
outlist:[],//导出数据列表
inputList:[],// 导入数据
inputListLen:0// 导入数据长度
limitUpload:1 //限制长度