PHPExcel 导出 xls 乱码
ringa_lee
ringa_lee 2017-05-16 13:03:35
0
2
719

使用 PHPExcel 的 save 方法时,

// 保存为 xls 文件,但是这个只是保存到服务器上了,还需要手动下载到本地
$writer->save('商品列表.xls');

直接下载:

        if ($this->fileVersion == 'Excel5') {
            header('Content-Type: application/vnd.ms-excel');   // 输出xls格式的文件
            header('Content-Disposition: attachment;filename="' . $config['fileName'] . '.xls"'); //告诉浏览器输出文件的名称
        } else {
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');  // 输出xlsx格式的文件
            header('Content-Disposition: attachment;filename="' . $config['fileName'] . '.xlsx"'); //告诉浏览器输出文件的名称
        }

        // If you're serving to IE 9, then the following may be needed
        header('Cache-Control: max-age=1');

        // If you're serving to IE over SSL, then the following may be needed
        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

        $writer->save('php://output');

通过 php://output 可以直接下载导出的 xls 文件,并且不会在服务器上生成 xls 文件;
但是,在下载的时候,直接报错,这个错误是在浏览器开发者工具中的 network 中出现的,也没有触发文件下载操作

一直没查到这个乱码是怎样造成的,如果直接在 header 之前,print_r($writer); exit; 看到的对象 $writer 是没有乱码的

很困惑这个问题

ringa_lee
ringa_lee

ringa_lee

모든 응답(2)
習慣沉默

이 방법을 사용하면 내보낸 파일을 직접 다운로드할 수 있지만 Ajax를 사용할 수는 없습니다.

Ty80

header('Content-Type: application/vnd.ms-excel');改为header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');改为header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet; charset=UTF-8');

최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!