Membuat komponen sendiri, sebagai contoh disini membuat komponen dengan nama MyController.php taruh di direktori Protected\Components\MyController.php yang isinya seperti berikut ini:
- <?php
- class MyController extends Controller {
- //ekport excel
- public function createExcel($fileName) {
- #tentukan mime/format data dan force download excel
- header("Content-type: application/vnd.ms-excel; charset=utf-8");
- #menentukan lampiran file dan nama filenya
- header("Content-Disposition: attachment; filename=$fileName.xls"); }
- }
- <?php
- class ExportController extends MyController{
- public $layout='//layouts/column2';
- //fungsi untuk export data dosen aktif
- public function actionToexcel($fileName){
- $this->createExcel($fileName);
- $sql=Yii::app()->db->createCommand()
- ->select('*') ->from('tb_pegawai peg')
- ->join('tb_golongan gol', 'peg.golongan_id=gol.id')
- ->join('tb_jabatan jab', 'peg.jabatan_id=jab.id')
- ->join('tb_pendidikan pend', 'peg.ijazah_id=pend.id')
- ->join('tb_unit unit', 'peg.unit_id=unit.id')
- ->where('peg.jnspeg=1 AND (peg.status_kepeg=1 OR peg.status_kepeg=3)
- AND (peg.status="1" OR peg.status="3" OR peg.status="6")')
- ->order(array('gol.bobot DESC', 'peg.tmtgol ASC', 'jab.bobot DESC', 'peg.mkth DESC', 'peg.mkbl DESC', 'pend.bobot DESC', 'peg.luluspend ASC', 'peg.tgl_lhr ASC'))
- ->queryAll();
- $dataExcel=$this->renderPartial("report",array( "dataExport"=>$sql, ));
- //download excel
- echo $dataExcel;
- }
- //Membuat halaman index Download
- public function actionIndex()
- {
- $this->render("index");
- }
- }
- ?>
Halaman Index.php
<div>
<h4><strong><u>EXPORT DATA EXEL:</u></strong></h4>
</div>
<div class="dashboardIcons span-23">
<div class="dashboardIcons span-3">
<a href="<?php echo $this->createUrl("export/toexcel/fileName/reportexcel");?>" Title="Export Dosen Aktif">
<img src="<?php echo yii::app()->request->baseUrl.'/images/excel-logo.png';?>" alt="Export Dosen Aktif"/></a>
<div class="dashIconText">Export Dosen Aktif</div>
</div>
</div>
Halaman Report.php
<table border="1" align="center" cellpadding="5">
<caption>
DAFTAR DOSEN
</caption>
<tr>
<th>NO</th>
<th>NIP</th>
<th>NIDN</th>
<th>NAMA</th>
<th>GELAR DEPAN</th>
<th>GELAR BELAKANG</th>
<th>TEMPAT LAHIR</th>
<th>TANGGAL LAHIR</th>
<th>JENIS KELAMIN</th>
<th>GOLONGAN</th>
<th>STATUS KEPEG</th>
<th>TMT GOLONGAN</th>
<th>JABATAN</th>
<th>TMT JABATAN</th>
<th>SERDOS</th>
<th>NO SERDOS</th>
<th>TMT SERDOS</th>
<th>IJAZAH</th>
<th>TAHUN LULUS</th>
<th>STATUS BEKERJA </th>
<th>TMT STATUS BEKERJA</th>
<th>KETERANGAN</th>
<th>JENIS PEGAWAI</th>
<th>UNIT KERJA</th>
</tr>
<?php
$no=1;
foreach($dataExport as $data):?>
<tr>
<td><?php echo $no;?></td>
<td><?php echo "'".$data['nip'];?></td>
<td><?php echo "'".$data['nidn'];?></td>
<td><?php echo $data['nama'];?></td>
<td><?php echo $data['glr_dpn'];?></td>
<td><?php echo $data['glr_blkg'];?></td>
<td><?php echo $data['tmp_lhr'];?></td>
<td><?php echo MyComponent::getInputDate($data['tgl_lhr']);?></td>
<td><?php echo $data['jnskel'];?></td>
<td><?php echo $data['gol'];?></td>
<td><?php echo MyComponent::ubahStatusKepegawaian($data['status_kepeg']);?></td>
<td><?php echo $data['tmtgol'];?></td>
<td><?php echo $data['jabatan'];?></td>
<td><?php echo MyComponent::getInputDate($data['tmtjab']);?></td>
<td><?php echo MyComponent::ubahserdos($data['serdos']);?></td>
<td><?php echo "'".$data['noserdos'];?></td>
<td><?php echo $data['tmtserdos'];?></td>
<td><?php echo $data['ijazah'];?></td>
<td><?php echo $data['luluspend'];?></td>
<td><?php echo MyComponent::ubahStatusPegawai($data['status']);?></td>
<td><?php echo MyComponent::getInputDate($data['tmt_status']);?></td>
<td><?php echo $data['ket'];?></td>
<td><?php echo MyComponent::ubahJenisPegawai($data['jnspeg']);?></td>
<td><?php echo $data['unit'];?></td>
</tr>
<?php
$no++;
endforeach;?>
</table>
Demikian tutorial cara membuat menu export data excel dari aplikasi YII Framework, semoga bisa membantu Anda yang sedang membutuhkan tutorial ini. Salam
kurang detail, saya blm ngerti :(
BalasHapus