使用插件js-export-excel将table数据导出为excel文件
首先在项目中安装js-export-excel插件,进入项目目录,执行以下命令:
npm安装:npm install --save js-export-excel,
const downloadExcel = () => {
const getRepaymentPlanList = exceldata ? exceldata : '';//exceldata 表格总数据
let option = {}; //option代表的就是excel文件
let dataTable = []; //excel文件中的数据内容
if (getRepaymentPlanList && getRepaymentPlanList.length > 0) {
for (let i in getRepaymentPlanList) { //循环获取excel中每一行的数据
// let _planDay = formatTime(getRepaymentPlanList[i].createdAt, true); //格式化日期(自定义方法)
let obj = {
'ID': getRepaymentPlanList[i].id,
'会员id': getRepaymentPlanList[i].memberid,
'账变id': getRepaymentPlanList[i].account_changeid,
'出金方式': getRepaymentPlanList[i].way,
'汇率': getRepaymentPlanList[i].parities,
'原币金额': getRepaymentPlanList[i].price,
'本位币金额(美金)': getRepaymentPlanList[i].money,
'备注': getRepaymentPlanList[i].remark,
'创建人': getRepaymentPlanList[i].author,
'创建时间': getRepaymentPlanList[i].creation_time,
'状态': getRepaymentPlanList[i].status,
// '事件':_planDay
}
dataTable.push(obj); //设置excel中每列所获取的数据源
}
}
option.fileName = '出金查询数据'; //excel文件名称
option.datas = [
{
sheetData: dataTable, //excel文件中的数据源
sheetName: '出金查询数据', //excel文件中sheet页名称
sheetFilter: ['ID','会员id','账变id','出金方式','汇率','原币金额','本位币金额(美金)','备注','创建人','创建时间','状态'], //excel文件中需显示的列数据
sheetHeader: ['ID','会员id','账变id','出金方式','汇率','原币金额','本位币金额(美金)','备注','创建人','创建时间','状态'], //excel文件中每列的表头名称
}
]
let toExcel = new ExportJsonExcel(option); //生成excel文件
toExcel.saveExcel(); //下载excel文件
}