【问题标题】:Excel export problem in IEIE中的Excel导出问题
【发布时间】:2011-05-19 20:32:39
【问题描述】:

我有这个脚本可以将 mysql 数据导出到 excel。我试过了 一切,但我无法让这个脚本为 IE 工作。这 脚本使用 FireFox 或 Chrome 下载数据,但 IE 失败并且 说:

Internet Explorer 无法从 www.mysite.com 下载 list_view.php。 (这是文件所在的站点)。

Internet Explorer 无法 打开此 Internet 站点。请求的站点不可用或 找不到。请稍后再试。

这是我正在使用的代码:

$sql = "SELECT...."
     $result = mysql_query($sql) 
or die("\nError Retrieving Records.<hr />sql: $sql<hr />ERROR:".mysql_error()); 

if(mysql_num_rows($result) > 0){

    // build a filename that excel will use to save it as
    $filename=$name."_".dateFromDB($_GET['confDate']).".xls";

    // headers to force the open/save into Excel

    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=$filename");
    header("Pragma: no-cache");
    header("Expires: 0");}?>
        <table>     
    <thead>
    <tr>
        <th>address</th>
        <th>phone</th>
        <th>email</th>  
        <th>status</th>
    </tr>
    </thead>
    <tbody>
    <?php // loop over items
    while ($row = mysql_fetch_array ($result, MYSQL_ASSOC)) { ?>
        <tr>
            <td><?=$row['address'];?></td>
            <td><?=$row['phone'];?></td>
            <td><?=$row['email'];?></td>
            <td><?=$row['status'];?></td>
        </tr><?php
    }?>
    </tbody>
    </table><?php 
    exit(); // exit or it will throw an error when it tries to continue        

我知道可能有人建议不要使用IE,但实际使用导出功能的人无法访问其他浏览器。

编辑:

我忘了提到我正在运行 SSL(https)。如果我关闭 SSL,IE 上一切正常,但 SSL 在 IE 上的那一刻显示错误。

任何想法如何使它在 SSL 下工作?

【问题讨论】:

  • IE 有一些非常愚蠢的缓存逻辑,因为一切都通过浏览器缓存传递,包括下载。使用 no-cache pragma,您可以告诉 IE 在下载完成后立即删除下载,然后才会弹出保存文件对话框。
  • 那你有什么建议?我试过没有那条线,但仍然不能为 IE 工作
  • 同时删除 expires 标头。
  • 仍然无法工作...有没有我可以使用的工作脚本?
  • 你可以尝试使用PHPExcel并生成一个真正的Excel文件,而不是一些伪装成Excel的html。

标签: php mysql excel ssl export-to-excel


【解决方案1】:

标题

标题在 IE 中可能很棘手。事情是这样的,你应该把它设置为不要像这样缓存:

 ini_set('zlib.output_compression','Off');
        header('Pragma: public');
        header("Expires: Sat, 26 Jul 1997 05:00:00 GMT");                  // Date in the past
        //header('Last-Modified: '.gmdate('D, d M Y H:i:s') . ' GMT');
        header('Cache-Control: no-store, no-cache, must-revalidate');     // HTTP/1.1
        header('Cache-Control: pre-check=0, post-check=0, max-age=0');    // HTTP/1.1
        header ("Pragma: no-cache");
        header("Expires: 0");

现在可能会出现问题,如果您的服务器时间未设置为正确的时区,这实际上可能会产生相反的效果并强制 IE 对其进行缓存。确保您的时区设置为您所在的正确时区。它是共享服务器吗?你有 ssh 访问权限吗??

Excel 标题

现在您需要为 IE 提供一组标头

header('Content-Transfer-Encoding: none');
        header('Content-Type: application/vnd.ms-excel;');                 // This should work for IE & Opera
        header("Content-type: application/x-msexcel");                    // This should work for the rest
        header('Content-Disposition: attachment; filename="'.basename($filename).'"');

试试吧,希望它会工作。

【讨论】:

    【解决方案2】:

    除了将所有内容输出为表格之外,您是否可以将其输出为 CSV,手动格式化,或类似implode('","', $row)?将内容类型标题保持原样。我在这方面取得了成功,即使在 IE 中也是如此。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2012-04-10
      • 2021-05-25
      • 1970-01-01
      • 1970-01-01
      • 2023-03-19
      • 1970-01-01
      • 1970-01-01
      • 2012-02-12
      相关资源
      最近更新 更多