一聚教程网:一个值得你收藏的教程网站

热门教程

thinkphp利用phpexcel实现导入excel

时间:2022-06-25 02:31:26 编辑:袖梨 来源:一聚教程网

 1.上传excel文件。

2.读取excel中的数据到数据库

那么我们就从第一步开始

上传excel代码:

html部分:

 代码如下 复制代码

   
   
    上传EXCEL文件
   
   
   


       
       
   

   
   

执行代码:

 代码如下 复制代码

        class IndexAction extends Action {
        public function index(){
            $this->display();
        }
        public function upload()
        {
            import("ORG.Net.UploadFile");
            if(!empty($_FILES['photo']['name']))
            {
                $upload = new UploadFile();
                $upload->maxSize  = 93145728 ;
                $upload->saveRule =time;
                $upload->allowExts  = array('xls','xlsx');
                $upload->savePath =  './Uploads/';
                if(!$upload->upload())
                {
                    $this->error($upload->getErrorMsg());
                }else{
                    $info =  $upload->getUploadFileInfo();
                } www.111com.net

                //下面就是我们说的第二步了
                import("ORG.Excel.PHPExcel");
                $file_name=$info[0]['savepath'].$info[0]['savename'];
                $objReader = PHPExcel_IOFactory::createReader('Excel5');
                $objPHPExcel = $objReader->load($file_name,$encode='utf-8');
                $sheet = $objPHPExcel->getSheet(0);
                $highestRow = $sheet->getHighestRow(); // 取得总行数
                $highestColumn = $sheet->getHighestColumn(); // 取得总列数
                for($i=2;$i<=$highestRow;$i++)
                {
                    $name = $objPHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
                    $old = $objPHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
                    $sex = $objPHPExcel->getActiveSheet()->getCell("C".$i)->getValue();
                    $ID    = $objPHPExcel->getActiveSheet()->getCell("D".$i)->getValue();
                    $guoji = $objPHPExcel->getActiveSheet()->getCell("E".$i)->getValue();
                    $minzu = $objPHPExcel->getActiveSheet()->getCell("F".$i)->getValue();
                    $address = $objPHPExcel->getActiveSheet()->getCell("G".$i)->getValue();
                }
                echo $name."//".$old."//".$sex."//".$ID."//".$guoji."//".$minzu."//".$address;exit;
            }else
            {
                $this->error("请选择上传的文件");
            }       
        }
    }

效果图:

 

thinkphp利用phpexcel实现导入excel

导入数据库和平常的入库代码一样。

热门栏目