php导出excel乱码的解决方法

发布时间:2020-11-02 10:02:29 作者:小新
来源:亿速云 阅读:282

这篇文章将为大家详细讲解有关php导出excel乱码的解决方法,小编觉得挺实用的,因此分享给大家做个参考,希望大家阅读完这篇文章后可以有所收获。

php导出excel乱码的解决办法:首先打开相应的PHP代码文件;然后在处理完数据之后,以及输出excel文件之前添加ob_end_clean函数即可解决乱码问题。

php导出excel乱码

使用PHP导出excel文档,有时候莫名其妙就会出现导出的数据乱码,现在推荐一个万能修补大法

话不多说,直接上代码

核心就是在处理完数据之后,输出excel文件之前 添加 ob_end_clean()函数;具体见示例代码,此处只罗列部分代码

foreach ($licenseList as $key => $item) {
                    $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A' . ($key + 2), $item["company_name"])
                        ->setCellValue('B' . ($key + 2), $item["user_name"])
                        ->setCellValue('C' . ($key + 2), $item["order_number"])
                        ->setCellValue('D' . ($key + 2), $item['apply_type']==2 ? 'official':'trial')
                        ->setCellValue('E' . ($key + 2), $item["license_key"])
                        ->setCellValue('F' . ($key + 2), $statusArr[$item['license_status']])->setCellValue('G' . ($key + 2), $item["user_email"])
                        ->setCellValue('H' . ($key + 2), date('y/m/d H:i:s', strtotime($item['insert_time'])));
                }

                $objPHPExcel->getActiveSheet()->setTitle('Simple');
                $objPHPExcel->setActiveSheetIndex(0);
                ob_end_clean();//解决乱码核心 就在此处添加此函数
                header('Content-Type: application/vnd.ms-excel');
                header('Content-Disposition: attachment;filename="test_list.xls"');
                header('Cache-Control: max-age=0');
                header('Cache-Control: max-age=1');
                header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
                header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
                header('Cache-Control: cache, must-revalidate');
                header('Pragma: public');

                $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
                $objWriter->save('php://output');
                exit;

关于php导出excel乱码的解决方法就分享到这里了,希望以上内容可以对大家有一定的帮助,可以学到更多知识。如果觉得文章不错,可以把它分享出去让更多的人看到。

推荐阅读:
  1. php乱码的解决方法是什么
  2. php运行乱码的解决方法

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

php excel ce

上一篇:php禁止ip访问的方法

下一篇:用c语言编写动态烟花的方法

相关阅读

您好,登录后才能下订单哦!

密码登录
登录注册
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》