PHP generated Excel file is different when downloaded

。_饼干妹妹 提交于 2020-01-14 14:13:25

问题


I have a PHP file that generates xls files using the module found at http://pear.php.net/package/Spreadsheet_Excel_Writer/

I can create the sample document just fine and when I open it, it looks fine.

My next step it to turn it into a downloadable link. To do that, I did this:

    $mimeType = "application/vnd.ms-excel";
    $file_name = "test.xls";
    $file_path = "/tmp/".$file_name;
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");
    header('Content-Type: application/' . $mimeType);
    header('Content-Length: '.$size);
    header("Content-Disposition: attachment;filename=$file_name ");
    header("Content-Transfer-Encoding: binary ");

    // open the file in binary read-only mode
    // display the error messages if the file can´t be opened
    $file = & fopen($file_path, 'rb');

    if ($file) {
        // stream the file and exit the script when complete
        fpassthru($file);
        exit;

    } else {
        echo $err;
    }

When I download the file however, it contains a lot of garbage data both in Excel and OpenOffice. The diff says that then binary file in the /tmp folder and the downloaded file are different from each other. I'm guessing that it has something to do with the headers or with fpassthru but I haven't had much luck with debugging the issue.

Any ideas on what the problem is?


回答1:


The multiple Content-Type headers are uncessary. You're essentially saying that the file is a muffin and a pizza and a ford taurus all at the same time. All you need is the application/octet-stream version, unless you want to serve up the exact mime type.

As well, is there any reason you're trying to turn the file handle returned by fopen() into a reference?

Try something simpler:

<?php

header("Content-Type: application/octet-stream");
header("Content-Disposition: attachment;filename=$file_name");

readfile("/tmp/test.xls");

exit();
?>

and see if that does any better.




回答2:


Just make sure that you don't send ANYTHING out to the browser BEFORE the actual file content gets send.

It might just be some php 'error' or even 'notice' that Spreadsheet_Excel_Writer is producing and you don't even see. Or it might be a closing '?>' tag thats followed by s simple space or newline.

I had a similar error where the file that was generated inside the web folders were working. However the delivery using header('...') gave me corrupt files. This was due to a single space at the end of one php file after the closing '?>' tag.




回答3:


I am using the same library and I just discovered that the files in the library itself are creating the whitespace.

Solution: In the following files remove the whitespace at the end of the file, or remove the ?> closing tag at the end.

Files to edit (all files in the Spreadsheet_Excel_Writer package):

Writer.php 
Workbook.php
Worksheet.php
PPS.php
Parser.php
OLE.php
Parser.php
File.php
BIFFWriter.php
Validator.php
Root.php



回答4:


Add the following code at the top of the page where the excel file is generated

ob_clean();

This would clear all the gibberish data.Also check for any echo statements.If echo statements are present, remove them. The data should always present in format specified by excel package.



来源:https://stackoverflow.com/questions/3704340/php-generated-excel-file-is-different-when-downloaded

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!