美文网首页WEB前端程序开发程序员
thinkphp3.2 phpExcel的导出示例教程

thinkphp3.2 phpExcel的导出示例教程

作者: 前端丶米店 | 来源:发表于2017-04-11 14:09 被阅读0次

    phpExcel下载 && 导入传送门

    public function getExcel(){
            //导入excel方式一
                vendor('PHPExcel');
                $opjPHPExcel    =   new \PHPExcel();
                $opjPHPExcel ->getProperties()->setCreated("li")                    //创建人
                ->setLastModifiedBy("li")                                       //最后修改人
                ->setTitle("兑换记录")   //标题
                ->setSubject("Office 2007 XLSX Test Document")   //题目
                ->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")   //描述
                ->setKeywords("office 2007 openxml php")   //关键字
                ->setCategory("Test result file");    //种类
        
                //设置宽度
                $opjPHPExcel->getActiveSheet()->getColumnDimension("A")->setWidth(22);
                $opjPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(20);
                $opjPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(26);
                $opjPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(26);
                $opjPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(40);
                $opjPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(20);
                $opjPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
    
                
                //设置align为center
                $opjPHPExcel->getActiveSheet()->getStyle('A')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $opjPHPExcel->getActiveSheet()->getStyle('B')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $opjPHPExcel->getActiveSheet()->getStyle('C')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $opjPHPExcel->getActiveSheet()->getStyle('D')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $opjPHPExcel->getActiveSheet()->getStyle('E')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $opjPHPExcel->getActiveSheet()->getStyle('F')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                $opjPHPExcel->getActiveSheet()->getStyle('G')->getAlignment()->setHorizontal(\PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                
                $opjPHPExcel->setActiveSheetIndex(0)    //设置当前sheet
                ->setCellValue('A1','真实姓名')
                ->setCellValue('B1','手机号码')
                ->setCellValue('C1','地址')
                ->setCellValue('D1','昵称')
                ->setCellValue('E1','物品昵称')
                ->setCellValue('F1','数量')
                ->setCellValue('G1','时间');
                 
    
                //写了6行数据
                for($i=0;$i<5;$i++){
                    $opjPHPExcel->setActiveSheetIndex(0)
                    ->setCellValue('A'.($i+2),$i)  //自己设置值,我都设置成  $i  了
                    ->setCellValue('B'.($i+2),$i)
                    ->setCellValue('C'.($i+2), $i)
                    ->setCellValue('D'.($i+2), $i)
                    ->setCellValue('E'.($i+2), $i)
                    ->setCellValue('F'.($i+2), $i)
                    ->setCellValue('G'.($i+2), $i);
                }
                
                //  sheet命名
                $excel_name  ="记录".time();
                $opjPHPExcel->getActiveSheet()->setTitle($excel_name);
                
                // excel头参数
                ob_end_clean();
                header('Content-Type: application/vnd.ms-excel;');
                header('Content-Disposition: attachment;filename="'.$excel_name.'.xlsx"');  //日期为文件名后缀
                header('Cache-Control: max-age=0');
                
                $objWriter = \PHPExcel_IOFactory::createWriter($opjPHPExcel, 'Excel2007');  //excel5为xls格式,excel2007为xlsx格式
                
                $objWriter->save('php://output');exit();
            
        }
    

    访问上面的方法就会自动下载了,里面的数据自己填进去就好了,注释写的很详细

    <h1>本文为原创文章,转载请注明出处</h1>
    <h1>觉得本文对你有帮助</h1>
    关注简书mid米店,持续分享中。。。

    相关文章

      网友评论

        本文标题:thinkphp3.2 phpExcel的导出示例教程

        本文链接:https://www.haomeiwen.com/subject/layyattx.html