ホームページ  >  に質問  >  本文

PHPExcel 导出 xls 乱码

使用 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_leeringa_lee2683日前721

全員に返信(2)返信します

  • 習慣沉默

    習慣沉默2017-05-16 13:05:35

    この方法ではエクスポートされたファイルを直接ダウンロードできますが、Ajaxは使用できません

    返事
    0
  • PHP中文网

    PHP中文网2017-05-16 13:05:35

    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
  • キャンセル返事