在Yii2中导入Excel

时间:2016-01-05 20:39:04

标签: mysql excel import yii2-advanced-app

我有yii2应用程序使用高级模板和数据库mySql,我已经创建了将excel文件导入其中一个表的函数, 我在名为student的控制器中创建了该函数,其中包含学生数据的CRUD。这是我的代码

 public function actionImportExcel()
{
    $inputFile = 'uploads/siswa_file.xlsx';
    try{
        $inputFileType = \PHPExcel_IOFactory::identify($inputFile);
        $objReader = \PHPExcel_IOFactory::createReader($inputFileType);
        $objPHPExcel = $objReader->load($inputFile);
    } catch (Exception $e) {
        die('Error');
    }

    $sheet = $objPHPExcel->getSheet(0);
    $highestRow = $sheet->getHighestRow();
    $highestColumn = $sheet->getHighestColumn();

    for($row=1; $row <= $highestRow; $row++)
    {
        $rowData = $sheet->rangeToArray('A'.$row.':'.$highestColumn.$row,NULL,TRUE,FALSE);

        if($row==1)
        {
            continue;
        }

        $siswa = new Siswa();
        $siswa->nis = $rowData[0][0]; 
        $siswa->nama_siswa  = $rowData[0][1]; 
        $siswa->jenis_kelamin  = $rowData[0][2]; 
        $siswa->ttl  = $rowData[0][3]; 
        $siswa->alamat  = $rowData[0][4]; 
        $siswa->telp  = $rowData[0][5]; 
        $siswa->agama  = $rowData[0][6]; 
        $siswa->nama_ortu  = $rowData[0][7]; 
        $siswa->telp_ortu  = $rowData[0][8]; 
        $siswa->pekerjaan_ortu = $rowData[0][9];
        $siswa->tahun_masuk = $rowData[0][10];
        $siswa->kelas = $rowData[0][11];
        $siswa->save();

        print_r($siswa->getErrors());
    }
    die('okay');
}

但我不知道如何在视图中创建一个按钮以使此功能正常工作。我的意思是我想制作一个按钮,当用户点击按钮并浏览他们的excel文件时,他们可以导入该文件,并且excel中的数据可以导入到数据库

1 个答案:

答案 0 :(得分:4)

首先你应该上传文件

然后使用您的函数进行处理

您必须生成几部分代码..

例如用户上传文件的视图

  

查看:@ app / views / site / upload.php

     
<?php $form = ActiveForm::begin(['options' => ['enctype' => 'multipart/form-data']]) ?>

    <?= $form->errorSummary($model); ?>

    <?= $form->field($model, 'imageFile')->fileInput() ?>

    <button>Submit</button>

<?php ActiveForm::end() ?>
     

控制器:@ app / controllers / SiteController.php

namespace app\controllers;

use Yii;
use yii\web\Controller;
use app\models\UploadForm;
use yii\web\UploadedFile;

class SiteController extends Controller
{
    public function actionUpload()
    {
        $model = new UploadForm();

        if (Yii::$app->request->isPost) {
            $model->imageFile = UploadedFile::getInstance($model, 'imageFile');
            if ($model->upload()) {
                // file is uploaded successfully
                return;
            }
        }

        return $this->render('upload', ['model' => $model]);
    }
}
     

型号:@ app / models / UploadForm.php

namespace app\models;

use yii\base\Model;
use yii\web\UploadedFile;

class UploadForm extends Model
{
    /**
     * @var UploadedFile
     */
    public $imageFile;

    public function rules()
    {
        return [
            [['imageFile'], 'file', 'skipOnEmpty' => false, 'extensions' => 'png, jpg'],
        ];
    }

    public function upload()
    {
        if ($this->validate()) {
            $this->imageFile->saveAs('uploads/' . $this->imageFile->baseName . '.' . $this->imageFile->extension);
            return true;
        } else {
            return false;
        }
    }
}

代码来自this doc