Home  >  Article  >  Backend Development  >  How to solve the problem of garbled characters exported to excel by php

How to solve the problem of garbled characters exported to excel by php

藏色散人
藏色散人Original
2020-10-30 10:18:122469browse

The solution to the garbled code exported by php: first open the corresponding PHP code file; then add the ob_end_clean function after processing the data and before outputting the excel file to solve the garbled code problem.

How to solve the problem of garbled characters exported to excel by php

Recommended: "PHP Video Tutorial"

php export excel garbled code

Using PHP to export excel documents, sometimes the exported data will be garbled for no reason. Now I recommend a universal repair method

Not much to say, just go to the code

The core is processing After completing the data, add the ob_end_clean() function before outputting the excel file; see the sample code for details, only part of the code is listed here

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;

As shown in the above code, if this method still does not work, please try to use the iconv() function , the specific use will not be explained in detail here, please Baidu

The above is the detailed content of How to solve the problem of garbled characters exported to excel by 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