phpexcel 导出Demo

Posted z329982468

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了phpexcel 导出Demo相关的知识,希望对你有一定的参考价值。

// 导出excel用户信息
    public function export()
        $model = D('Common/UserInfo');
        $data = $model -> searchExport();
        $date = date("Y-m-d",time());
        $filename="用户信息表".$date;
        if($data)
            $phpexcel = new \\PHPExcel();
            $phpexcel->getActiveSheet()->setTitle($filename);
            $phpexcel->getActiveSheet()
                  ->setCellValue('A1','序号')
                  ->setCellValue('B1','手机号')
                  ->setCellValue('C1','昵称')
                  ->setCellValue('D1','来源')
                  ->setCellValue('E1','UID')
                  ->setCellValue('F1','微博昵称')
                  ->setCellValue('G1','微博UID')
                  ->setCellValue('H1','积分总数')
                  ->setCellValue('I1','用户等级');
            $i =   2;
            foreach ( $data as $k => $val ) 
                $phpexcel->getActiveSheet() 
                         ->setCellValue('A'.$i, $k+1)
                         ->setCellValue('B'.$i, $val['tel'])
                         ->setCellValue('C'.$i, $val['Nick'])
                         ->setCellValue('D'.$i, $val['from'])
                         ->setCellValue('E'.$i, $val['uid'])
                         ->setCellValue('F'.$i, $val['blog'])  
                         ->setCellValue('G'.$i, $val['blog_uid'])  
                         ->setCellValue('H'.$i, $val['integral'])     
                         ->setCellValue('I'.$i, $val['level']);
                $i++;                
    
            
    
            $obj      = new \\PHPExcel_IOFactory();
            $obj_Writer = $obj->createWriter($phpexcel,'Excel5');
            //设置header
            header("Content-Type: application/force-download"); 
            header("Content-Type: application/octet-stream"); 
            header("Content-Type: application/download"); 
            header('Content-Disposition:inline;filename="'.$filename.'.xls"'); 
            header("Content-Transfer-Encoding: binary"); 
            header("Last-Modified: " . gmdate("D, d M Y H:i:s") . " GMT"); 
            header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 
            header("Pragma: no-cache"); 
            $obj_Writer->save('php://output');//输出
        else
            $this -> error('系统错误,请稍后重试~');
        
    

以上是关于phpexcel 导出Demo的主要内容,如果未能解决你的问题,请参考以下文章

phpexcel导出阿里云图片

phpexcel 导出方法

PHPExcel导入导出常用方法总结

使用PHPExcel导出excel备忘录

PHPExcel导出xls

PHPExcel导出导入