PHPExcel TP5导入功能

PHPexcel tp5导入 格式必须是xlsx 如果格式是其他的需要引入不同的类
e x t e n s i o n = s t r t o l o w e r ( p a t h i n f o ( extension = strtolower( pathinfo( extension=strtolower(pathinfo(fileName, PATHINFO_EXTENSION) );
if ($extension ==‘xlsx’) {
$objReader = new PHPExcel_Reader_Excel2007();
$objExcel = o b j R e a d e r − > l o a d ( objReader ->load( objReader>load(file);
} else if ($extension ‘xls’) {
$objReader = new PHPExcel_Reader_Excel5();
$objExcel = o b j R e a d e r − > l o a d ( objReader ->load( objReader>load(file);
} else if ($extension
’csv’) {
$PHPReader = new PHPExcel_Reader_CSV();

前端代码

 public function drexcel(){
        $file = $_FILES['drexcel']['tmp_name'][0];
        $data = $this->import_excel($file);

        if(db('tmall')->insertAll($data)){
            $this->success('添加成功','index');
        }else{
            $this->error('添加失败');
        }
        // var_dump($data);
       
    }
    private function import_excel($file){
        $type=pathinfo($file);
        $type = strtolower($type["extension"]);
        $type=$type==='csv' ? $type : 'Excel5';
        ini_set('max_execution_time', '0');
        //引入excel包
        Loader::import('PHPExcel.Classes.PHPExcel');
        Loader::import('PHPExcel.Classes.PHPExcel.IOFactory');
        //判断格式
        // $objReader = \PHPExcel_IOFactory::createReader($type);
        $objReader = new \PHPExcel_Reader_Excel2007();
        $objPHPExcel = $objReader->load($file);
        $sheet = $objPHPExcel ->getSheet(0);      
        // / 取得总行数
        $highestRow = $sheet->getHighestRow();
        // 取得总列数
        $highestColumn = $sheet->getHighestColumn();
        for($row=1;$row<=$highestRow;$row++){
            $ListData = $sheet->rangeToArray('A'.$row.':'.$highestColumn.$row,NULL,TRUE,FALSE);
            $ListData=$ListData[0];
                if($row!=1){
                    $rowData[]=[
                        'id'=>$ListData['0'],
                        'city'=>$ListData['1'],
                        'xsname'=>$ListData['2'],
                        'cdl'=>$ListData['3'],
                        'zxl'=>$ListData['4'],
                        'cdyj'=>$ListData['5'],
                        // 'date'=>$ListData['6'],
                        'date'=>date('Y-m-d'),
                        'user'=>$ListData['7'],
                    ];
                }   
        }
        return $rowData;
    }

第二种方法

public function addexcel() {
   //引入类
   include_once('extend/PHPExcel/Classes/PHPExcel.PHP');
   //实例化类
   $excel_class= new \PHPExcel();
   //获取上传文件信息
   $file = request()->file('addexcel');
   //1000b=1kb  大小不超过10mb
   $info = $file->validate(['size'=>10000000 ,'ext'=>'xlsx,xls,csv'])->move(ROOT_PATH . 'public' . DS . 'excel');
   if($info){
       //获取文件名
       $excel_path=$info->getSaveName(); //'20190218\55c1e5db867c4fffa0ab4cb9a45bf810.xlsx'
       //获取上传文件地址--
       $file_name = ROOT_PATH . 'public' . DS . 'excel' . DS . $excel_path;//D:\wamp\www\admin1\public\excel\20190218\c4a5a982b1d5c7a8d86988ddbf76a491.xlsx
       $objReader =\PHPExcel_IOFactory::createReader('Excel2007');
       //加载文件内容 utf8geshi
       $obj_PHPExcel =$objReader->load($file_name, $encode = 'utf-8');
       // echo "
";
       //转换为数组格式
       $excel_array=$obj_PHPExcel->getsheet(0)->toArray();
       //输出数组中的当前元素和下一个元素的值,然后把数组的内部指针重置到数组中的第一个元素
       $arr  = reset($excel_array);
       //销毁标题
       unset($excel_array[0]); 
       $data = [];  
       $i=0;  
       foreach ($excel_array as $k => $v) {
           $data[$k]['id'] = $v[0];
           $data[$k]['city'] = $v[1]; 
           $data[$k]['xsname'] = $v[2]; 
           $data[$k]['cdl'] = $v[3]; 
           $data[$k]['zxl'] = $v[4]; 
           $data[$k]['cdyj'] = $v[5]; 
           $data[$k]['date'] = $v[6]; 
           $data[$k]['user'] = $v[7]; 
           $i++; 
       }
       $res = db('tmall')->insertAll($data);
       if($res){
           $this->success('添加成功','index');
       }else{
           $this->error('添加失败');
       }
   }else{
       $this->error($file->getError());
   }
}

你可能感兴趣的:(PHP)