>백엔드 개발 >PHP 튜토리얼 >json如何输出成excel

json如何输出成excel

WBOY
WBOY원래의
2016-06-23 14:20:131014검색

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>
성명:
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.