Advertisement
kura2yamato

Excel 009 Template dr XLSX

May 31st, 2021
1,241
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.86 KB | None | 0 0
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  3. use PhpOffice\PhpSpreadsheet\IOFactory;
  4. /*
  5. Bagian ini silahkan anggap tidak ada..
  6. karena dasarnya ini include composer (autoload.php)
  7. */
  8. {
  9.     $paths = [
  10.         __DIR__ . '/../vendor/autoload.php', // In case PhpSpreadsheet is cloned directly
  11.         __DIR__ . '/../../../autoload.php', // In case PhpSpreadsheet is a composer dependency.
  12.         __DIR__ . '/../../../web/example/vendor/autoload.php' //memakai ini
  13.     ];
  14.  
  15.     foreach ($paths as $path) {
  16.         if (file_exists($path)) {
  17.             require_once $path;
  18.             //echo "run:$path";
  19.             ;
  20.         }
  21.     }
  22.  
  23. }
  24.  
  25. function sCellPos($row,$col) //perbaikan
  26. {
  27.     //hanya untuk A-Z (26)
  28.     //$p=ord('A');//65
  29.     $colInt=64+$col;
  30.     $p=chr($colInt).$row;
  31.     return $p;
  32. }
  33.  
  34. /**
  35. ##INFO##
  36. {"title":"excel Dasar ","detail":"Membuat excel menggunakan PHP Spreadsheet . Memanfaatkan excel yang sudah dibuat sebelumnya dan menambahkan isinya. "}
  37. ##INFO##
  38. **/
  39.  
  40.  
  41. $filename=__DIR__ . '/../contoh002-a.xlsx';
  42.  
  43. $spreadsheet = IOFactory::load($filename);
  44. if(true)
  45. {
  46.     $sheet=$spreadsheet->setActiveSheetIndex(0);
  47.     $kalimat=date("Y m d H i s");
  48.     $dates = explode(" ",$kalimat);
  49.  
  50.     $pos=1;
  51.     foreach($dates  as $str)
  52.     {
  53.         $spreadsheet->setActiveSheetIndex(0)->setCellValue(sCellPos(5,$pos++), $str);
  54.        
  55.     }
  56.    
  57. }  
  58.  
  59.  
  60. /*
  61. Pada Bagian ini disarankan simpan ke folder dahulu
  62. sebelum mengeluarkan..
  63. atau di download
  64. */
  65.  
  66. $file='test009.xlsx';
  67.  
  68. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  69. $writer->save($file);
  70.  
  71. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  72. header('Content-Disposition: attachment;filename="'.$file.'"');
  73. header('Content-Length: ' . filesize($file));
  74.  
  75. header('Content-Transfer-Encoding: binary');
  76. header('Cache-Control: must-revalidate');
  77. header('Pragma: public');
  78.  
  79. ob_clean();
  80. flush();
  81.  
  82. $str=file_get_contents($file);
  83.  
  84. die($str);
  85.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement