i want to use PHPSpreadsheet library in my project to export data to excel the file exported but when i try to open the file this error display: excel cannot open the file because the file format or file extension is not valid. verify that the file has not been corrupted
Note: i use MVC in my project so the code in controller as the following:
protected function Excel($view, $variables = []) { require_once PATH_LIBRARY_FOLDER.'PhpSpreadsheet\vendor\autoload.php'; ob_start(); // Note: make new Spreadsheet object $spreadsheet = new Spreadsheet(); // Note: get current active sheet (frist sheet) $sheet = $spreadsheet->getActiveSheet(); $sheet->setCellValue('A1', 'Hello World !'); extract($variables); include($this->viewPath.$view.'.php'); // Note: set the header to define it is excel file header("Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"); // Note: set the header to define excel file name header("Content-Disposition: attachment;filename=\"filename.xlsx\""); header("Cache-Control: max-age=0"); // Note: create IOFactory object $writer = IOFactory::createWriter($spreadsheet, 'xlsx'); ob_get_clean(); $writer->save('php:://output'); exit(); }
when i open the file as a text i found this error:
Fatal error: Uncaught PhpOffice\PhpSpreadsheet\Writer\Exception: Could not open php:://output for writing. in C:\xampp\htdocs\GL_App\Library\PhpSpreadsheet\vendor\phpoffice\phpspreadsheet\src\PhpSpreadsheet\Writer\Xlsx.php:218