Cara Import Excel di Yii


Salam Coding, kawan!
pada kesempatan kali ini saya ingin membagi tutorial bagaimana cara import excel di yii.
saya asumsikan anda semua sudah bisa instal dan paham seluk beluk yii, dan sementara saya masih menggunakan yii versi 1.

permata : download library phpexcel di https://github.com/PHPOffice/PHPExcel

taruh di folder protected/extension

kedua : tampilan view seperti coding dibawah ini

<div class="modal fade" id="myModalExcel" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
 <div class="modal-dialog">
 <div class="modal-content">
 <div class="modal-header">
 <button type="button" class="close" data-dismiss="modal"><span aria-hidden="true">&times;</span><span class="sr-only">Close</span></button>
 <h4 class="modal-title" id="myModalLabel">Import File</h4>
 </div>
 <div class="modal-body">
 <form class="form-horizontal" action="<?php echo Yii::app()->createUrl('site/importExcel'); ?>" method="post" enctype="multipart/form-data">
 <div class="row" >
 <div class="col-sm-12">
 
 <div class="box-body">
 <div class="form-group">
 <label for="inputPassword3" class="col-sm-3 control-label">Ambil File</label>
 <div class="col-sm-8">
 <input type="file" name="filee">
 </div>
 </div>
 </div>
 </div>
 <div class="modal-footer">
 <button type="button" class="btn btn-default" data-dismiss="modal">Close</button>
 <button type="submit" class="btn btn-primary"><i class="fa fa-file-excel-o"></i> Import Excel</button>
 </div>
 </div>
 
 </form>

 </div>
 </div>
 </div>
 </div>

ketiga : tambahkan function di controller anda, sebagai contoh ini salah satu function di sitecontroller saya,  seperti coding dibawah ini

public function actionimportExcel(){
 if(isset($_FILES['filee']))
 {

 require(Yii::getPathOfAlias('webroot').'/protected/extensions/phpexcel/Classes/PHPExcel/IOFactory.php');
 $uploadedFile = CUploadedFile::getInstanceByName('filee');

 
 $path=Yii::getPathOfAlias('webroot').'/images/'.$_FILES['filee']['name'];
 $uploadedFile->saveAs($path);
 try {
 $inputFileType = PHPExcel_IOFactory::identify($path);
 $objReader = PHPExcel_IOFactory::createReader($inputFileType);
 $objPHPExcel = $objReader->load($path);
 } catch (Exception $e) {
 die('Error loading file "' . pathinfo($path, PATHINFO_BASENAME) 
 . '": ' . $e->getMessage());
 }
 
 $sheets = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true); 
 $dataArr = array();
 $jo=array();
 $so=array();
 $i = 1;
 
 foreach ($sheets as $sheet) {
 if ($i > 1) {

 $model=new Contoh;
 $model->member=$sheet['A'];
 $model->patner=$sheet['B'];
 $model->categ=$sheet['C'];
 $model->ntm=$sheet['D'];
 $model->in_force=$sheet['E'];
 $model->description=$sheet['F'];
 $model->domestic=$sheet['G'];
 $model->produk=$sheet['H'];
 $model->hs=$sheet['I'];
 $model->source=$sheet['J'];
 $model->national_legal=$sheet['K'];
 
 $model->save(false);

 }
 $i++;
 }

 unlink($path);
 Yii::app()->user->setFlash('berhasil',' Data Contoh berhasil Ditambah ');
 $this->redirect(array('add'));
 }
 }

demikian tutorial singkat saya tentang import excel di yii
semoga bermanfaat

terima kasih dan salam coding

Leave a Reply

Your email address will not be published. Required fields are marked *