Home >Backend Development >PHP Problem >How to solve the problem of garbled characters in excel files exported by php
The solution to garbled php exported excel files: first process the relevant script data; then add the "ob_end_clean()" function before outputting the excel file. This function is used to clear the buffer and close the output buffer.
php export excel file garbled problem
Using PHP to export excel documents, sometimes The exported data will appear garbled inexplicably. Now I recommend a universal repair method
Without further ado, let’s go directly to the code
The core is to add ob_end_clean(() before outputting the excel file after processing the data. ) function; 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 red marked code above, 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 recommend on Baidu
: "PHP Tutorial"
Related introduction:
ob_end_clean — Clear (erase) the buffer and close the output buffer
Description
ob_end_clean (void): bool
This function discards the contents of the top-level output buffer and closes the buffer. If you want to further process the contents of the buffer, you must call ob_get_contents() before ob_end_clean(), because the buffer contents will be discarded when ob_end_clean() is called.
Return value
Returns TRUE on success, or FALSE on failure. The first reason for the error is that there is no active buffer at the time of the call, or the buffer cannot be deleted for some reason (perhaps for a special buffer).
Error/Exception
If the function fails, an E_NOTICE exception will be raised.
The above is the detailed content of How to solve the problem of garbled characters in excel files exported by php. For more information, please follow other related articles on the PHP Chinese website!