01_Simple_download_xlsx.php 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. <?php
  2. use PhpOffice\PhpSpreadsheet\Helper\Sample;
  3. use PhpOffice\PhpSpreadsheet\IOFactory;
  4. use PhpOffice\PhpSpreadsheet\Spreadsheet;
  5. require_once __DIR__ . '/../../src/Bootstrap.php';
  6. $helper = new Sample();
  7. if ($helper->isCli()) {
  8. $helper->log('This example should only be run from a Web Browser' . PHP_EOL);
  9. return;
  10. }
  11. // Create new Spreadsheet object
  12. $spreadsheet = new Spreadsheet();
  13. // Set document properties
  14. $spreadsheet->getProperties()->setCreator('Maarten Balliauw')
  15. ->setLastModifiedBy('Maarten Balliauw')
  16. ->setTitle('Office 2007 XLSX Test Document')
  17. ->setSubject('Office 2007 XLSX Test Document')
  18. ->setDescription('Test document for Office 2007 XLSX, generated using PHP classes.')
  19. ->setKeywords('office 2007 openxml php')
  20. ->setCategory('Test result file');
  21. // Add some data
  22. $spreadsheet->setActiveSheetIndex(0)
  23. ->setCellValue('A1', 'Hello')
  24. ->setCellValue('B2', 'world!')
  25. ->setCellValue('C1', 'Hello')
  26. ->setCellValue('D2', 'world!');
  27. // Miscellaneous glyphs, UTF-8
  28. $spreadsheet->setActiveSheetIndex(0)
  29. ->setCellValue('A4', 'Miscellaneous glyphs')
  30. ->setCellValue('A5', 'éàèùâêîôûëïüÿäöüç');
  31. // Rename worksheet
  32. $spreadsheet->getActiveSheet()->setTitle('Simple');
  33. // Set active sheet index to the first sheet, so Excel opens this as the first sheet
  34. $spreadsheet->setActiveSheetIndex(0);
  35. // Redirect output to a client’s web browser (Xlsx)
  36. header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
  37. header('Content-Disposition: attachment;filename="01simple.xlsx"');
  38. header('Cache-Control: max-age=0');
  39. // If you're serving to IE 9, then the following may be needed
  40. header('Cache-Control: max-age=1');
  41. // If you're serving to IE over SSL, then the following may be needed
  42. header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
  43. header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT'); // always modified
  44. header('Cache-Control: cache, must-revalidate'); // HTTP/1.1
  45. header('Pragma: public'); // HTTP/1.0
  46. $writer = IOFactory::createWriter($spreadsheet, 'Xlsx');
  47. $writer->save('php://output');
  48. exit;