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

使用 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は使用できません

いいねを押す +0
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');

いいねを押す +0
人気のチュートリアル
詳細>
最新のダウンロード
詳細>
ウェブエフェクト
公式サイト
サイト素材
フロントエンドテンプレート
私たちについて 免責事項 Sitemap
PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!