php 使用PHPExcel 拓展上传文件
作者:互联网
public static function ImportExcel($door_id) { set_time_limit(0); //解决上传超时 @ini_set('memory_limit', '512M'); // 解决上传内存不够 $self = new self(); //导入excel $addFile=$_FILES['file']; if (empty($addFile['name'])){ return ['err'=> 1,'msg' =>'请上传表格']; } $excelFileName =$addFile['tmp_name']; $file_types = explode ( ".", $addFile['name'] ); $excel_type = array('xlsx'); if($addFile['error'] > 0){ return ['err' => 1,'msg'=>'上传出现错误,错误代码'.$addFile['error']]; } if (!in_array(strtolower(end($file_types)),$excel_type)){ return ['err'=> 2,'msg' =>'文件格式错误,只允许上传xlsx文件']; } include_once dirname(dirname(dirname(__FILE__))).'/vendor/Classes/PHPExcel/IOFactory.php'; include_once dirname(dirname(dirname(__FILE__))).'/vendor/Classes/PHPExcel/Cell.php'; $objPHPExcel = \PHPExcel_IOFactory::load($excelFileName); $sheet = $objPHPExcel->getSheet(0); $data=$sheet->toArray(); $highestColumn = $sheet->getHighestColumn(); $highestRow = $sheet->getHighestRow(); // 取得总行数 $imageFilePath='static/excel/media/'.$door_id.'/';//图片在本地存储的路径 if (!file_exists ( $imageFilePath )){ mkdir("$imageFilePath", 0777, true); } //excel 中带有图片 // foreach($sheet->getDrawingCollection() as $img){ // list($startColumn,$startRow)= \PHPExcel_Cell::coordinateFromString($img->getCoordinates());//获取图片所在行和列 // $imageFileName = $img->getCoordinates() . mt_rand(100, 999); // copy($img->getPath(),$imageFilePath.$imageFileName.'.'.$img->getExtension()); // //插入代码 // $startColumn = self::ABC2decimal($startColumn);//由于图片所在位置的列号为字母,转化为数字 // $data[$startRow-1][$startColumn]=$imageFilePath.$imageFileName.'.'.$img->getExtension();//把图片插入到数组中 // } $columns = self::ABC2decimal($highestColumn); $str = ''; // $industry = MediaIndustry::find()->select('id,industry_name')->where(['media_id' => 1])->asArray()->all(); $industry = MediaIndustry::find()->select('id,industry_name')->asArray()->all(); $indu = ArrayHelper::map($industry,'id','industry_name'); $transaction = Yii::$app->db->beginTransaction(); for ($row = 1; $row < $highestRow; $row++){ for ($col = 0; $col <= $columns ; $col++) { $str = $data[0][$col]; $media = new Media; //判断excel 表格中的数据 switch ($str) { case 'industry_id': $media->industry_id = (int)array_search($data[$row][$col],$indu); break; case 'region_id': // $media->industry_id = array_search($data[$row][$col],$indu); break; case 'link_type': $media->link_type = array_search($data[$row][$col],$self->link); break; case 'entrance_level': $media->entrance_level = array_search($data[$row][$col],$self->entrance_level); break; case 'news_source': $media->news_source = array_search($data[$row][$col],$self->news_sources); break; default: $media->$str = (string)$data[$row][$col]; break; } } if(!$media->save()){ $transaction->rollback(); return ['err'=> 1,'msg' =>'请检查第'.$row.'行']; } } unset($media); unset($data); $transaction->commit(); return ['err'=> 0,'msg' =>'导入成功']; } /** * 文件导入时由于图片所在位置的列号为字母,转化为数字 * @param [type] $abc [description] */ public static function ABC2decimal($abc) { $ten = 0; $len = strlen($abc); for($i=1;$i<=$len;$i++){ $char = substr($abc,0-$i,1);//反向获取单个字符 $int = ord($char); $ten += ($int-65)*pow(26,$i-1); } return $ten; }
标签:media,industry,data,col,PHPExcel,php,上传,id,row 来源: https://www.cnblogs.com/wangsaii/p/13152823.html