首頁 >後端開發 >php教程 >PHP如何匯出Excel

PHP如何匯出Excel

墨辰丷
墨辰丷原創
2018-06-05 13:39:203548瀏覽

這篇文章主要介紹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