hellope

Fungsi export2Excel pada file Routines.php

Mar 15th, 2017
350
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 4.59 KB | None | 0 0
  1. function table2Excel($company,$sheetTitle,$data,$fieldCaption){
  2.     /* $sheetTitle -> judul worksheet excel
  3.     * $data -> dataset array dari hasil 'SELECT' query
  4.     * $fieldCaption -> data array untuk header kolom
  5.     */
  6.     if (count($data)>0) {
  7.         $objPHPExcel = new PHPExcel();
  8.         // set properties
  9.         $objPHPExcel->getProperties()
  10.                     ->setCreator("Export to Excel Generator")
  11.                     ->setLastModifiedBy("SYSTEM")
  12.                     ->setTitle("Office 2007 XLSX - Export to Excel")
  13.                     ->setSubject("Office 2007 XLSX - Export to Excel")
  14.                     ->setDescription($sheetTitle)
  15.                     ->setKeywords("")
  16.                     ->setCategory("Export to Excel");
  17.         $objset = $objPHPExcel->setActiveSheetIndex(0);
  18.         $objget = $objPHPExcel->getActiveSheet();
  19.  
  20.         $objget->setTitle($sheetTitle); //sheet title
  21.            
  22.         $val = $fieldCaption;
  23.         array_unshift($val, 'No');
  24.  
  25.         // style for column header
  26.         $headerStyle = array(
  27.             'alignment'=>array(
  28.                 'horizontal' => PHPExcel_Style_Alignment::VERTICAL_CENTER,
  29.             ),
  30.             'fill'=>array( // background style
  31.                 'type'=>PHPExcel_Style_Fill::FILL_SOLID,
  32.                 'color'=>array('rgb'=>'92d050')
  33.             ),
  34.             'font'=>array( // font style
  35.                 'color'=>array('rgb'=>'000000'),
  36.                 'bold'=>true,
  37.                 'size'=>12
  38.             ),
  39.             'borders'=>array( // border style
  40.                 'top' => array(
  41.                     'style' => PHPExcel_Style_Border::BORDER_THIN,
  42.                     'color' => array('argb' => 'FF000000'),
  43.                 ),
  44.                 'left' => array(
  45.                     'style' => PHPExcel_Style_Border::BORDER_THIN,
  46.                     'color' => array('argb' => 'FF000000'),
  47.                 ),
  48.                 'right' => array(
  49.                     'style' => PHPExcel_Style_Border::BORDER_THIN,
  50.                     'color' => array('argb' => 'FF000000'),
  51.                 ),
  52.                 'inside' => array(
  53.                     'style' => PHPExcel_Style_Border::BORDER_THIN,
  54.                     'color' => array('argb' => 'FF000000'),
  55.                 ),
  56.                 'bottom' => array(
  57.                     'style' => PHPExcel_Style_Border::BORDER_DOUBLE,
  58.                     'color' => array('argb' => 'FF000000'),
  59.                 ),
  60.             )
  61.         );
  62.         $cells = array(
  63.             'borders'=>array(
  64.                 'allborders'=>array(
  65.                     'style'=>PHPExcel_Style_Border::BORDER_THIN,
  66.                     'color'=>array('rgb'=>'000000')
  67.                 )
  68.             )
  69.         );
  70.         $companyStyle = array(
  71.             'font' => array(
  72.                 'color' => array('rgb'=>'1BB635'),
  73.                 'bold' => true,
  74.                 'size' => 15
  75.             )
  76.         );
  77.         $sheetTitleStyle = array(
  78.             'font' => array(
  79.                 'color' => array('rgb'=>'4C4D4C'),
  80.                 'bold' => true,
  81.                 'size' => 12
  82.             )
  83.         );
  84.         $date = date('d-m-Y H:i:s');
  85.  
  86.         $objset->setCellValue('A1',ucwords($company));         
  87.         $objset->setCellValue('A2',ucwords($sheetTitle));
  88.         $objPHPExcel->getActiveSheet()->getStyle('A1')->applyFromArray($companyStyle);
  89.         $objPHPExcel->getActiveSheet()->getStyle('A2')->applyFromArray($sheetTitleStyle);
  90.         $objPHPExcel->getActiveSheet()->mergeCells('A1:C1');   
  91.         $objPHPExcel->getActiveSheet()->mergeCells('A2:C2');   
  92.         // set kolom mulai dari A
  93.         $cols = 'A';
  94.         for ($i=0; $i < count($val); $i++) {
  95.             // set kolom header dengan value dari $val=$fieldCaption
  96.             $objset->setCellValue($cols.'5',ucwords($val[$i]));                            
  97.             // set lebar kolom (lebar otomatis)
  98.             $objPHPExcel->getActiveSheet()->getColumnDimension($cols)->setAutoSize(true);                      
  99.             // set style kolom header dari array $headerStyle
  100.             $objPHPExcel->getActiveSheet()->getStyle($cols.'5')->applyFromArray($headerStyle);
  101.             $cols++;
  102.         }
  103.  
  104.         $baris = 6;
  105.         $no = 1;
  106.         $key = array_keys($data[0]);           
  107.         for ($x=0; $x < count($data); $x++) {
  108.             // set kolom mulai dari A
  109.             $cols = 'A';
  110.             for ($i=0; $i < count($val); $i++) {
  111.                 // set cell dengan data dari variabel $data. diLooping sebanyak isi $fieldCaption
  112.                 if ($cols=='A') {
  113.                     $objset->setCellValue($cols.$baris, $no);
  114.                     // set number format
  115.                     $objPHPExcel->getActiveSheet()->getStyle($cols.$baris)->getNumberFormat()->setFormatCode('0');
  116.                 } else {
  117.                     $objset->setCellValue($cols.$baris, $data[$x][$key[$i-1]]);
  118.                 }                                  
  119.                 // set cell style dari array $cells
  120.                 $objPHPExcel->getActiveSheet()->getStyle($cols.$baris)->applyFromArray($cells);
  121.                 $cols++;
  122.             }
  123.             $baris++;
  124.             $no++;
  125.         }                      
  126.         $objset->setCellValue('A'.$baris+=1,ucwords('Dibuat pada: '.$date));   
  127.         $objPHPExcel->getActiveSheet()->mergeCells('A'.$baris.':C'.$baris);        
  128.         $objPHPExcel->getActiveSheet()->setTitle($sheetTitle);
  129.         $objPHPExcel->setActiveSheetIndex(0);  
  130.  
  131.         $objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);
  132.         ob_start();
  133.         $objWriter->save('php://output');
  134.         $xlsData = ob_get_contents();
  135.         ob_end_clean();
  136.  
  137.         $response = array(
  138.             'status' => 'oke',
  139.             'file' => "data:application/vnd.ms-excel;base64,".base64_encode($xlsData)
  140.         );
  141.         return $response;
  142.     } else {
  143.         return json_encode(array('msg'=>'Tidak bisa export'));
  144.     }
  145. }
Advertisement
Add Comment
Please, Sign In to add comment