fiddle导出jmx文件_javascript把json数据导出为excel文件

JavaScript

语言:

JaveScriptBabelCoffeeScript

确定

function jsonToExcel() {

const jsonData = [{

name: '路人甲',

phone: '123456',

email: '[email protected]'

}, {

name: '路人乙',

phone: '123456',

email: '[email protected]'

}, {

name: '路人丙',

phone: '123456',

email: '[email protected]'

}, {

name: '路人丁',

phone: '123456',

email: '[email protected]'

}, ]

//列标题

let str = '

姓名电话邮箱';

//循环遍历,每行加入tr标签,每个单元格加td标签

for (let i = 0; i < jsonData.length; i++) {

str += '

';

for (let item in jsonData[i]) {

//增加\t为了不让表格显示科学计数法或者其他格式

str += `

${ jsonData[i][item] + '\t'}`;

}

str += '

';

}

//Worksheet名

let worksheet = 'Sheet1'

let uri = 'data:application/vnd.ms-excel;base64,';

//下载的表格模板数据

let template = `

xmlns:x="urn:schemas-microsoft-com:office:excel"

xmlns="http://www.w3.org/TR/REC-html40">

`;

//下载模板

window.location.href = uri + base64(template)

}

//输出base64编码

function base64(s) {

return window.btoa(unescape(encodeURIComponent(s)))

}

你可能感兴趣的:(fiddle导出jmx文件)