欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  后端开发

json如何输出成excel

程序员文章站 2022-03-29 09:20:44
...
json内容如下:
[{"ZtdID":"100000","ZtdNo":"001 ","ZtdName":"1号店 ","Address":"XX市ZZ区YY路01号铺 ","Remark":"","AreaID":"01","ZtdTel":"0000-8888888","ZtdContact":"张三"},{"ZtdID":"100001","ZtdNo":"002 ","ZtdName":"1号店 ","Address":"XX市ZZ区YY路01号铺 ","Remark":"","AreaID":"01","ZtdTel":"0000-8888888","ZtdContact":"李三"}]

json解析并输出excel

回复讨论(解决方案)

先格式化成数组,然后可以用一些生成excel的类来生成,比如 http://code.google.com/p/php-excel/

phpexcel 或者其他一些轻量级的插件 生成excel

先把json转回成数据,json_decode
再生成excel,把数组中的输入放入

先格式化成数组,然后可以用一些生成excel的类来生成,比如http://code.google.com/p/php-excel/

header("Content-type: text/html; charset=utf-8");
require 'php-excel.class.php';
$json='[{"ZtdID":"100000","ZtdNo":"001 ","ZtdName":"1号店 ","Address":"XX市ZZ区YY路01号铺 ","Remark":"","AreaID":"01","ZtdTel":"0000-8888888","ZtdContact":"张三"},{"ZtdID":"100001","ZtdNo":"002 ","ZtdName":"1号店 ","Address":"XX市ZZ区YY路01号铺 ","Remark":"","AreaID":"01","ZtdTel":"0000-8888888","ZtdContact":"李三"}]';
$sr = json_decode($json, true);
echo "

";  
//var_dump($sr);
$data = $sr;

$xls = new Excel_XML('UTF-8', false, 'My Test Sheet');
$xls->addArray($data);
$xls->generateXML('my-test');
?>

to 1楼,出来成一行了?

header("Content-type: text/html; charset=utf-8");
require 'php-excel.class.php';
$json='[{"ZtdID":"100000","ZtdNo":"001 ","ZtdName":"1号店 ","Address":"XX市ZZ区YY路01号铺 ","Remark":"","AreaID":"01","ZtdTel":"0000-8888888","ZtdContact":"张三"},{"ZtdID":"100001","ZtdNo":"002 ","ZtdName":"1号店 ","Address":"XX市ZZ区YY路01号铺 ","Remark":"","AreaID":"01","ZtdTel":"0000-8888888","ZtdContact":"李三"}]';
$sr = json_decode($json, true);
echo "

";  
//var_dump($sr);

$headeAarray = array();
$headeAarray[] = array('店铺ID', '店铺编号', '店铺名称', '地址', '标记','区域ID','联系电话','联系人'); //表头二维数组
$dataArray = array();
while($row = ($sr)) {
$dataArray[] = array($row['ZtdID'], $row['ZtdNo'], $row['ZtdName'], $row['Address'], $row['Remark'],$row['AreaID'],$row['ZtdTel'],$row['ZtdContact']);
}
$xls = new Excel_XML('UTF-8', false, 'My Test Sheet');
$xls->addArray($headeAarray);
$xls->addArray($dataArray);
$xls->generateXML('my-test');
?>

这样还不行,有同学指导一下吗?

json 转换下格式,调用NPOI的dll,生成数据流放到excel里,下载下来