How to import/export Excel in Lavaral 10?

By admin, 5 六月, 2023

导出简单CVS文件可以用下面方法(不适用于有引号、换行符等情况。如果有中文,应该自己拼接字符串):

use Illuminate\Http\Response;
public function downloadExcel()
{
    $headers = [
        'Content-Type' => 'application/vnd.ms-excel',
        'Content-Disposition' => 'attachment; filename="file.xlsx"',
    ];
    $data = [
        ['name', 'email'],
        ['John Doe', 'johndoe@example.com'],
        ['Jane Doe', 'janedoe@example.com'],
    ];
    return new Response($data, 200, $headers);
}

装maatwebsite/excel的方法更好,但有时会装错版本,可以尝试以下命令

 composer require maatwebsite/excel:3.1.48 --with-all-dependencies

参考:

标签

评论

Restricted HTML

  • 允许的HTML标签:<a href hreflang> <em> <strong> <cite> <blockquote cite> <code> <ul type> <ol start type> <li> <dl> <dt> <dd> <h2 id> <h3 id> <h4 id> <h5 id> <h6 id> <img src>
  • 自动断行和分段。
  • 网页和电子邮件地址自动转换为链接。
验证码
This question is for testing whether or not you are a human visitor and to prevent automated spam submissions.
请输入"Drupal10"

最新评论