Home  >  Article  >  Backend Development  >  How to export Excel with PHP

How to export Excel with PHP

墨辰丷
墨辰丷Original
2018-06-05 13:39:203533browse

This article mainly introduces how to export Excel with PHP. Interested friends can refer to it. I hope it will be helpful to everyone.

The code is as follows:


<?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;


Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

php method to implement a simple probability

Methods and simple examples of while loop control in php

PHP method to implement cross-domain operations


# #

The above is the detailed content of How to export Excel with PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn