PHPExcel导入

<form id="upload" action="__URL__/upload/" method="post" enctype="multipart/form-data">
                <input type="file" name="file" id="file" style='float:left'><input type="submit" name="submit" value="导入" />
                
            </form>

 

    public function upload(){
        header("content-type:text/html;charset=utf-8");
        $files = $_FILES['file'];

        if (!empty($files)) {
        
            // exl格式,否则重新上传
            // if($files['type'] !='application/octet-stream'){
            //     $this->error('不是Excel文件,请重新上传');    
            // }

            // 上传
            $upload = new \Think\Upload();// 实例化上传类
            $upload->maxSize   =     10000000000 ;// 设置附件上传大小
            $upload->exts      =     array('csv');// 设置附件上传类型
            $upload->rootPath  =     './uploads/'; // 设置附件上传根目录
            $upload->savePath  =     'excel/'; // 设置附件上传(子)目录
            $upload->subName   =     array('date', 'Ym');
            $upload->subName   =     '';
            // 上传文件  
            $info   =   $upload->upload();

            $file_name =  $upload->rootPath.$info['file']['savepath'].$info['file']['savename'];

            
            //调用导入方法
            $exl = $this->importExcel($file_name);
            // print_r($exl);die;

            $save_num = 0;
            $success = 0;
            $error = 0;
            $count_num = count($exl);
            
            foreach ($exl as $key => $val){
                   // $title = iconv('gbk', 'utf-8', $val['title']);
                   //      $content = iconv('gbk', 'utf-8', $val['content']);
                if(M('user_task_comment')->add(array('title' => $val['title'], 'content' => $val['content']),array(),true)){
                     $success++;
                        }else {
                     $error++;
                        }
                
            }
        
            $this->success('总共:'.$count_num.'条数据,成功:'.$success.'失败:'.$error.'条','',5);
        
    
    }
}

 

//         /**
//        *
//        * 导入Excel文件
//        */
      public function importExcel($file) {
         $M = new \Think\Model();
        ob_end_clean();//清除缓冲区,避免乱码 
        header("content-type:text/html;charset=utf-8");
      
        //引入PHPExcel类
        Vendor('Classes.PHPExcel');
      

       // 判断文件是什么格式
       $type = pathinfo($file); 
       $type = strtolower($type["extension"]);
       $type=$type==='csv' ? $type : 'Excel5';
       ini_set('max_execution_time', '0');

        // 判断使用哪种格式
        $objReader = \PHPExcel_IOFactory::createReader($type);
     

        //读取Excel文件
        $PHPExcel = $objReader->load($file);
       
        //读取excel文件中的第一个工作表
        $sheet = $PHPExcel->getSheet(0);
        
        //取得最大的列号
        $allColumn = $sheet->getHighestColumn();
        //取得最大的行号
        $allRow = $sheet->getHighestRow();
        //从第二行开始插入,第一行是列名
        // for ($currentRow = 2; $currentRow <= $allRow; $currentRow++) {
        //   //获取B列的值
        //   $title = $PHPExcel->getActiveSheet()->getCell("A" . $currentRow)->getValue();
          
        //   //获取C列的值
        //   $content = $PHPExcel->getActiveSheet()->getCell("B" . $currentRow)->getValue();
         
        //   //获取D列的值
        //   // $count = $PHPExcel->getActiveSheet()->getCell("D" . $currentRow)->getValue();
      
        //   $m = M('user_task_comment');
        //   $num = $m->add(array('title' => $title, 'content' => $content),array(),true);
        //   // echo $M->getLastSql();
          
        // }
   // die;
        // if ($num > 0) {
        //      $this->success('导入成功');
        //     }else{
        //         $this->error('导入失败');
        //     }


            //循环读取excel文件,读取一条,插入一条
           $data=array();
           //从第一行开始读取数据
           for($i=2;$i<$allRow;$i++){  
                $data[$i]['title'] = $PHPExcel->getActiveSheet()->getCell("A".$i)->getValue();
                $data[$i]['content'] = $PHPExcel->getActiveSheet()->getCell("B".$i)->getValue();
            }
           return array_merge($data);

      }
      

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值