Download Source Code Aplikasi

Cara Export data ke Excel dengan PHPExcel

Thursday, February 11th, 2016 - Tutorial Php

Export data sangat dibutuhkan dalam pengembangan sistem informasi, modul pertama saya ditempat kerjaan saya adalah membuat rekap laporan dan kemudian mengexportnya menjadi file excel. Dalam menyelesaikan pekerjaan ini saya menggunakan PHPExcel, dokumentasi dari phpexcel ini lengkap banget, tutorial ini dibuat sebagai dokumentasi saya pribadi, dan mungkin berguna juga bagi yang lain.

cara export-php-excel

untuk menggunakan phpexcel pertama download dulu phpexcel disini masukkan class phpexcel dengan fungsi requireonce, dibawah ini contoh penggunaan script phpexcel. Modul ini merupakan contoh modul untuk melakukan rekap laporan detail :

<?php /** PHPExcel */ require_once 'Classes/PHPExcel.php'; $f=$_GET['filter']; if($f=='99'){ $filter = " and MONTH(updated)=".$_GET['m']." and YEAR(updated)=".$_GET['year']; } else{ $filter = " and approved_status='".$f."' and MONTH(updated)=".$_GET['m']." and YEAR(updated)=".$_GET['year']; } $q=mysql_query("select judul,sender,approved_status, DATE_FORMAT(approved_time,'%e/%b/%Y %H:%i wib') as date from online_tempointeraktif_news where approved_by='".$_GET['user']."' $filter order by approved_time DESC"); // Create new PHPExcel object $object = new PHPExcel(); // Set properties $object->getProperties()->setCreator("Tempo")
               ->setLastModifiedBy("Tempo")
               ->setCategory("Approve by ");
// Add some data
$object->getActiveSheet()->getColumnDimension('A')->setWidth(50);
$object->getActiveSheet()->getColumnDimension('B')->setWidth(30);
$object->getActiveSheet()->getColumnDimension('C')->setWidth(30);
$object->getActiveSheet()->getColumnDimension('D')->setWidth(30);
$object->getActiveSheet()->getColumnDimension('E')->setWidth(30);
$object->getActiveSheet()->getColumnDimension('F')->setWidth(30);
$object->getActiveSheet()->mergeCells('A1:F1');
$object->getActiveSheet()->mergeCells('A2:F2');
$object->setActiveSheetIndex(0)
            ->setCellValue('A1', 'Rekap Berita Approved by : '.$_GET['user'])
            ->setCellValue('A4', 'Judul')
            ->setCellValue('B4', 'Uploader')
            ->setCellValue('C4', 'Approve Date')
            ->setCellValue('D4', 'Delay Date')
            ->setCellValue('E4', 'Drop Date')
            ->setCellValue('F4', 'Post Edit Date');
 
//add data
$counter=5;
$ex = $object->setActiveSheetIndex(0);
while($d=mysql_fetch_array($q)){
           
          $time_approve='-';
          $time_delay='-';
          $time_drop='-';
          $time_passed='-';
           
           
          $status=$d['approved_status'];
          $date=$d['date'];
          if($status==1){
            $time_approve=$date;
          }
          else if($status==2){
            $time_delay=$date;
          }
          else if($status==3){
            $time_drop=$date;
          }
          else if($status==4){
            $time_passed=$date;
          }
          $ex->setCellValue("A".$counter,$d['judul']);
          $ex->setCellValue("B".$counter,$d['sender']);
          $ex->setCellValue("C".$counter,"$time_approve");
          $ex->setCellValue("D".$counter,"$time_delay");
          $ex->setCellValue("E".$counter,"$time_drop");
          $ex->setCellValue("F".$counter,"$time_passed");
          $counter=$counter+1;
           
}    
 
// Rename sheet
$object->getActiveSheet()->setTitle('Detail_approve_'.$_GET['user'].'_'.$_GET['m'].'_'.$_GET['year']);
 
 
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$object->setActiveSheetIndex(0);
 
 
// Redirect output to a client’s web browser (Excel2007)
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
header('Content-Disposition: attachment;filename="Detail_approve_'.$_GET['user'].'_'.$_GET['m'].'_'.$_GET['year'].'.xlsx"');
header('Cache-Control: max-age=0');
 
$objWriter = PHPExcel_IOFactory::createWriter($object, 'Excel2007');
$objWriter->save('php://output');
exit;
?>

Selamat mencoba

Sumber : jagocoding

(Telah didownload 224 kali, 1 kali dibaca hari ini)
Download Source Code Aplikasi
Cara Export data ke Excel dengan PHPExcel | admin | 4.5


Leave a Reply
You might also likeclose
error: Content is protected !!