Thinkphp excel 导出和下载

首先用到 PHPexcel,官网地址 :安装方法:composer require phpoffice/phpexcel

[[email protected]]# composer require phpoffice/phpexcel
Warning: This development build of composer is over 60 days old. It is recommended to update it by running "/usr/local/bin/composer self-update" to get the latest version.
Using version ^1.8 for phpoffice/phpexcel
./composer.json has been updated
Loading composer repositories with package information
Updating dependencies (including require-dev)
  - Installing phpoffice/phpexcel (1.8.1)
    Downloading: 100%         

Writing lock file
Generating autoload files


use PHPExcel;
use PHPExcel_IOFactory;


public function export(){
        $objPHPExcel = new PHPExcel();
        // Set document properties
                    ->setKeywords("文件关键词 貌似是用空格隔开")
                    ->setCategory("Test result file");

        // 添加第一栏数据
                    ->setCellValue('A1', '优惠券代码')
                    ->setCellValue('B1', '优惠券规则组')
                    ->setCellValue('C1', '结束时间')
                    ->setCellValue('D1', '使用规则')
                    ->setCellValue('E1', '绑定ID(课程或系列)')
                    ->setCellValue('F1', '使用范围');
        //二维数组 从数据库拿出来
        $excelData = D('coupons')->limit(10)->select();
        for ($i=2;$i<count($excelData);$i++){
                        ->setCellValue('A'.$i, $excelData[$i]['code'])
                        ->setCellValue('B'.$i, $excelData[$i]['group'])
                        ->setCellValue('C'.$i, $excelData[$i]['indate'])
                        ->setCellValue('D'.$i, $excelData[$i]['rule'])
                        ->setCellValue('E'.$i, $excelData[$i]['bind'])
                        ->setCellValue('F'.$i, $excelData[$i]['useRange']);

        // 设置下边的文章标题 我也不知道叫啥
        // 设置打开文件浏览的页面(0为第一页)
        // 保存为Excel 2007 文件
        $callStartTime = microtime(true);
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save(str_replace('.php', '.xls', '优惠券'));
        $callEndTime = microtime(true);
        $callTime = $callEndTime - $callStartTime;
        // Redirect output to a client’s web browser (Excel2007)
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="文件名称.xlsx"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');
        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0
        $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel2007');


文章来源: Thinkphp excel 导出和下载

人吐槽 人点赞



用户名: 密码:
验证码: 匿名发表


查看评论:Thinkphp excel 导出和下载