laravel-admin 导出CSV乱码
使用laravel-admin默认的导出按钮,导出的中文有乱码
解决办法
文件位置:vendor\encore\laravel-admin\src\Grid\Exporters\CsvExporter.php
public function export()
{
$filename = $this->getTable().'.csv';
$headers = [
'Content-Encoding' => 'UTF-8',
'Content-Type' => 'text/csv;charset=UTF-8',
'Content-Disposition' => "attachment; filename=\"$filename\"",
];
print(chr(0xEF).chr(0xBB).chr(0xBF));//设置utf-8 + bom ,处理汉字显示的乱码
response()->stream(function () {
$handle = fopen('php://output', 'w');
$titles = [];
$this->chunk(function ($records) use ($handle, &$titles) {
if (empty($titles)) {
$titles = $this->getHeaderRowFromRecords($records);
// Add CSV headers
fputcsv($handle, $titles);
}
foreach ($records as $record) {
fputcsv($handle, $this->getFormattedRecord($record));
}
});
// Close the output stream
fclose($handle);
}, 200, $headers)->send();
exit;
}
增加了:print(chr(0xEF).chr(0xBB).chr(0xBF));//设置utf-8 + bom ,处理汉字显示的乱码
这一行