-1

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

tereško
  • 58,060
  • 25
  • 98
  • 150
tokhy2000
  • 65
  • 3
  • 10

2 Answers2

7

I have the same problem,My solution here . Just put code below :

ob_end_clean();

Code:

        ob_end_clean();
        header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
        header('Content-Disposition: attachment;filename="'. $filename );
        header('Cache-Control: max-age=0');

        $writer->save('php://output'); // download file
Murat B.
  • 111
  • 1
  • 1
  • 10
0

Change this line of code

$writer->save('php:://output');

to

$writer->save('php://output');

It should work. Also, how do you pass data to excel sheet? It's possible that you have corrupt data.

Please, insert this in your code after autoload.php

    use PhpOffice\PhpSpreadsheet\Spreadsheet;
    use PhpOffice\PhpSpreadsheet\Writer\Xlsx;
Budimir Skrtic
  • 419
  • 3
  • 12
  • i changed it but still i cannot open the file and the same error @Budimir Skrtic – tokhy2000 Apr 03 '19 at 07:04
  • OK, what does this do extract($variables); include($this->viewPath.$view.'.php');? Do you get any data output? Take a look at my updated post and add those two lines of code. – Budimir Skrtic Apr 03 '19 at 07:08
  • the above 2 line already added in the top of the page as under namespace : namespace Core\Controller; use Core\App; use PhpOffice\PhpSpreadsheet\Spreadsheet; use PhpOffice\PhpSpreadsheet\Writer\Xlsx; – tokhy2000 Apr 03 '19 at 07:19
  • thanks many thanks i make clear cash and after i edit my code to : $writer->save('php://output'); it's working now @Budimir Skrtic – tokhy2000 Apr 03 '19 at 07:23