当前位置:首页 > CMS教程 > 其它CMS > 列表

yii2.0框架实现上传excel文件后导入到数据库的方法示例

发布:smiling 来源: PHP粉丝网  添加日期:2022-02-26 08:56:50 浏览: 评论:0 

本文实例讲述了yii2.0框架实现上传excel文件后导入到数据库的方法,分享给大家供大家参考,具体如下:

Model模型

  1. <?php 
  2. /** 
  3.  * 描述... 
  4.  * @author zcy 
  5.  * @date 2019/8/13 
  6.  */ 
  7.  
  8. namespace app\models; 
  9.  
  10. use yii\base\Model; 
  11. use yii\db\ActiveRecord; 
  12. use yii\web\UploadedFile; 
  13.  
  14. class uploadForm extends ActiveRecord 
  15. { 
  16.   public $file; 
  17.  
  18.   public function rules() 
  19.   { 
  20.     return [ 
  21.       [['file'],'file', 'skipOnEmpty' => false,'extensions' => 'xls,xlsx'], 
  22.     ]; 
  23.   } 
  24.  
  25.   public function attributeLabels() 
  26.   { 
  27.     return [ 
  28.       'file'=> '上传文件' 
  29.     ]; 
  30.   } 
  31.  
  32.   public function upload() 
  33.   { 
  34.     $file = UploadedFile::getInstance($this, 'file'); 
  35.  
  36.     if ($this->rules()) { 
  37.       $tmp_file = $file->baseName . '.' . $file->extension; 
  38.       $path = 'upload/' . 'Files/'; 
  39.       if (is_dir($path)) { 
  40.         $file->saveAs($path . $tmp_file); 
  41.       } else { 
  42.         mkdir($path, 0777, true); 
  43.       } 
  44.       $file->saveAs($path . $tmp_file); 
  45.       return true; 
  46.     } else { 
  47.       return '验证失败'; 
  48.     } 
  49.   } 
  50.  
  51. } 

Views视图

  1. <?php 
  2.  
  3. use yii\widgets\ActiveForm; 
  4.  
  5. $model = new app\models\uploadForm(); 
  6. $form = ActiveForm::begin([ 
  7.   'id' => 'upload', 
  8.   'options' => ['enctype' => 'multipart/form-data'], 
  9. ]) 
  10. ?> 
  11.  
  12. <?= $form->field($model,'file')->fileInput(['multiple'=>'multiple']) ?> 
  13.  
  14.   <button>上传</button> 
  15. <?php ActiveForm::end() ?> 

Controller控制器

  1. <?php 
  2. /** 
  3.  * 描述... 
  4.  * @author zcy 
  5.  * @date 2019/8/16 
  6.  */ 
  7.  
  8. namespace app\controllers; 
  9.  
  10. use app\models\uploadForm; 
  11. use Yii; 
  12. use yii\web\Controller; 
  13. use yii\web\UploadedFile; 
  14.  
  15. class UploadController extends Controller 
  16. { 
  17.   /** 
  18.    * 导入 
  19.    * @author zcy 
  20.    * @date 2019/8/16 
  21.    */ 
  22.   public function actionImport() 
  23.   { 
  24.     $model = new uploadForm(); 
  25.  
  26.     if (Yii::$app->request->isPost) { 
  27.       $model->file = UploadedFile::getInstance($model,'file'); 
  28. //      if ($model->upload()) { 
  29. //        print <<<EOT 
  30. // <script>alert('上传成功')</script> 
  31. //EOT; 
  32. //      } else { 
  33. //        print <<<EOT 
  34. // <script>alert('上传失败')</script> 
  35. //EOT; 
  36. //      } 
  37.       if (!$model->upload()) { 
  38.         print <<<EOT 
  39.  <script>alert('上传失败')</script> 
  40. EOT; 
  41.       } 
  42.     } 
  43.  
  44.     $ok = 0; 
  45.     if ($model->load(Yii::$app->request->post())) { 
  46.       $file = UploadedFile::getInstance($model,'file'); 
  47.  
  48.       if ($file) { 
  49.         $filename = 'upload/Files/' . $file->name; 
  50.         $file->saveAs($filename); 
  51.  
  52.         if (in_array($file->extension,array('xls','xlsx'))) { 
  53.           $fileType = \PHPExcel_IOFactory::identify($filename);//文件名自动判断类型 
  54.           $excelReader = \PHPExcel_IOFactory::createReader($fileType); 
  55.  
  56.           $phpexcel = $excelReader->load($filename)->getSheet(0);//载入文件并获取第一个sheet 
  57.           $total_line = $phpexcel->getHighestRow();//总行数 
  58.           $total_column = $phpexcel->getHighestColumn();//总列数 
  59.  
  60.           if (1 < $total_line) { 
  61.             for ($row = 2;$row <= $total_line;$row++) { 
  62.               $data = []; 
  63.               for ($column = 'A';$column <= $total_column;$column++) { 
  64.                 $data[] = trim($phpexcel->getCell($column.$row)); 
  65.               } 
  66.  
  67.               $info = Yii::$app->db->createCommand() 
  68. ->insert('{{%shop_info}}',['shop_name' => $data[0],'shop_type' => $data[1]]) 
  69. ->execute(); 
  70.  
  71.               if ($info) { 
  72.                 $ok = 1; 
  73.               } 
  74.             } 
  75.           } 
  76.  
  77.           if ($ok == 1) { 
  78.             echo "<script>alert('导入成功');window.history.back();</script>"; 
  79.           } else { 
  80.             echo "<script>alert('操作失败');window.history.back();</script>"; 
  81.           } 
  82.         } 
  83.       } 
  84.     } else { 
  85.       return $this->render('import',['model' => $model]); 
  86.     } 
  87.   } 
  88. }

Tags: yii2.0上传excel文件

分享到: