本文主要为大家分享一篇javascript将json格式数组下载为excel表格的方法,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧,希望能帮助到大家。
实例如下:
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=utf-8">
<script type="text/javascript" src="jquery183.min.js"></script>
<script type="text/javascript">
$(document).ready(function(){
$('#wwo').click(function(){
var data = {
title:
[
{value:a1标题},
{value:b1标题}
],
data:
[
[
{value:好好},
{value:2015-08-24}
],
[
{value:123},
{value:hahah}
]
]
};
if(data == ''){
return;
}else{
jsontoexcelconvertor(data.data, report, data.title);
}
});
});
function jsontoexcelconvertor(jsondata, filename, showlabel) {
//先转化json
var arrdata = typeof jsondata != 'object' ? json.parse(jsondata) : jsondata;
var excel = '<table>';
//设置表头
var row = <tr>;
for (var i = 0, l = showlabel.length; i < l; i++) {
row += "<td> + showlabel[i].value + '</td>';
}
//换行
excel += row + </tr>;
//设置数据
for (var i = 0; i < arrdata.length; i++) {
var row = <tr>;
for (var index in arrdata[i]) {
var value = arrdata[i][index].value === . ? : arrdata[i][index].value;
row += '<td>' + value + '</td>';
}
excel += row + </tr>;
}
excel += </table>;
var excelfile = <html xmlns:o='urn:schemas-microsoft-com:office:office' xmlns:x='urn:schemas-microsoft-com:office:excel' xmlns='http://www.w3.org/tr/rec-html40'>;
excelfile += '<meta http-equiv="content-type" content="application/vnd.ms-excel; charset=utf-8">';
excelfile += '<meta http-equiv="content-type" content="application/vnd.ms-excel';
excelfile += '; charset=utf-8">';
excelfile += <head>;
excelfile += <!--[if gte mso 9]>;
excelfile += <xml>;
excelfile += <x:excelworkbook>;
excelfile += <x:excelworksheets>;
excelfile += <x:excelworksheet>;
excelfile += <x:name>;
excelfile += {worksheet};
excelfile += </x:name>;
excelfile += <x:worksheetoptions>;
excelfile += <x:displaygridlines/>;
excelfile += </x:worksheetoptions>;
excelfile += </x:excelworksheet>;
excelfile += </x:excelworksheets>;
excelfile += </x:excelworkbook>;
excelfile += </xml>;
excelfile += <![endif]-->;
excelfile += </head>;
excelfile += <body>;
excelfile += excel;
excelfile += </body>;
excelfile += </html>;
var uri = 'data:application/vnd.ms-excel;charset=utf-8,' + encodeuricomponent(excelfile);
var link = document.createelement(a);
link.href = uri;
link.style = visibility:hidden;
link.download = filename + .xls;
document.body.appendchild(link);
link.click();
document.body.removechild(link);
}
</script>
</head>
<body>
<input type="button" id="wwo" value="导出" />
</body>
</html>
相关推荐:
java将xml文档转换成json格式数据
如何使用javascript将url解析为json格式的方法
javascript解析json格式数据的方法示例
以上就是实例详解javascript将json格式数组下载为excel表格的详细内容。