参考博客
前端导出表格为CSV文件功能
代码
getExportList() {
let jsonData = {
trade:{
tHeader: ["名字","性别","年龄","爱好","身份证号码"],
filterVal: ["name","sex","nlin","aihao","sfz"],
list: [{name: "名字啊",sex: "性别啊",aihao: "爱好啊",nlin:"年龄啊",sfz:"123456789012345678"},
{name: "名字哦",sex: "性别哦",nlin:"年龄哦",aihao: "爱好哦",sfz:"123456789012345678"}]
},
order:{
tHeader: ["附表名字","附表性别","附表年龄","附表爱好"],
filterVal: ["name","sex","nlin","aihao"],
list: [{name: "附表名字啊",sex: "附表性别啊",nlin:"附表年龄啊",aihao: "附表爱好啊",},
{name: "附表名字哦",sex: "附表性别哦",nlin:"附表年龄哦",aihao: "附表爱好哦",}]
}
}
this.exportPathMethod(jsonData)
},
exportPathMethod(data) {
let mainLists = data.trade
let subLists = data.order
let mainTitle = mainLists.tHeader;
let mainTitleForKey = mainLists.filterVal;
let mainList = mainLists.list;
let mainStr = [];
mainStr.push(mainTitle.join("\t,")+"\n");
for(let i=0;i<mainList.length;i++){
let temp = [];
for(let j=0;j<mainTitleForKey.length;j++){
temp.push(mainList[i][mainTitleForKey[j]]);
}
mainStr.push(temp.join("\t,")+"\n");
}
let subTitle = subLists.tHeader;
let subTitleForKey = subLists.filterVal;
let subData = subLists.list;
let subStr = [];
subStr.push(subTitle.join("\t,")+"\t\n");
for(let i=0;i<subData.length;i++){
let temp = [];
for(let j=0;j<subTitleForKey.length;j++){
temp.push(subData[i][subTitleForKey[j]]);
}
subStr.push(temp.join("\t,")+"\t\n");
}
let merged = mainStr.join("") + '\n' + subStr.join("")
const uri = 'data:text/csv;charset=utf-8,\ufeff' + encodeURIComponent(merged)
let link = document.createElement('a')
link.href = uri
link.download = '客户管理列表.csv'
document.body.appendChild(link)
link.click()
},
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步