下载时PHP生成的Excel文件不同

时间:2021-07-02 10:07:54

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

我有一个PHP文件,使用http://pear.php.net/package/Spreadsheet_Excel_Writer/上的模块生成xls文件

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.

但是,当我下载文件时,它在Excel和OpenOffice中都包含大量垃圾数据。差异说,然后/ tmp文件夹中的二进制文件和下载的文件彼此不同。我猜它与标题或fpassthru有关,但我没有太多运气调试问题。

Any ideas on what the problem is?

关于问题是什么的任何想法?

4 个解决方案

#1


0  

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.

多个Content-Type标头是不必要的。你基本上是说这个文件同时是一个松饼,一个披萨和一个浅滩金牛座。您需要的只是application / octet-stream版本,除非您想要提供精确的mime类型。

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

同样,你是否有任何理由试图将fopen()返回的文件句柄转换为引用?

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


0  

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.

它可能只是一些PHP'错误'或甚至'通知'Spreadsheet_Excel_Writer正在产生,你甚至没有看到。或者它可能是一个结束'?>'标签,后面跟着简单的空格或换行符。

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.

我有一个类似的错误,在Web文件夹中生成的文件正在工作。但是使用标题('...')的传递给了我损坏的文件。这是因为在关闭'?>'标记之后,一个php文件末尾有一个空格。

#3


0  

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):

要编辑的文件(Spreadsheet_Excel_Writer包中的所有文件):

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

#4


0  

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

在生成excel文件的页面顶部添加以下代码

ob_clean();

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.

这将清除所有乱码数据。还要检查任何echo语句。如果存在echo语句,请删除它们。数据应始终以excel包指定的格式显示。

#1


0  

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.

多个Content-Type标头是不必要的。你基本上是说这个文件同时是一个松饼,一个披萨和一个浅滩金牛座。您需要的只是application / octet-stream版本,除非您想要提供精确的mime类型。

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

同样,你是否有任何理由试图将fopen()返回的文件句柄转换为引用?

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


0  

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.

它可能只是一些PHP'错误'或甚至'通知'Spreadsheet_Excel_Writer正在产生,你甚至没有看到。或者它可能是一个结束'?>'标签,后面跟着简单的空格或换行符。

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.

我有一个类似的错误,在Web文件夹中生成的文件正在工作。但是使用标题('...')的传递给了我损坏的文件。这是因为在关闭'?>'标记之后,一个php文件末尾有一个空格。

#3


0  

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):

要编辑的文件(Spreadsheet_Excel_Writer包中的所有文件):

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

#4


0  

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

在生成excel文件的页面顶部添加以下代码

ob_clean();

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.

这将清除所有乱码数据。还要检查任何echo语句。如果存在echo语句,请删除它们。数据应始终以excel包指定的格式显示。