Home  >  Article  >  Backend Development  >  What should I do if garbled characters appear when exporting Excel from PHP?

What should I do if garbled characters appear when exporting Excel from PHP?

王林
王林Original
2020-11-04 15:14:032474browse

Solution to the garbled code when exporting excel in php: After processing the data and before outputting the excel file, use the ob_end_clean() function to clear the buffer and close the output buffer.

What should I do if garbled characters appear when exporting Excel from PHP?

Solution:

After processing the data, use the ob_end_clean() function to process it before outputting the excel file.

ob_end_clean clears (erases) the buffer and closes output buffering. Returns TRUE on success, or FALSE on failure.

(Recommended tutorial: java video tutorial)

Part of the code is as follows:

foreach ($licenseList as $key => $item) {
                    $objPHPExcel->setActiveSheetIndex(0)
                        ->setCellValue('A' . ($key + 2), $item["company_name"])
                        ->setCellValue('B' . ($key + 2), $item["user_name"])
                        ->setCellValue('C' . ($key + 2), $item["order_number"])
                        ->setCellValue('D' . ($key + 2), $item['apply_type']==2 ? 'official':'trial')
                        ->setCellValue('E' . ($key + 2), $item["license_key"])
                        ->setCellValue('F' . ($key + 2), $statusArr[$item['license_status']])->setCellValue('G' . ($key + 2), $item["user_email"])
                        ->setCellValue('H' . ($key + 2), date('y/m/d H:i:s', strtotime($item['insert_time'])));
                }

                $objPHPExcel->getActiveSheet()->setTitle('Simple');
                $objPHPExcel->setActiveSheetIndex(0);
                ob_end_clean();//解决乱码核心 就在此处添加此函数
                header('Content-Type: application/vnd.ms-excel');
                header('Content-Disposition: attachment;filename="test_list.xls"');
                header('Cache-Control: max-age=0');
                header('Cache-Control: max-age=1');
                header('Expires: Mon, 26 Jul 1997 05:00:00 GMT');
                header('Last-Modified: ' . gmdate('D, d M Y H:i:s') . ' GMT');
                header('Cache-Control: cache, must-revalidate');
                header('Pragma: public');

                $objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
                $objWriter->save('php://output');
                exit;

Related recommendations: php training

The above is the detailed content of What should I do if garbled characters appear when exporting Excel from PHP?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn