Blogger Information
Blog 8
fans 0
comment 1
visits 10260
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
thinkphp5.0利用PHPExcel导出pdf格式
Moments的博客
Original
1103 people have browsed it

1.去PHPExcel官网下载,然后将解压后的classes里面的内容全部放到Thinkphp框架里的extend下面,如下图:

            QQ图片20170930100530.png

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

    public function exportPDF()

    {

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

           Loader::import('PHPExcel.php');

           Loader::import('PHPExcel.IOFactory.php');

           Loader::import('PHPExcel.Writer.PDF.php');

           Loader::import('PHPExcel.Writer.IWriter.php');

           $objPHPExcel = new \PHPExcel();

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

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

           {

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

                   $objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$i,  $value['name'])

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

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

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

          }

           // 下载一个pdf文件

           $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'PDF');

           header("Content-type:application/vnd.ms-excel;charset=UTF-8");

           header('Content-Type: application/pdf');

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

          header('Cache-Control: max-age=0');

           $objWriter->save('php://output');

    }

3.如未成功,考虑是PHPExcel类的问题,因为版本不同,会导致pdf导出是否正确,不过不建议使用PHPExcel导出pdf格式文件,会出现很多小问题比如样式,乱码等

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
0 comments
Author's latest blog post
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!