问题
I have a script to output a mysql result to excel, which is working just fine in all browsers. However, I need the script to also work on iPad, opening the excel in Safari. At this moment, the script just "hangs" on the iPad, it keeps loading. Is this a headers problem? The code headers at this moment:
header( 'Content-Transfer-Encoding: binary' );
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header('Content-Type: application/vnd.ms-excel');
//header("Content-Type: application/octet-stream");
header('Content-Disposition: attachment;filename="' . $_POST['Client_FirstName'] . ' ' . $_POST['Client_LastName'] . ' ' . $_POST['month'] . '-' . $_POST['year'] . '.xlsx"');
header('Cache-Control: max-age=0');
header("Pragma: public");
header("Expires: 0");
回答1:
Apparently it was the application/vnd.ms-excel header that caused the problem. Setting it to octet-stream solved the issue, but then caused problems in IE8. So I used this code to totally solve the problem:
if(stristr($_SERVER['HTTP_USER_AGENT'], 'ipad') OR stristr($_SERVER['HTTP_USER_AGENT'], 'iphone') OR stristr($_SERVER['HTTP_USER_AGENT'], 'ipod'))
{
header("Content-Type: application/octet-stream");
}else{
header('Content-Type: application/vnd.ms-excel');
}
来源:https://stackoverflow.com/questions/11449989/php-exporting-to-excel-on-ipad