laravel 5.2 使用phpexcel 导出 excel文件出问题
PHP中文网
PHP中文网 2017-04-11 10:32:31
[PHP讨论组]

1.出现的问题

2.导出代码(首先声明导出使用PHPExcel_IOFactory::createWriter($objExcel, 'Excel5')->save(outpath)导出正常)
`

        //导出的文件名
        $outputFileName =iconv ( 'UTF-8', 'gb2312', date('Y-m-d_H-i-s',time()).'.xls' );
        //文件直接输出到浏览器
        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/vnd.ms-excel');
        header ( 'Content-Type:application/octet-stream');
        header ( 'Content-Type:application/download');
        header ( 'Content-Disposition:attachment;filename='. $outputFileName );
        header ( 'Content-Transfer-Encoding:binary');
        ob_end_clean();
        \PHPExcel_IOFactory::createWriter($objExcel, 'Excel5')->save('php://output');
        //记录日志
        $this->optionData("导出文件",session('auth')['userName']."执行了导出文件");
        ob_end_clean();
        return '0';`

3.只要使用php://out就不能输出文件就出现1的情况 但是不使用php://out
就没问题
4.现在的需求是导出时,弹出对话框让用户指定保存位置,提高交互的体验,使用 PHPExcel_IOFactory::createWriter($objExcel, 'Excel5')->save(outpath);
导出后保存到public/upload下面了
5.环境 laravel 5.2 php版本5.5 win7 64bit 大概信息就这样 如有需要可以提供更多信息

PHP中文网
PHP中文网

认证高级PHP讲师

全部回复(3)
黄舟

laravel 用的不是这个包吗 maatwebsite/excel

巴扎黑

Laravel 有现成封装的 excel 的库吧,不需要想你这么写吧

阿神

将第二个ob_end_clean();删除就好了,本地测试可以下载

热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送

Copyright 2014-2026 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号