我有这个脚本可以将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,但实际使用导出功能的人无法访问不同的浏览器。
EDIT:
我忘了提及我正在运行 SSL(https)。如果我关闭 SSL,一切在 IE 上都工作正常,但是当 SSL 在 IE 上打开时,就会显示错误。
有什么想法可以让它在 SSL 下工作吗?
Headers
对于 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).'"');
尝试一下,希望它能起作用。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)