【问题标题】:php generating excel and downloadingphp生成excel并下载
【发布时间】:2014-06-03 11:24:40
【问题描述】:

我有一个网站,它有一个简单的功能来生成包含数组数据的 Excel,然后提供给用户下载。

header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=spreadsheet.xls" );
echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n";
echo 'John' . "\t" . 'Doe' . "\t" . '555-5555' . "\n";

上面的代码是用来测试的,但是我在excel中只得到了网站的一些html代码,没有数据。

请问为什么会这样? 谢谢!

【问题讨论】:

  • 你到底从服务器得到了什么?
  • 你的脚本为我工作
  • @ZeroWorks 嗨,我得到了一些 html 代码和 js 代码。现在我改变了 mHouses 的建议,我看到了测试数据和整个源代码。
  • 如果您从中获取 html,那么您在某处有一些其他代码正在吐出该 html。

标签: php excel download


【解决方案1】:

确保在标头调用之前不发送任何内容。

// At the begginnig of script...
ob_start();

// ... do some stuff ...

ob_get_clean();

header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=spreadsheet.xls" );
echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n";
echo 'John' . "\t" . 'Doe' . "\t" . '555-5555' . "\n";
die();

如果您需要处理整个脚本,另一种方法:

<?php
// At the beggining...
ob_start();    
$content="";
$normalout=true;

// ... do some stuff ...

// i guess if some condition is true...
$content=ob_get_clean();
$normalout=false;
header( "Content-Type: application/vnd.ms-excel" );
header( "Content-disposition: attachment; filename=spreadsheet.xls" );
echo 'First Name' . "\t" . 'Last Name' . "\t" . 'Phone' . "\n";
echo 'John' . "\t" . 'Doe' . "\t" . '555-5555' . "\n";

// Here you could die() or continue...
ob_start();

// ... rest of execution ...

$content.=ob_get_clean();
if($normalout)
{
    echo($content);
} else {
      // Excel provided no output.
}
?>

这应该可行。

【讨论】:

  • 您好,感谢您的回复。是的,问题是我确实有其他内容,我想这就是为什么我得到带有 html 代码的 excel 文件的原因。 :(
  • 只需在发送任何内容之前加上ob_start();,然后在标头之前加上ob_get_flush();,它就会起作用。
  • 喜欢 ob_start(); header("内容类型:application/vnd.ms-excel"); header("内容配置:附件;文件名=spreadsheet.xls");回声“名字”。 "\t" 。 '姓' 。 "\t" 。 '电话' 。 "\n";回声“约翰”。 "\t" 。 'Doe' 。 "\t" 。 '555-5555' 。 "\n"; ob_get_flush();?
  • 不幸的是,它没有帮助。 :(
  • 嗨,我现在在 excel 中得到的是两条测试线,以及整个站点的源代码,如果我不在 echo 之后放置 die()。
【解决方案2】:

该代码似乎在 Chrome 上运行没有任何问题(当然,使用 php 标签)。

无论如何,这里有一个模板,我用它来导出正在发送的 POST 表。

<?php
        header("Content-Type: application/vnd.ms-excel");
        header("Expires: 0");
        $date = date('Y-m-d-Hi');
        header("content-disposition: attachment;filename=Excel_Report_$date.xls");
        $table = $_POST["table_info"];
?>

<html>
        <head>
                <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
        </head>
        <body>
                <table>
                        <tr><th>&nbsp;</th></tr>
                        <tr><td>&nbsp;</td></tr>
                        <?=$table?>
                </table>
        </body>
</html>

如果您要导出一些需要更多详细信息的报告,我会推荐 PHPExcel 作为迄今为止通过 PHP 代码管理 CSV/XML/XLS(X) 的最佳库。

【讨论】:

  • 您好,感谢您的回复。该应用程序在标题和容器中还有其他内容,所以我想这就是为什么我使用 html 代码而不是测试数据获得 excel。
  • 可能这就是原因,或者它没有将您的 html 信息格式化为表格。但是,您的纯代码确实可以在这里工作。祝你好运。
【解决方案3】:

您发送的文件是以制表符分隔的值,而不是 XLS(这是一种二进制格式)。

您可以将文件作为 CSV (Content-type="text/csv") 发送,并将文件格式化为 csv:

header("Content-Type: text/csv");
header("content-disposition: attachment;filename=my_document.csv");
echo '"First Name";"Last Name";"Phone"' . "\n";
echo '"John";"Doe";"555-123456"' . "\n";

【讨论】:

  • 您好,感谢您的帮助!好一点,至少我看到了测试数据,但我仍然在 excel 文件中看到了整个源代码。 :(
  • 尝试在最后一个回声之后放置一个“骰子”。可能是您的应用程序在 csv 之后发送了更多内容。
  • 是的,它有效。但是把 die() 放在那里并不是最优的,对吧
  • @user3382722 将 die() 放在那里可能不是最佳选择,但您必须做一些事情来停止脚本,或者至少停止向客户端发送更多数据。
猜你喜欢
  • 2011-04-11
  • 1970-01-01
  • 1970-01-01
  • 2015-10-01
  • 2015-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多