Skip to content

Instantly share code, notes, and snippets.

@tmwatchanan
Created June 21, 2019 17:30
Show Gist options
  • Select an option

  • Save tmwatchanan/d0228080003913b6d9b8c5afbc4ff52a to your computer and use it in GitHub Desktop.

Select an option

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
<?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');
@tmwatchanan
Copy link
Copy Markdown
Author

php://output is 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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment