Heim >Backend-Entwicklung >PHP-Tutorial >json如何输出成excel
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 "
"; <br> //var_dump($sr); <br> $data = $sr; <br> <br> $xls = new Excel_XML('UTF-8', false, 'My Test Sheet'); <br> $xls->addArray($data); <br> $xls->generateXML('my-test'); <br> ?> <p class="sougouAnswer"> to 1楼,出来成一行了? </p> <p class="sougouAnswer"> <?php <br /> header("Content-type: text/html; charset=utf-8"); <br> require 'php-excel.class.php'; <br> $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":"李三"}]'; <br> $sr = json_decode($json, true); <br> echo "</p><pre class="brush:php;toolbar:false">"; <br> //var_dump($sr); <br> <br> $headeAarray = array(); <br> $headeAarray[] = array('店铺ID', '店铺编号', '店铺名称', '地址', '标记','区域ID','联系电话','联系人'); //表头二维数组 <br> $dataArray = array(); <br> while($row = ($sr)) { <br> $dataArray[] = array($row['ZtdID'], $row['ZtdNo'], $row['ZtdName'], $row['Address'], $row['Remark'],$row['AreaID'],$row['ZtdTel'],$row['ZtdContact']); <br> } <br> $xls = new Excel_XML('UTF-8', false, 'My Test Sheet'); <br> $xls->addArray($headeAarray); <br> $xls->addArray($dataArray); <br> $xls->generateXML('my-test'); <br> ?> <br> <br> 这样还不行,有同学指导一下吗? <br> <p class="sougouAnswer"> json 转换下格式,调用NPOI的dll,生成数据流放到excel里,下载下来 </p>