Advertisement
kura2yamato

Excel 004 basic 2

May 31st, 2021
1,011
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 1.99 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. }
  26.  
  27. ?>
  28. <!--
  29. /**
  30. ##INFO##
  31. {"title":"excel Dasar ","detail":"Membuat excel menggunakan PHP Spreadsheet . Memanfaatkan Properties"}
  32. ##INFO##
  33. **/
  34. -->
  35. <?php
  36. $spreadsheet = new Spreadsheet();
  37.  
  38. $spreadsheet->setActiveSheetIndex(0)
  39.     ->setCellValue('A1', 'Hello')
  40.     ->setCellValue('B2', 'world!')
  41.     ->setCellValue('C1', 'Hello')
  42.     ->setCellValue('D2', 'world!');
  43.    
  44. $spreadsheet->getActiveSheet()
  45.     ->setTitle('Simple');
  46.  
  47. //properties
  48. $spreadsheet->getProperties()->setCreator('Gunawan Wibisono')
  49.     ->setLastModifiedBy('Scripter PHP')
  50.     ->setTitle('Learn Document part 2')
  51.     ->setSubject('Office 2007 XLSX Learn Document')
  52.     ->setDescription('membuat dokumen Office 2007 XLSX, memanfaatkan PHP-spreadsheets.')
  53.     ->setKeywords('office 2007 openxml php')
  54.     ->setCategory('php excel native');
  55.    
  56. /*
  57. Pada Bagian ini disarankan simpan ke folder dahulu
  58. sebelum mengeluarkan..
  59. atau di download
  60. */
  61. $file='contoh004.xlsx';
  62.  
  63. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  64. header('Content-Disposition: attachment;filename="'.$file.'"');
  65. header('Content-Length: ' . filesize($file));
  66.  
  67. header('Content-Transfer-Encoding: binary');
  68. header('Cache-Control: must-revalidate');
  69. header('Pragma: public');
  70.  
  71. ob_clean();
  72. flush();
  73.  
  74. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  75. $writer->save($file);
  76.  
  77. $str=file_get_contents($file);
  78.  
  79. die($str);
  80.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement