thinkphp3.2.3 使用PHPExcel上传并插入数据库 以及下载表格

  • thinkphp3.2.3 使用PHPExcel上传并插入数据库 以及下载表格

不多说,直接上代码。。。

加公号获取更多学习资源

 

thinkphp3.2.3 使用PHPExcel上传并插入数据库 以及下载表格_第1张图片

网上下载phpexcel 并把phpexcel 放org/util下

public function uploadExcel(){
     if (!empty($_FILES)){  
            $upload = new \Think\Upload();                      // 实例化上传类  
            $upload->maxSize   =     10485760000 ;                 // 设置附件上传大小  
            $upload->exts      =     array('xls','xlsx');       // 设置附件上传类型  
            $upload->rootPath  = './Public/excel/';             // 设置附件上传根目录  
            $upload->autoSub   = false;                         // 将自动生成以photo后面加时间的形式文件夹,关闭  
            // 上传文件  
            $info   =   $upload->upload();                                   // 上传文件  
            $exts   = $info['file']['ext'];                                 // 获取文件后缀  
            $filename = $upload->rootPath.$info['file']['savename'];        // 生成文件路径名  
            if(!$info) {                                                     // 上传错误提示错误信息  
                $this->error($upload->getError());   
            }else{                                                           // 上传成功  
                import("Org.Util.PHPExcel");                                 // 导入PHPExcel类库,因为PHPExcel没有用命名空间,只能import导入  
                $PHPExcel = new \PHPExcel();                                 // 创建PHPExcel对象,注意,不能少了\  
                if ($exts == 'xls') {                                        // 如果excel文件后缀名为.xls,导入这个类  
                    import("Org.Util.PHPExcel.Reader.Excel5");  
                    $PHPReader = new \PHPExcel_Reader_Excel5();  
                } else   
 
                    if ($exts == 'xlsx') {  
                        import("Org.Util.PHPExcel.Reader.Excel2007");  
                        $PHPReader = new \PHPExcel_Reader_Excel2007();  
 
                    }  
                // var_dump($filename);exit;
                $PHPExcel=$PHPReader->load($filename);  
                $currentSheet = $PHPExcel->getSheet(0);                      // 获取表中的第一个工作表,如果要获取第二个,把0改为1,依次类推  
                $allColumn = $currentSheet->getHighestColumn();              // 获取总列数  
                $allRow = $currentSheet->getHighestRow();                    // 获取总行数
                // echo $allRow;exit;
                $data=array();  
                for($j=1;$j<=$allRow;$j++){  
                    //从A列读取数据  
                    for($k='A';$k<=$allColumn;$k++){  
                        // 读取单元格  
                        $data[$j][]=$PHPExcel->getActiveSheet()->getCell("$k$j")->getValue();  
                    }  
                }   
               for ($i = 2; $i <= $allRow; $i++) {  
                    $data['province'] =$PHPExcel->getActiveSheet()->getCell("A" . $i)->getValue();  
                    $data['city'] =$PHPExcel->getActiveSheet()->getCell("B" .$i)->getValue();  
                    $data['vocation'] =$PHPExcel->getActiveSheet()->getCell("C" .$i)->getValue();  
                    $data['age'] = $PHPExcel->getActiveSheet()->getCell("D". $i)->getValue();  
                    $data['sex'] =$PHPExcel->getActiveSheet()->getCell("E" .$i)->getValue();
                    $data['company'] =$PHPExcel->getActiveSheet()->getCell("F" . $i)->getValue();  
                    $data['phone'] =(string)$PHPExcel->getActiveSheet()->getCell("G" . $i)->getValue();  
                    $data['other'] =$PHPExcel->getActiveSheet()->getCell("H" . $i)->getValue();  
                    $ex=M('phone')->add($data);  
              }  
              // var_dump($data_p);exit;
            if($ex){                                           
               $this->success("导入成功",U('Phone/index'));  
            }else{  
                $this->error("导入失败,原因可能是excel表中格式错误",U('Phone/add'),"5");// 提示错误  
               }  
            }  
        }else {  
          $this->display('Phone/add');   
        }      
    }  

    public function downExcel(){
            // $this->display('Phone/add');
            $data= M('phone')->select();//查出的数据
            // $data= M('User')->findAll(); //查出数据
            $name='Excelfile'; //生成的Excel文件文件名
            // $res=service('ExcelToArrary')->push($data,$name);
            import("Org.Util.PHPExcel");//引入的文件
            error_reporting(E_ALL);//错误级别
            // date_default_timezone_set('Europe/London');//时间
            $objPHPExcel = new \PHPExcel();//实例化对象
            import("Org.Util.PHPExcel.Reader.Excel5");//引入的文件
            // /*设置excel的属性*/ 可有可无
            $objPHPExcel->getProperties()->setCreator("Admin")//创建人
            ->setLastModifiedBy("A")//最后修改人
            ->setTitle("产品EXCEL导出")//标题
            ->setSubject("产品EXCEL导出")//题目
            ->setDescription("产品")//描述
            ->setKeywords("excel")//关键字
            ->setCategory("result file");//种类
                
            //第一行数据
            $objPHPExcel->setActiveSheetIndex(0)
            ->setCellValueExplicit('A1', '省份')
            ->setCellValueExplicit('B1', '城市')
            ->setCellValueExplicit('C1', '行业')
            ->setCellValueExplicit('D1', '年龄')
            ->setCellValueExplicit('E1', '性别')
            ->setCellValueExplicit('F1', '运营商')
            ->setCellValueExplicit('G1', '电话')
            ->setCellValueExplicit('H1', '备注');
    
            
            


            $objActSheet=$objPHPExcel->getActiveSheet();
            foreach($data as $k => $v){
                    $k=$k+1;
                    $num=$k+1;//数据从第二行开始录入
                $objActSheet
                //Excel的第A列,查出数组的键值,下面以此类推
                ->setCellValueExplicit('A'.$num, $v['province'],\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValueExplicit('B'.$num, $v['city'],\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValueExplicit('C'.$num, $v['vocation'],\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValueExplicit('D'.$num, $v['age'],\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValueExplicit('E'.$num, $v['sex'],\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValueExplicit('F'.$num, $v['company'],\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValueExplicit('G'.$num, $v['phone'],\PHPExcel_Cell_DataType::TYPE_STRING)
                ->setCellValueExplicit('H'.$num, $v['other'],\PHPExcel_Cell_DataType::TYPE_STRING);
              
                //设置单元格宽度自动 以下设置宽度可有可无
                // $objActSheet->getColumnDimension('A')->setAutoSize(true);
                // $objActSheet->getColumnDimension('B')->setAutoSize(true);
                // $objActSheet->getColumnDimension('C')->setAutoSize(true);
                // $objActSheet->getColumnDimension('D')->setAutoSize(true);
                // $objActSheet->getColumnDimension('E')->setAutoSize(true);
                // $objActSheet->getColumnDimension('F')->setAutoSize(true);
                // $objActSheet->getColumnDimension('G')->setAutoSize(true);
                // $objActSheet->getColumnDimension('H')->setAutoSize(true);
                $objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(8);
                $objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(18);
                $objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(35);
                $objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(10);
                $objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(10);
                $objPHPExcel->getActiveSheet()->getColumnDimension('F')->setWidth(12);
                $objPHPExcel->getActiveSheet()->getColumnDimension('G')->setWidth(20);
                $objPHPExcel->getActiveSheet()->getColumnDimension('H')->setWidth(20);

            }
            $objPHPExcel->setActiveSheetIndex(0);
            header('Content-Type: application/vnd.ms-excel');
            header('Content-Disposition: attachment;filename="'.date('YmdHis').'.xls"');
            header('Cache-Control: max-age=0');
            $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
            $objWriter->save('php://output');
            exit;
    }

 

你可能感兴趣的:(PHP,mysql)