IE中Excel导出问题

发布于 2024-11-08 05:55:58 字数 1780 浏览 0 评论 0原文

我有这个脚本可以将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 下工作吗?

I have this script to export mysql data to excel. I have tried
everything but I am not able to get this script to work for IE. The
script downloads the data using FireFox or Chrome but IE fails and
says:

Internet Explorer cannot download list_view.php from www.mysite.com. (this is the site on which the file resides).

Internet Explorer was not able to
open this Internet site. The requested site is either unavailable or
cannot be found. Please try again later.

here is the code that I am using:

$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        

I know there maybe some people suggesting not to use IE, but the person who is actually using the export function do not have access to different browser.

EDIT:

I forgot to mention that I am running a SSL(https). If i turn SSL off everything is working fine on IE, but the moment SSL is on the IE shows an error.

Any ideas how to make it work under SSL?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

哽咽笑 2024-11-15 05:55:58

标头

对于 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).'"');

尝试一下,希望它能够工作。

Headers

Headers can be tricky with IE. Here's the thing, you should set it not to cache like this:

 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");

Now there can be an issue with that if your server time is not set to the right timezone, this may actually have the opposite effect and force IE to cache it. Make sure your timezone is set for the correct timezone you are in. Is it a shared server? do you have ssh access??

Excel Headers

Now what you need is to provide the set of headers for 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).'"');

Try that, hopefully it should work.

七度光 2024-11-15 05:55:58

您是否可以将所有内容输出为 CSV,或者手动格式化它,或者类似 implode('","', $row) ,而不是将所有内容输出为表格?保持内容类型标头不变。我已经成功地做到了这一点,即使在 IE 中也是如此。

Instead of outputting everything as a table, can you just output it as CSV, either manually formatting it, or something like implode('","', $row)? Leave the content-type header the way it is. I've had success doing that, even in IE.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文