首页 > 解决方案 > 从 Laravel 导出 Excel 而不安装任何库并获得奇怪的字符

问题描述

使用 Laravel 8

我的路线:

Route::get('socios/export', [SocioController::class, 'export'])->name('socios.export');

我的控制器方法:

public function export(Request $request)
    {
        $registros = User::socios()->get();
        $content = view('socios.excel', compact('registros'));
        
        $status = 200;
        $headers = [
            'Content-Type' => 'application/vnd.ms-excel; charset=utf-8',
            'Content-Disposition' => 'attachment; filename="filename.xls"',
        ];

        $response = response($content, $status, $headers);
        return $response;
    }

我的“socios.excel”视图:

<table>
    <thead>
        <tr>
            <th>ID</th>
            <th>name</th>
            <th>...</th>
        </tr>
    </thead>
    <tbody>
        @foreach ($registros as $registro)
            <tr>
                <td>{{ $registro->id }}</td>
                <td>{{ $registro->name }}</td>
                <td>...</td>
            </tr>
        @endforeach
    </tbody>
</table>

该文件导出正常,但像“Dirección”这样的字符串包含错误的字符:

在此处输入图像描述

我在内容类型标题中尝试了不同的字符集,但无法修复它。

一些帮助?

谢谢!

标签: phpexcellaravelencode

解决方案


推荐阅读