kura2yamato

Excel 003 - basic 1

May 31st, 2021 (edited)
246
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.71 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.     //throw new \Exception('Composer autoloader could not be found. Install dependencies with `composer install` and try again.');
  24.    
  25.    
  26. }
  27.  
  28. ?>
  29. <!--
  30. /**
  31. ##INFO##
  32. {"title":"excel Dasar ","detail":"membuat excel menggunakan PHP Spreadsheet "}
  33. ##INFO##
  34. **/
  35. -->
  36. <?php
  37. $spreadsheet = new Spreadsheet();
  38.  
  39. $spreadsheet->setActiveSheetIndex(0)
  40.     ->setCellValue('A1', 'Hello')
  41.     ->setCellValue('B2', 'world!')
  42.     ->setCellValue('C1', 'Hello')
  43.     ->setCellValue('D2', 'world!');
  44.    
  45. $spreadsheet->getActiveSheet()
  46.     ->setTitle('Simple');
  47.  
  48.  
  49. /*
  50. Pada Bagian ini disarankan simpan ke folder dahulu
  51. sebelum mengeluarkan..
  52. atau di download
  53. */
  54. $file='contoh001.xlsx';
  55.  
  56. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  57. header('Content-Disposition: attachment;filename="'.$file.'"');
  58. header('Content-Length: ' . filesize($file));
  59.  
  60. header('Content-Transfer-Encoding: binary');
  61. header('Cache-Control: must-revalidate');
  62. header('Pragma: public');
  63.  
  64. ob_clean();
  65. flush();
  66.  
  67. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  68. $writer->save($file);
  69.  
  70. $str=file_get_contents($file);
  71.  
  72. die($str);
  73.  
Add Comment
Please, Sign In to add comment