Advertisement
kura2yamato

export report

Mar 23rd, 2023
639
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 11.44 KB | None | 0 0
  1. <?php
  2.     //Controller
  3.     public function export()
  4.     {
  5.         $isValidSource = !empty($_SERVER['HTTP_REFERER'])&&strpos($_SERVER['HTTP_REFERER'],site_url());
  6.         //if($this->request->isAJAX())
  7.         if($isValidSource)
  8.         {
  9. //----------seharusnya ini POST jangan GET..
  10.             $fromDate = $this->request->getVar('fromDate');
  11.             $toDate = $this->request->getVar('toDate');
  12.             $submit = $this->request->getVar('submit');
  13.  
  14.             $response = array();
  15.      
  16.             // Read new token and assign in $response['token']
  17.             $response['token'] = csrf_hash();
  18.  
  19.             /* created excel */
  20.             $spreadsheet = new Spreadsheet();
  21.             $sheet = $spreadsheet->getActiveSheet();
  22.  
  23.             // Buat sebuah variabel untuk menampung pengaturan style dari header tabel
  24.             $style_col = [
  25.                 'font' => ['bold' => true], // Set font nya jadi bold
  26.                 'alignment' => [
  27.                     'horizontal' => \PhpOffice\PhpSpreadsheet\Style\Alignment::HORIZONTAL_CENTER, // Set text jadi ditengah secara horizontal (center)
  28.                     'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER // Set text jadi di tengah secara vertical (middle)
  29.                 ],
  30.                 'borders' => [
  31.                     'top' => ['borderStyle'  => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN], // Set border top dengan garis tipis
  32.                     'right' => ['borderStyle'  => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN],  // Set border right dengan garis tipis
  33.                     'bottom' => ['borderStyle'  => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN], // Set border bottom dengan garis tipis
  34.                     'left' => ['borderStyle'  => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN] // Set border left dengan garis tipis
  35.                     ]
  36.             ];
  37.  
  38.             // Buat sebuah variabel untuk menampung pengaturan style dari isi tabel
  39.             $style_row = [
  40.                 'alignment' => [
  41.                     'vertical' => \PhpOffice\PhpSpreadsheet\Style\Alignment::VERTICAL_CENTER // Set text jadi di tengah secara vertical (middle)
  42.                 ],
  43.                 'borders' => [
  44.                     'top' => ['borderStyle'  => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN], // Set border top dengan garis tipis
  45.                     'right' => ['borderStyle'  => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN],  // Set border right dengan garis tipis
  46.                     'bottom' => ['borderStyle'  => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN], // Set border bottom dengan garis tipis
  47.                     'left' => ['borderStyle'  => \PhpOffice\PhpSpreadsheet\Style\Border::BORDER_THIN] // Set border left dengan garis tipis
  48.                 ]
  49.             ];
  50.  
  51. //----------mohon cari cara penulisan value cell yang lebih bisa terbaca.. jadi tidak menuliskan A1
  52. //----------contoh
  53. //---------- $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $value);
  54.  
  55.             //$sheet->setCellValueByColumnAndRow(1,1, "CHECK SHEET VERIFIKASI HASIL UPLOAD");
  56.             $sheet->setCellValue('A1', "CHECK SHEET VERIFIKASI HASIL UPLOAD"); // Set kolom A1 dengan tulisan "DATA SISWA"
  57.             $sheet->mergeCells('A1:K1'); // Set Merge Cell pada kolom A1 sampai E1
  58.             $sheet->getStyle('A1')->getFont()->setBold(true); // Set bold kolom A1
  59.  
  60.             // Buat header tabel nya pada baris ke 3
  61.             $sheet->setCellValue('A3', "NO");
  62.             $sheet->setCellValue('B3', "TIPE");
  63.             $sheet->setCellValue('C3', "LOT");
  64.             $sheet->setCellValue('D3', "DELIVERY");
  65.             $sheet->setCellValue('E3', "SIDE MAIN");
  66.             $sheet->setCellValue('F3', "PIC");
  67.             $sheet->setCellValue('G3', "SIDE B");
  68.             $sheet->setCellValue('H3', "PIC");
  69.             $sheet->setCellValue('I3', "SIDE A");
  70.             $sheet->setCellValue('J3', "PIC");
  71.             $sheet->setCellValue('K3', "RESULT");
  72.  
  73.             // Apply style header yang telah kita buat tadi ke masing-masing kolom header
  74.             $sheet->getStyle('A3')->applyFromArray($style_col);
  75.             $sheet->getStyle('B3')->applyFromArray($style_col);
  76.             $sheet->getStyle('C3')->applyFromArray($style_col);
  77.             $sheet->getStyle('D3')->applyFromArray($style_col);
  78.             $sheet->getStyle('E3')->applyFromArray($style_col);
  79.             $sheet->getStyle('F3')->applyFromArray($style_col);
  80.             $sheet->getStyle('G3')->applyFromArray($style_col);
  81.             $sheet->getStyle('H3')->applyFromArray($style_col);
  82.             $sheet->getStyle('I3')->applyFromArray($style_col);
  83.             $sheet->getStyle('J3')->applyFromArray($style_col);
  84.             $sheet->getStyle('K3')->applyFromArray($style_col);
  85.  
  86.             //panggil data checksheet   ch_delivery >= '".$start."' AND ch_delivery <= '".$end."'
  87.             $db = \Config\Database::connect();
  88.             $boms = $db->table('tb_cek_harian')->getWhere(['ch_delivery >=' => $fromDate, 'ch_delivery <=' => $toDate])->getResult();
  89.  
  90.             $no = 1; // Untuk penomoran tabel, di awal set dengan 1
  91.             $numrow = 4; // Set baris pertama untuk isi tabel adalah baris ke 4
  92.             foreach($boms as $dt => $data){ // Lakukan looping
  93.  
  94.                 $sheet->setCellValue('A'.$numrow, $no);
  95.                 $sheet->setCellValue('B'.$numrow, $data->ch_name);
  96.                 $sheet->setCellValue('C'.$numrow, $data->ch_lot);
  97.                 $sheet->setCellValue('D'.$numrow, $data->ch_delivery);
  98.                 $sheet->setCellValue('E'.$numrow, $data->ch_side_main);
  99.                 $sheet->setCellValue('F'.$numrow, $data->ch_pic_main);
  100.                 $sheet->setCellValue('G'.$numrow, $data->ch_side_b);
  101.                 $sheet->setCellValue('H'.$numrow, $data->ch_pic_b);
  102.                 $sheet->setCellValue('I'.$numrow, $data->ch_side_a);
  103.                 $sheet->setCellValue('J'.$numrow, $data->ch_pic_a);
  104.                 $sheet->setCellValue('K'.$numrow, $data->ch_result);
  105.  
  106.                 // Apply style row yang telah kita buat tadi ke masing-masing baris (isi tabel)
  107.                 $sheet->getStyle('A'.$numrow)->applyFromArray($style_row);
  108.                 $sheet->getStyle('B'.$numrow)->applyFromArray($style_row);
  109.                 $sheet->getStyle('C'.$numrow)->applyFromArray($style_row);
  110.                 $sheet->getStyle('D'.$numrow)->applyFromArray($style_row);
  111.                 $sheet->getStyle('E'.$numrow)->applyFromArray($style_row);
  112.                 $sheet->getStyle('F'.$numrow)->applyFromArray($style_row);
  113.                 $sheet->getStyle('G'.$numrow)->applyFromArray($style_row);
  114.                 $sheet->getStyle('H'.$numrow)->applyFromArray($style_row);
  115.                 $sheet->getStyle('I'.$numrow)->applyFromArray($style_row);
  116.                 $sheet->getStyle('J'.$numrow)->applyFromArray($style_row);
  117.                 $sheet->getStyle('K'.$numrow)->applyFromArray($style_row);
  118.  
  119.                 $no++; // Tambah 1 setiap kali looping
  120.                 $numrow++; // Tambah 1 setiap kali looping
  121.             }
  122.  
  123.             // Set width kolom
  124.             $sheet->getColumnDimension('A')->setWidth(5); // Set width kolom A
  125.             $sheet->getColumnDimension('B')->setWidth(15); // Set width kolom B
  126.             $sheet->getColumnDimension('C')->setWidth(25); // Set width kolom C
  127.             $sheet->getColumnDimension('D')->setWidth(20); // Set width kolom D
  128.             $sheet->getColumnDimension('E')->setWidth(30); // Set width kolom E
  129.             $sheet->getColumnDimension('F')->setWidth(30); // Set width kolom E
  130.             $sheet->getColumnDimension('G')->setWidth(30); // Set width kolom E
  131.             $sheet->getColumnDimension('H')->setWidth(30); // Set width kolom E
  132.             $sheet->getColumnDimension('I')->setWidth(30); // Set width kolom E
  133.             $sheet->getColumnDimension('J')->setWidth(30); // Set width kolom E
  134.             $sheet->getColumnDimension('K')->setWidth(30); // Set width kolom E
  135.  
  136.             // Set height semua kolom menjadi auto (mengikuti height isi dari kolommnya, jadi otomatis)
  137.             $sheet->getDefaultRowDimension()->setRowHeight(-1);
  138.  
  139.             // Set orientasi kertas jadi LANDSCAPE
  140.             $sheet->getPageSetup()->setOrientation(\PhpOffice\PhpSpreadsheet\Worksheet\PageSetup::ORIENTATION_LANDSCAPE);
  141.  
  142.             // Set judul file excel nya
  143.             $sheet->setTitle("Checksheet");
  144.             /* ./ created excel */
  145.  
  146.             $writer = new Xlsx($spreadsheet);
  147.             $filename = date('Y-m-d-His'). '-Data-Verifikasi';
  148.  
  149.             $writer->save('php://output');
  150.  
  151.             header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  152.             header("Pragma: public");
  153.             header("Expires: 0");
  154.             header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  155.             header("Content-Type: application/force-download");
  156.             header("Content-Type: application/octet-stream");
  157.             header("Content-Type: application/download");;
  158.             header('Content-Disposition: attachment;filename='  . $filename . '.xlsx');
  159.  
  160.             readfile($filename); // send file
  161.             sleep(5);
  162.             unlink($filename); // delete file
  163.             exit;
  164.            
  165.             $data = [
  166.                 'filename' => $filename.'.xlsx',
  167.                 'token' => csrf_hash(),
  168.             ];
  169.  
  170.             echo json_encode($data);
  171.            
  172.         }
  173.         else{
  174.             exit('Maaf request tidak dapat diproses!');
  175.         }
  176.     }
  177. //ajax
  178. // select data for check sheet
  179.     $('#btn_e').on('click', function(e){
  180.         e.preventDefault();
  181.         var fromDate = $("#fromDate").val();
  182.         var toDate = $("#toDate").val();
  183. //----mohon tambahkan filter yg lebih baik lagi ===
  184.         if(fromDate == ''){
  185.           error_firstname = 'please enter from delivery date';
  186.           alert(error_firstname);
  187.         }
  188.         else if(toDate == ''){
  189.           error_lastname = 'please enter to delivery date';
  190.           alert(error_lastname);
  191.         }else{
  192.             console.log('test');
  193.             // CSRF Hash
  194.             var csrfName = $('.txt_csrfname').attr('name'); // CSRF Token name
  195.             var csrfHash = $('.txt_csrfname').val(); // CSRF hash
  196.  
  197.             var dataJson = {
  198.                 [csrfName]: csrfHash,
  199.                 fromDate: $("#fromDate").val(),
  200.                 toDate: $("#toDate").val()
  201.             };
  202.             var urlTarget = '<?= site_url('/checksheet/export'); ?>';
  203.             window.location = urlTarget+'?fromDate='+fromDate+"&toDate="+toDate"&csrfHash"+csrfHash;
  204.             /*
  205.             $.ajax({
  206.                 url: "<?php echo base_url('/checksheet/export'); ?>",
  207.                 type: "post",
  208.                 data: dataJson,
  209.                 dataType: "json",
  210.                 success: function(response)
  211.                 {
  212.                     // Update CSRF Token
  213.                     $('.txt_csrfname').val(data.token);
  214.                     // console.log(data.filename);
  215.                     window.location = '<?= site_url('/checksheet/export') ?>';
  216.                    
  217.                     // formDate = "",
  218.                     // toDate = ""
  219.                 },
  220.                 error: function (jqXhr, textStatus, errorMessage) { // error callback
  221.                     alert(xhr.status + "\n" + xhr.responseText + "\n" + thrownError);
  222.                 }
  223.             });
  224.             */
  225.             return false;
  226.         }
  227.     });
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement