在EXCEL中打开创建的文件时出现PHPExcel错误


PHPExcel error when opening created File in EXCEL

我用一个php脚本创建了一个简单的.xlsx文件,该脚本使用PHPExcel库。但当我想在Win7上的MS Excel 2010中打开它时,我收到一条错误消息,文件格式错误或文件已损坏。从互联网上尝试了几种可能的解决方案,但对我来说都不起作用。

公共函数createControllingFile($后缀){$this->PHPExcel=新的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();
}

希望你能帮助我,会话的事情是计数一些

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

保存到服务器文件系统上的文件中,并且不会向客户端浏览器发送任何内容。

如果你想发送到客户端的浏览器,请将该行更改为:

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

就像在/Tests或/Examples目录中的01simple-download-xlsx.php一样