public function actionImport() { $model = new A(); if (isset($_POST['A'])) { Yii::import('ext.phpexcelreader.excel_reader2', true); $model->attributes = $_POST['A']; $import = CUploadedFile::getInstance($model, 'filee'); if ($import == null) { Yii::app()->user->setFlash('error', 'File Kosong'); $this->redirect(array('')); } else { $import->saveAs('coba/' . $import); } if ($import->type == "application/ynd.ms.excel") { $data = new Spreadsheet_Excel_Reader('/../controller/coba1.xls/'); echo $data->dump(true, true); $id = array(); $nama = array(); for ($j = 2; $j <= $data->rowcount(); $j++) { if (empty($data->sheets[0]['cells'][$j][1]) || empty($data->sheets[0]['cells'][$j][2])) { Yii::app()->user->setFlash('error', 'Data Gagal di Import (File excel harus diisi semua)'); $id[$j] = null; $nama[$j] = null; } else { $id[$j] = $data->sheets[0]['cells'][$j][1]; $nama[$j] = $data->sheets[0]['cells'][$j][2]; } } $niki = $data->rowcount(0); for ($i = 1; $i < $niki; $i++) { $model = new A(); $model->id = $id[$i]; $model->nama = $nama[$i]; $model2 = A::model()->findByPk($id[$i]); if ($model2 != null) { Yii::app()->user->setFlash('error', 'Data Gagal di Import (NIDN sudah ada sebelumnya)'); $this->redirect(array('import')); } else { $model->save(); } } $this->redirect(array('index')); } else { Yii::app()->user->setFlash('error', 'Format file tidak dikenali (format file harus .xls)'); $this->redirect(array('import')); } } $this->render('import', array('model' => $model)); /* $model = new A(); if (isset($_POST['A'])) { $model->attributes = $_POST['A']; $itu = CUploadedFile::getInstance($model, 'filee'); $path = 'coba1.xls'; //$itu->saveAs($path); $data = new Spreadsheet_Excel_Reader('coba1.xls'); }*/ }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer $id the ID of the model to be loaded * @return A the loaded model * @throws CHttpException */ public function loadModel($id) { $model = A::model()->findByPk($id); if ($model === null) { throw new CHttpException(404, 'The requested page does not exist.'); } return $model; }