Created
June 21, 2019 17:30
-
-
Save tmwatchanan/d0228080003913b6d9b8c5afbc4ff52a to your computer and use it in GitHub Desktop.
A PHP script of how to directly output Xlsx (Excel) file when a user access a webpage
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <?php | |
| require_once 'vendor/autoload.php'; | |
| use PhpOffice\PhpSpreadsheet\Spreadsheet; | |
| use PhpOffice\PhpSpreadsheet\Writer\Xlsx; | |
| $spreadsheet = new Spreadsheet(); | |
| $sheet = $spreadsheet->getActiveSheet(); | |
| $sheet->setCellValue('A1', 'Hello World !'); | |
| $writer = new Xlsx($spreadsheet); | |
| $filename = 'hello world'; | |
| header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); | |
| header('Content-Disposition: attachment;filename="' . $filename . '.xlsx"'); | |
| header('Cache-Control: max-age=0'); | |
| $writer->save('php://output'); |
Author
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
php://outputis a writable stream that is sent to Apache and will be returned to the browser that requested your page.Cr. https://stackoverflow.com/a/7186297/7150241