PHP 下载 excel 文件损坏

标签 php excel download corrupt

我有一个 excel 文件,我希望用户能够从我的服务器下载该文件。我在这里看了很多问题,但我找不到正确下载没有损坏的文件的方法。我假设它是标题,但我还没有它们的有效组合。这就是我现在拥有的,在我收到的损坏文件中,我可以看到我想要的电子表格的列名,但它全乱了。

$filename = '/var/www/web1/web/public/temporary/Spreadsheet.xls';        
header("Content-type: application/octet-stream");
header("Content-type: application/vnd-ms-excel");
header("Content-Disposition: attachment; filename=ExcelFile.xls;");
header("Pragma: no-cache");
header("Expires: 0");
readfile($filename);

编辑:解决方案 我忘了补充一点,我使用的是 Zend,它在尝试使用 native php 方法时破坏了文件。我完成的代码是在我的 Controller 中放置一个指向另一个操作的链接,并从那里下载文件

public function downloadAction(){
        $file = '/var/www/web1/web/public/temporary/Spreadsheet.xls';
        header('Content-Type: application/vnd.ms-excel');
    header('Content-Disposition: attachment; filename="Spreadsheet.xls"');
    readfile($file);

    // disable the view ... and perhaps the layout
    $this->view->layout()->disableLayout();
        $this->_helper->viewRenderer->setNoRender(true);


    }

最佳答案

尝试这样做

 ob_get_clean();
 echo file_get_contents($filename);
 ob_end_flush();

关于PHP 下载 excel 文件损坏,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9084011/

相关文章:

php - 如何在 PHP 中显示 MSSQL varbinary(3072) 图像?

php - MAMP localhost 包含和 cookie 设置功能

vba - 如何在新单元格中生成随机数

Excel:使用数组公式搜索特定字符串中的字符串列表?

java - 实际下载链接

php - WordPress 在内存中存储身份验证 token

php - 我如何在 while 循环中回显来自 mysql 的两条不同记录

python - 从 Excel 到元组列表

java - 下载已通过 Liferay 门户中的动态数据列表上传的文件

http - 如何管理 ffmpeg 下载 m3u8 文件?