美文网首页
解决header输出Excel文件名乱码

解决header输出Excel文件名乱码

作者: henryspace | 来源:发表于2018-08-06 16:11 被阅读0次

正常仅需将utf-8转为gbk即可,IE内核的如下处理,PHP代码如下:

//
$ua = $_SERVER['HTTP_USER_AGENT'];
$ua = strtolower($ua);
if(preg_match('/msie/', $ua) || preg_match('/edge/', $ua) || preg_match('/trident/', $ua)) { //判断是否为IE或Edge浏览器
    $filename = str_replace('+', '%20', urlencode($filename)); //使用urlencode对文件名进行重新编码
}
//2、浏览器保存
ob_end_clean();
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
header("Content-Type:application/force-download");
header("Content-Type:application/octet-stream");
header("Content-Type:application/download");

header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); //告诉浏览器输出07Excel文件
//header('Content-Type:application/vnd.ms-excel');//告诉浏览器将要输出Excel03版本文件
header('Content-Disposition: attachment;filename=' . $filename . '.xlsx'); //告诉浏览器输出浏览器名称
header('Cache-Control: max-age=0'); //禁止缓存
header("Content-Transfer-Encoding:binary");
$PHPWriter->save("php://output");

相关文章

网友评论

      本文标题:解决header输出Excel文件名乱码

      本文链接:https://www.haomeiwen.com/subject/qxjgvftx.html