Thinkphp5-phpexcel Exporting data

Source: Internet
Author: User

Php-excel

tags (space delimited): PHP

Class Library: https://codeload.github.com/PHPOffice/PHPExcel/zip/1.8
Basic implementation of PHP export Excel tabular data

Implementation steps:

1 把文件夹放到extend目录下2 import助手函数导入类3 设置excel表头4 填充数据

Precautions:

1 数据从第二行开始填充
ThinkPHP5 Example:
# Common method Public Function Phpexcel ($title, $list, $fileName) {import (' PHPExcel.Classes.PHPExcel ');    Import (' PHPExcel.Classes.PHPExcel.IOFactory.PHPExcel_IOFactory ');    $PHPExcel = new \phpexcel;    $PHPSheet = $PHPExcel->getactivesheet ();                 foreach ($list as $k + $v) {# code ... foreach ($title as $key + = $value) {if ($k = = 0) { $PHPSheet->setcellvalue ($key. '            1 ', End ($value));            } $i = $k + 2;        $PHPSheet->setcellvalue ($key. $i, $v [Reset ($value)]);    }} $PHPWriter = \phpexcel_iofactory::createwriter ($PHPExcel, "Excel2007");    Header ("Pragma:public");    Header ("expires:0");    Header ("Cache-control:must-revalidate, Post-check=0, pre-check=0");    Header ("Content-type:application/force-download");    Header ("Content-type:application/vnd.ms-execl");    Header ("Content-type:application/octet-stream");    Header ("Content-type:application/download");; Header (' Content-disposition:attachmenT;filename= '. $fileName.    Xlsx ');    Header ("Content-transfer-encoding:binary"); $PHPWriter->save ("Php://output");} # Call Method: Public Function Main () {$title = [' A ' = = = ' id ', ' id '], ' B ' = = [' nickname ', ' nickname '], ' C ' = = [' Phone ', ' mobile number '], ' D ' = [' Avatar ', ' Avatar '], ' E ' + = [' profile ', ' personal signature '], ' F ' = [' Toke    n ', ' user uniquely identified '], ' G ' = = [' Sex ', ' gender '], ' H ' = = ' [' Status ', ' state '], ' I ' = [' to_host ', ' Organizer ID ']    ];    $field = ' id, nickname, phone, Avatar, profile, token, sex, status, To_host ';        $list = db::table (' T_user ')->field ($field)->limit ()->select ();    $fileName = ' Test '. Date (' y-m-d '); $this->excel ($title, $list, $fileName);}

Thinkphp5-phpexcel Export Data

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.