php 到处excel 乱码,php导出Excel乱码怎么解决?
php導(dǎo)出Excel亂碼的解決方法就是在PHP處理完數(shù)據(jù)之后,輸出excel文件之前,使用“ob_end_clean()”函數(shù)處理一下,再進(jìn)行輸出即可。
php導(dǎo)出Excel亂碼怎么解決?
使用PHP導(dǎo)出excel文檔,有時(shí)候莫名其妙就會(huì)出現(xiàn)導(dǎo)出的數(shù)據(jù)亂碼,現(xiàn)在推薦一個(gè)萬(wàn)能修補(bǔ)大法
話不多說,直接上代碼
核心就是在處理完數(shù)據(jù)之后,輸出excel文件之前 添加 ob_end_clean()函數(shù);具體見示例代碼,此處只羅列部分代碼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();//解決亂碼核心 就在此處添加此函數(shù)
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;
如果此方法還是不行,請(qǐng)嘗試使用iconv()函數(shù),具體使用此處不做詳解,請(qǐng)自行百度
說明:ob_end_clean — 清空(擦除)緩沖區(qū)并關(guān)閉輸出緩沖
iconv — 字符串按要求的字符編碼來(lái)轉(zhuǎn)換
更多相關(guān)知識(shí),請(qǐng)?jiān)L問 PHP中文網(wǎng)!!
本文原創(chuàng)發(fā)布php中文網(wǎng),轉(zhuǎn)載請(qǐng)注明出處,感謝您的尊重!
總結(jié)
以上是生活随笔為你收集整理的php 到处excel 乱码,php导出Excel乱码怎么解决?的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 元类--用不上的先了解
- 下一篇: WSGI直观形象的了解一下