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

时间:2013-02-11 10:37:53

标签: php phpexcel

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

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();
}

希望你能帮助我,会议的事情就是要算上一些东西

1 个答案:

答案 0 :(得分:1)

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

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

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

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

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

一样