Blogger Information
Blog 11
fans 1
comment 0
visits 17419
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
thinkphp5使用execl 导出文件
姑苏城外
Original
1235 people have browsed it
//把PHPExcel放在vendor目录中
//没有的小伙伴在
//链接:https://pan.baidu.com/s/16NzArZFIXYExFqKnbiNo9Q 
//提取码:n43l
在控制器里
 //导出
                $path = dirname(__FILE__); //找到当前脚本所在路径
                vendor("PHPExcel.PHPExcel.PHPExcel");
                vendor("PHPExcel.PHPExcel.Writer.IWriter");
                vendor("PHPExcel.PHPExcel.Writer.Abstract");
                vendor("PHPExcel.PHPExcel.Writer.Excel5");
                vendor("PHPExcel.PHPExcel.Writer.Excel2007");
                vendor("PHPExcel.PHPExcel.IOFactory");
                $objPHPExcel = new \PHPExcel();
                $objWriter = new \PHPExcel_Writer_Excel5($objPHPExcel);
                $objWriter = new \PHPExcel_Writer_Excel2007($objPHPExcel);
                //垂直居中
                // $objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
                // $objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
                $letter=array('A','B','C','D','E','F','G','H','I','J','K','L','M','N','O','P','Q','R','S','T','U','V','W','X','Y','Z');
               //表头
                $list_a=Array([0] => 项目名称,[1] => ***时间,[2] => 数量);
                //数据
                $lists=Array(
          [0] => Array
            (
                [0] => ee
                [1] => Array
                    (
                        [0] => 2018-09-04
                        [1] => 2018-09-29
                    )

                [2] => Array
                    (
                        [0] => 2000
                        [1] => 1500
                    )
        )
    [1] => Array
        (
            [0] => gg
            [1] => Array
                (
                    [0] => 2018-10-15
                    [1] => 2018-10-15
                )

            [2] => Array
                (
                    [0] => 5000
                    [1] => 5000
                )

        )

)

                $count = count($list_a);
                for ($i=1; $i <=$count; $i++) { 
                    $objPHPExcel->setActiveSheetIndex(0)->setCellValue($letter[$i-1].'1',$list_a[$i-1]);
                }
                $counts = count($lists);
                foreach ($lists as $key_ks => $value_ks) {
                    foreach ($value_ks as $k_k => $v_v) {
                        
                            $vs=$key_ks+2;
                            $objPHPExcel->getActiveSheet()->getColumnDimension($letter[$k_k])->setWidth(30);
                            $as=count($v_v);
                            if($as=='1'){
                                if(isset($v_v[0])){
                                     $objPHPExcel->getActiveSheet()->setCellValue($letter[$k_k].$vs,$v_v[0]);
                                }else{
                                    $objPHPExcel->getActiveSheet()->setCellValue($letter[$k_k].$vs,$v_v);
                                }
                                
                            }else{
                                // foreach ($v_v as $k_v => $vv) {
                                  // $k_v=$k_v+2;
                                    // $abc = $vv."\r\n";
                                    // dump($v_v);
                                    $count  = count($v_v);
                                    for($i = 0; $i< $count; $i++){
                                        if($i+1 < $count){
                                            $abc = $v_v[$i]."\r\n".$v_v[$i+1]."\r\n";
                                        }
                                    }
                                    //dump($abc);
                                   $objPHPExcel->getActiveSheet()->setCellValue($letter[$k_k].$vs,$abc);
                                // }
                                // $aaa=$v_v;
                                // $objPHPExcel->getActiveSheet()->setCellValue($letter[$k_k].$vs,$aaa);
                               
                                
                                
                            }
                           
                    }
                   // die;

                
                }
                
               
                $objPHPExcel->getActiveSheet()->setTitle('productaccess');      //设置sheet的名称
                $objPHPExcel->setActiveSheetIndex(0);                   //设置sheet的起始位置
                $objPHPExcel->getProperties()->setCreator("Maarten Balliauw");
                ob_clean();  
                $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');   //通过PHPExcel_IOFactory的写函数将上面数据写出来
                
                $PHPWriter = \PHPExcel_IOFactory::createWriter( $objPHPExcel,"Excel2007");
                    
                header('Content-Disposition: attachment;filename="设备列表.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
0 comments
Author's latest blog post