1.安装依赖
npm install -S file-saver xlsx
npm install -D script-loader
2.导入两个JS
3.main.js 引入文件
import Blob from './components/excel/Blob'
import Export2Excel from './components/excel/Export2Excel.js'
4.组件中使用
//导出的方法
exportExcel() {
require.ensure([], () => {
const { export_json_to_excel } = require('../excel/Export2Excel');
const tHeader = ['序号', '昵称', '姓名'];
// 上面设置Excel的表格第一行的标题
const filterVal = ['index', 'nickName', 'name'];
// 上面的index、nickName、name是tableData里对象的属性
const list = this.tableData; //把data里的tableData存到list
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]))
}
tHeader是表头,filterVal 中的数据是表格的字段,tableData中存放表格里的数据,类型为数组,里面存放对象,表格的每一行为一个对象。
tableData 中的值为:
data () {
return {
tableData: [
{'index':'0',"nickName": "沙滩搁浅我们的旧时光", "name": "小明"},
{'index':'1',"nickName": "女人天生高贵", "name": "小红"},
{'index':'2',"nickName": "海是彩色的灰尘", "name": "小兰"}
]
}
}
在社保局项目PC端里有实现
健康鱼中:
handleQuery(isExportOperation = false) {
this.loading = true;
statisticsAppointment2({
...this.queryParams,
hospitalId: this.$store.state.user.userInfo.hospitalId
}).then(res => {
if (+res.data.code === 200 && Array.isArray(res.data.data)) {
this.statisticsList = res.data.data;
if (isExportOperation === true) {
let rowNumber = 0;
const data = this.statisticsList.map(row => {
return {
NO: ++rowNumber,
机构名称: row.orgName,
个人预约数: row.allocated,
个人检测数: row.detected,
个人报告数: row.reported,
团体申请数: row.teamApply,
团体预约数: row.teamAllocated,
团体检测数: row.teamDetected,
团体报告数: row.teamReported
};
});
const filename =
"HIV统计数据导出-" + parseTime(new Date(), "{y}{m}{d}{h}{i}{s}");
let wb = XLSX.utils.book_new();
XLSX.utils.book_append_sheet(
wb,
XLSX.utils.json_to_sheet(data),
"统计数据"
);
XLSX.writeFile(wb, filename + ".xlsx");
}
this.loading = false;
}
});
},