Blogger Information
Blog 8
fans 0
comment 1
visits 10296
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
thinkphp5.0利用PHPExcel导出excel
Moments的博客
Original
1472 people have browsed it
  1. 去PHPExcel官网下载,然后将解压后的classes里面的内容全部放到Thinkphp框架里的extend下面,如下图:

    QQ图片20170930100530.png

  2. 然后在你需要导出的地方添加如下代码:


    public function exportExcel()

    {

           $path = dirname(__FILE__); //找到当前脚本所在路径

           Loader::import('PHPExcel.php'); //加载所需的类文件,必须引入 use think\Loader;命名空间,否则loader无法加载

           Loader::import('PHPExcel.Reader.Excel2007');

           $PHPExcel = new \PHPExcel();

           $list=Db::name('user')->select();

           foreach ($list as $key => $value)

           {

                  $i=$key+1;//表格是从1开始的

                   $PHPExcel->getActiveSheet()->setCellValue('A'.$i,  $value['name']);//这里是设置A1单元格的内容

                  $PHPExcel->getActiveSheet()->setCellValue('B'.$i,  $value['id']);////这里是设置B1单元格的内容

                  $PHPExcel->getActiveSheet()->setCellValue('C'.$i,  $value['emp_no']);

                   $PHPExcel->getActiveSheet()->setCellValue('D'.$i,  $value['password']);

           }

           $PHPWriter = \PHPExcel_IOFactory::createWriter($PHPExcel,"Excel2007");

           header('Content-Disposition: attachment;filename="testExport.xlsx"');

           header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');

           $PHPWriter->save("php://output"); //表示在$path路径下面生成demo.xlsx文件

      }


Statement of this Website
The copyright of this blog article belongs to the blogger. Please specify the address when reprinting! If there is any infringement or violation of the law, please contact admin@php.cn Report processing!
All comments Speak rationally on civilized internet, please comply with News Comment Service Agreement
1 comments
波波alber1986 2018-05-22 22:29:53
跟这个一样吗http://www.sucaihuo.com/php/3279.html
1 floor
Author's latest blog post