首页 >后端开发 >php教程 >PHP如何导出Excel

PHP如何导出Excel

墨辰丷
墨辰丷原创
2018-06-05 13:39:203546浏览

本篇文章主要介绍PHP如何导出Excel,感兴趣的朋友参考下,希望对大家有所帮助。

代码如下:


<?php
error_reporting(E_ALL);
date_default_timezone_set(&#39;Asia/Shanghai&#39;);
require_once &#39;./Classes/PHPExcel.php&#39;;
$data=array(
  0=>array(
    &#39;id&#39;=>1001,

    &#39;username&#39;=>&#39;张飞&#39;,
    &#39;password&#39;=>&#39;123456&#39;,
    &#39;address&#39;=>&#39;三国时高老庄250巷101室&#39;
  ),
  1=>array(
    &#39;id&#39;=>1002,
    &#39;username&#39;=>&#39;关羽&#39;,
    &#39;password&#39;=>&#39;123456&#39;,
    &#39;address&#39;=>&#39;三国时花果山&#39;
  ),
  2=>array(
    &#39;id&#39;=>1003,
    &#39;username&#39;=>&#39;曹操&#39;,
    &#39;password&#39;=>&#39;123456&#39;,
    &#39;address&#39;=>&#39;延安西路2055弄3号&#39;
  ),
  3=>array(
    &#39;id&#39;=>1004,
    &#39;username&#39;=>&#39;刘备&#39;,
    &#39;password&#39;=>&#39;654321&#39;,
    &#39;address&#39;=>&#39;愚园路188号3309室&#39;
  )
);
$objPHPExcel=new PHPExcel();
$objPHPExcel->getProperties()->setCreator(&#39;http://www.jb51.net&#39;)
               ->setLastModifiedBy(&#39;http://www.jb51.net&#39;)
               ->setTitle(&#39;Office 2007 XLSX Document&#39;)
               ->setSubject(&#39;Office 2007 XLSX Document&#39;)
               ->setDescription(&#39;Document for Office 2007 XLSX, generated using PHP classes.&#39;)
               ->setKeywords(&#39;office 2007 openxml php&#39;)

               ->setCategory(&#39;Result file&#39;);

$objPHPExcel->setActiveSheetIndex(0)

      ->setCellValue(&#39;A1&#39;,&#39;ID&#39;)

      ->setCellValue(&#39;B1&#39;,&#39;用户名&#39;)

      ->setCellValue(&#39;C1&#39;,&#39;密码&#39;)

      ->setCellValue(&#39;D1&#39;,&#39;地址&#39;);

$i=2;     

foreach($data as $k=>$v){

  $objPHPExcel->setActiveSheetIndex(0)
      ->setCellValue(&#39;A&#39;.$i,$v[&#39;id&#39;])
      ->setCellValue(&#39;B&#39;.$i,$v[&#39;username&#39;])
      ->setCellValue(&#39;C&#39;.$i,$v[&#39;password&#39;])
      ->setCellValue(&#39;D&#39;.$i,$v[&#39;address&#39;]);

  $i++;

}
$objPHPExcel->getActiveSheet()->setTitle(&#39;三年级2班&#39;);

$objPHPExcel->setActiveSheetIndex(0);
$filename=urlencode(&#39;学生信息统计表&#39;).&#39;_&#39;.date(&#39;Y-m-dHis&#39;);
 
//生成xlsx文件
/*
header(&#39;Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet&#39;);
header(&#39;Content-Disposition: attachment;filename="&#39;.$filename.&#39;.xlsx"&#39;);
header(&#39;Cache-Control: max-age=0&#39;);
$objWriter=PHPExcel_IOFactory::createWriter($objPHPExcel,&#39;Excel2007&#39;);
*/

//生成xls文件
header(&#39;Content-Type: application/vnd.ms-excel&#39;);
header(&#39;Content-Disposition: attachment;filename="&#39;.$filename.&#39;.xls"&#39;);
header(&#39;Cache-Control: max-age=0&#39;);
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, &#39;Excel5&#39;);
$objWriter->save(&#39;php://output&#39;);
exit;


总结:以上就是本篇文的全部内容,希望能对大家的学习有所帮助。

相关推荐:

php实现一段简单概率的方法

php中while循环控制的的方法及简单实例

PHP实现跨域操作的方法


以上是PHP如何导出Excel的详细内容。更多信息请关注PHP中文网其他相关文章!

声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn