Marcel Bührig
Marcel Bührig

Reputation: 115

PHPExcel error when opening created File in EXCEL

I create with a php-script which uses the PHPExcel Library an simple .xlsx file. But when I want to open it in MS Excel 2010 on Win7, I get an error message that file format is wrong or the file is damaged. Tried several possible solutions from the internet but nothing worked for me.

public function createControllingFile($suffix){ $this->PHPExcel = new PHPExcel();

    $year = date('y');

    header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
    header('Cache-Control: max-age=0');


    $this->objWriter = PHPExcel_IOFactory::createWriter($this->PHPExcel, 'excel5');


    $this->objWriter->save('tmp/controlling_'.$year.'_'.$suffix.'.xlsx');

    $_SESSION['counter'] = 0;

    exit();
}

Hope you can help me, the session thing is to count up something

Upvotes: 0

Views: 2046

Answers (1)

Mark Baker
Mark Baker

Reputation: 212522

$this->objWriter->save('tmp/controlling_'.$year.'_'.$suffix.'.xlsx'); 

saves to a file on your servers filesystem, and nothing is sent to the client browser.

If you want to send to the client's browser, change that line to:

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

just like in 01simple-download-xlsx.php in the /Tests or /Examples directory

Upvotes: 1

Related Questions