我有一个CSV导出脚本(enrdata_arch.php),它从现有数据库调用信息并将其导出为CSV格式。但是,出于某种原因,该脚本仅在Internet Explorer中返回以下错误:
Microsoft Office Excel无法访问文件“https://www.domain.com/admin/enrdata_arch.php”。有几个可能的原因:
**
**
<?php
$host = 'localhost';
$user = 'username';
$pass = 'password';
$db = 'db_name';
$table = 'table_name';
$archive = 'archive_name';
$file = 'export';
$link = mysql_connect($host, $user, $pass) or die("Can not connect.".mysql_error());
mysql_select_db($db) or die("Can not connect.");
$result = mysql_query("SHOW COLUMNS FROM ".$table."");
$i = 0;
if (mysql_num_rows($result) > 0) {
while ($row = mysql_fetch_assoc($result)) {
$csv_output .= $row['Field'].", ";
$i++;
}
}
$csv_output .= "\n";
$values = mysql_query("SELECT * FROM ".$table."");
while ($rowr = mysql_fetch_row($values)) {
for ($j=0;$j<$i;$j++) {
$csv_output .= '"'.$rowr[$j].'",';
}
$csv_output .= "\n";
}
$filename = $file."_".date("Y-m-d",time());
header("Content-type: application/vnd.ms-excel");
header("Content-disposition: csv" . date("Y-m-d") . ".csv");
header( "Content-disposition: filename=".$filename.".csv");
print $csv_output;
//transfers old data to archive
$transfer = mysql_query('INSERT INTO '.$archive.' SELECT * FROM '.$table) or die(mysql_error());
//empties existing table
$query = mysql_query('TRUNCATE TABLE '.$table) or die(mysql_error());
mysql_close($link);
exit;
?>
似乎脚本只是在使用IE时才尝试保存并打开PHP文件。有什么想法吗?
答案 0 :(得分:1)
SSL的标题是IE的已知问题
我用
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
这是'Pragma:public,这是IE / ssl的关键
CSV文件的内容类型为
text/csv
或
text/plain
而不是
application/vnd.ms-excel
具体是MS Excel .xls speardsheets