PHPExcel返回损坏的二进制文件 [英] PHPExcel return a corrupted binary file

查看:286
本文介绍了PHPExcel返回损坏的二进制文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想编写一个参考xls文件并检索结果. 当我使用单个文件进行测试时,它可以工作,但是当参考文件很复杂时,这会返回带有数据种类的损坏文件:

I want to write a reference xls file and retrieve the result. When I tested with a single file, it works but when the reference file is complex, this returns me a corrupt file with with kinds of data:

首先是这样的:

Which is at the beginning like this:

我的php文件是:

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
date_default_timezone_set('Europe/London');

if (PHP_SAPI == 'cli')
    die('This example should only be run from a Web Browser');

/** Include PHPExcel */
require_once '../phpexcel/Classes/PHPExcel.php';
$objPHPexcel = PHPExcel_IOFactory::load('../upload/ref/fileref.xls');

$objWorksheet = $objPHPexcel->getActiveSheet();

$objWorksheet = $objPHPexcel->getActiveSheet();
$objWorksheet->getCell('B1')->setValue('toto');
$objWorksheet->getCell('B3')->setValue('toto');







header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="newfiletodownload.xls"');;
header('Cache-Control: max-age=0');
header('Cache-Control: max-age=1');
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT');
header ('Cache-Control: cache, must-revalidate');
header ('Pragma: public');

$objWriter = PHPExcel_IOFactory::createWriter($objPHPexcel, 'Excel5');
$objWriter->save('php://output');

您有解决方案吗? 谢谢,Neyoh

Do you have solution ? Thanks, Neyoh

推荐答案

您可以尝试将ob_end_clean();放在header(...)部分之前吗?

Can you try to put ob_end_clean(); just before the header(...) part?

这适用于遇到相同问题的人.请参阅此帖子的可接受答案上的评论:

This worked for someone with the same problem. See the comment at the accepted answer of this post: PHPExcel generated excel file not working. (File format or extension is invalid ).

注意:

使用ob_end_clean()只是解决此问题的方法.主要问题是将其他不必要的输出发送到PHPExcelWriter,这会导致文件混乱.

Using ob_end_clean() is just a work around for this problem. The main problem is that additional and unnecessary outputs are sent to the PHPExcelWriter that causes the file to get messed up.

这篇关于PHPExcel返回损坏的二进制文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆