search

Home  >  Q&A  >  body text

PHPExcel exports xls garbled characters

When using PHPExcel’s save method,

// Save it as an xls file, but this is only saved to the server and needs to be manually downloaded locally.
$writer->save('product list.xls');

Download directly:

 if ($this->fileVersion == 'Excel5') {
            header('Content-Type: application/vnd.ms-excel'); // Output a file in xls format
            header('Content-Disposition: attachment;filename="' . $config['fileName'] . '.xls"'); //Tell the browser the name of the output file
        } else {
            header('Content-Type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet'); // Output files in xlsx format
            header('Content-Disposition: attachment;filename="' . $config['fileName'] . '.xlsx"'); //Tell the browser the name of the output file
        }

        // 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');

The exported xls file can be downloaded directly through php://output, and the xls file will not be generated on the server;
However, when downloading, an error is reported directly. This error is in the browser developer tools appeared in the network, and did not trigger the file download operation

< /p>

I have never found out how this garbled code is caused. If it is directly before the header, print_r($writer); exit; does not see the object $writer Garbled

I’m very confused about this question

ringa_leeringa_lee2789 days ago807

reply all(2)I'll reply

  • 習慣沉默

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

    The problem has been solved. This method can directly download the exported file, but Ajax cannot be used

    reply
    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');

    reply
    0
  • Cancelreply