laravel框架利用phpExcel实现Excel数据的导入导出

所需要的phpexcel的类在vendor中

laravel框架利用phpExcel实现Excel数据的导入导出_第1张图片

导入excel 

前端:

后台: 

    public function upload(Request $request){
        //接收前台文件
        $ex = $_FILES['file'];
        //重设置文件名
        $filename = time().substr($ex['name'],stripos($ex['name'],'.'));
        $path = 'upload/excel/'.$filename;//设置移动路径
        move_uploaded_file($ex['tmp_name'],$path);
        //表用函数方法 返回数组
        $exfn = $this->_readExcel($path);
//      $this->redirect('input');
    }

    //创建一个读取excel数据,可用于入库
    public function _readExcel($path)
    {
    	//引用PHPexcel 类
    	$objPHPExcel = new \PHPExcel();
        $type = 'Excel2007';//设置为Excel5代表支持2003或以下版本,Excel2007代表2007版
        $xlsReader = \PHPExcel_IOFactory::createReader($type);
        $xlsReader->setReadDataOnly(true);
        $xlsReader->setLoadSheetsOnly(true);
        $Sheets = $xlsReader->load($path);
        //开始读取上传到服务器中的Excel文件,返回一个二维数组
        $dataArray = $Sheets->getSheet(0)->toArray();
        unset($dataArray[0]);
        $newArr = [];
        foreach($dataArray as $k=>$v){
        	$newArr[] = ['userid'=>$v[0],'name'=>$v[1]];
        }
        
        return $newArr;
    }

导出excel

public function reglist(Request $request, $date){
       $data = 数据库查询出来的数据
       $columnName = array('用户ID');
       $column = array('UserId');
       $fileName = '注册用户清单'.'.xls';
       $this->export($columnName,$column,$data,$fileName);
}


public function export($columnName,$column,$data,$fileName = 'test.xls'){
        $cell = 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');
        $data = $this->objectToArray($data);
        $count = count($column);
        $objPHPExcel = new \PHPExcel();
        $objPHPExcel->setActiveSheetIndex(0);
        foreach($columnName as $k => $v){
            $objPHPExcel->getActiveSheet()->setCellValue($cell[$k].'1', $v);
        }
        foreach ($data as $key => $value) {
            $i = $key+1;
            for($j=0;$j<$count;$j++){
                $objPHPExcel->getActiveSheet()->setCellValue($cell[$j].($i+1),  iconv('gbk','UTF-8',$value[$column[$j]]));
            }
        }
        // Redirect output to a client’s web browser (Excel5)
        header('Content-Type: application/vnd.ms-excel');
        header('Content-Disposition: attachment;filename="'.$fileName.'"');
        header('Cache-Control: max-age=0');
        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
        header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
        header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
        header ('Pragma: public'); // HTTP/1.0

        $objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
        $objWriter->save('php://output');
        exit;
    }

 

你可能感兴趣的:(php,laravel)