{"id":5081,"date":"2019-11-22T03:15:08","date_gmt":"2019-11-21T18:15:08","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=5081"},"modified":"2020-09-26T06:01:54","modified_gmt":"2020-09-25T21:01:54","slug":"%e3%80%90laravel%e3%80%91db%e3%83%87%e3%83%bc%e3%82%bf%e3%82%92csv%e3%83%bbexcel%e3%81%a7%e3%82%a8%e3%82%af%e3%82%b9%e3%83%9d%e3%83%bc%e3%83%88%e3%81%99%e3%82%8b","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=5081","title":{"rendered":"\u3010Laravel\u3011DB\u30c7\u30fc\u30bf\u3092CSV\u30fbExcel\u3067\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b"},"content":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u3053\u306e\u9593<a href=\"https:\/\/blog.capilano-fw.com\/?p=5022\">DB\u30c7\u30fc\u30bf\u3092CSV\u3067\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b\u6a5f\u80fd\u3092\u3064\u304f\u308b<\/a>\u3068\u3044\u3046\u8a18\u4e8b\u3092\u516c\u958b\u3057\u307e\u3057\u305f\u304c\u3001\u5b9f\u306f\u3044\u3064\u3082\u3088\u308a\u5c11\u3057\u3060\u3051\u30a2\u30af\u30bb\u30b9\u304c\u591a\u304b\u3063\u305f\u3067\u3059\ud83d\ude0a<\/p>\n<p>\u305d\u3057\u3066\u3001\u305b\u3063\u304b\u304f\u30a4\u30f3\u30dd\u30fc\u30c8\uff08\u30a2\u30c3\u30d7\u30ed\u30fc\u30c9\uff09\u3092\u7d39\u4ecb\u3057\u305f\u306e\u3067\u3001\u9006\u306e\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\uff08\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\uff09\u3082\u958b\u767a\u3057\u3066\u307f\u305f\u304f\u306a\u3063\u305f\u306e\u3067\u3001\u4eca\u56de\u306f\u3053\u306e\u8a71\u984c\u3092\u304a\u5c4a\u3051\u3057\u307e\u3059\u3002<\/p>\n<p><strong style=\"font-size: 35px;\">DB\u30c7\u30fc\u30bf\u3092CSV or Excel\u5f62\u5f0f\u3067\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b<\/strong><\/p>\n<p>\u3067\u3059\u3002<\/p>\n<p>\u3082\u3061\u308d\u3093\u696d\u52d9\u5185\u5bb9\u3067\u5909\u308f\u3063\u3066\u304f\u308b\u3068\u306f\u601d\u3044\u307e\u3059\u304c\u3001\u3088\u304f\u3042\u308b\u5f62\u5f0f\u3068\u3057\u3066\u300c\u30a6\u30a7\u30d6\u4e0a\u3067\u9867\u5ba2\u3084\u5546\u54c1\u30c7\u30fc\u30bf\u3092\u7ba1\u7406\u3057\u3001\u5fc5\u8981\u306a\u3068\u304d\u306b\u305d\u308c\u3089\u306e\u30c7\u30fc\u30bf\u3092\u53d6\u308a\u51fa\u3057\u3066\u30ed\u30fc\u30ab\u30eb\u74b0\u5883\u3067\u52a0\u5de5\uff06\u30d7\u30ea\u30f3\u30c8\u30a2\u30a6\u30c8\u3059\u308b\u300d\u3068\u3044\u3046\u6d41\u308c\u304c\u3042\u3063\u305f\u308a\u3057\u307e\u3059\u3002<\/p>\n<p>\u305d\u306e\u969b\u306b\u3001\u3044\u3061\u3044\u3061<code>MySQL<\/code>\u306a\u3069\u304b\u3089\u30c7\u30fc\u30bf\u3092\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b\u3088\u308a\u306f\u30b5\u30a4\u30c8\u306b\u30ed\u30b0\u30a4\u30f3\u3057\u3066\u30af\u30ea\u30c3\u30af\u3059\u308b\u3060\u3051\u3067<code>CSV<\/code>\u3084<code>Excel<\/code>\u30d5\u30a1\u30a4\u30eb\u304c\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3067\u304d\u305f\u3089\u3001\u3068\u3066\u3082\u4fbf\u5229\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u305c\u3072\u53c2\u8003\u306b\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u306d\ud83d\ude0a\u2728<\/p>\n<p><strong>\u3010\u8ffd\u8a18\uff1a2020.9.26\u3011<\/strong>\u300c\uff11\u884c\u76ee\u306b\u9805\u76ee\u540d\u3092\u5165\u308c\u305f\u3044\u5834\u5408\u300d\u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-3566 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1.png\" alt=\"\" width=\"306\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1.png 306w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/04\/no_1-287x300.png 287w\" sizes=\"auto, (max-width: 306px) 100vw, 306px\" \/><\/p>\n<p><strong>\u958b\u767a\u74b0\u5883\uff1a<\/strong> Laravel 6.x<\/p>\n<h1>\u3084\u308a\u305f\u3044\u3053\u3068<\/h1>\n<p>\u5404\u30e2\u30c7\u30eb\u306b\u72ec\u81ea\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3057\u3066\u3082\u3044\u3044\u306e\u3067\u3059\u304c\u3001\u305d\u3046\u306a\u308b\u3068\u305d\u308c\u4ee5\u5916\u306e\u30e2\u30c7\u30eb\u304b\u3089\u306f\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u304c\u3067\u304d\u307e\u305b\u3093\u306e\u3067\u3001\u3088\u308a\u6c4e\u7528\u7684\u306b\u4f7f\u3048\u308b\u3088\u3046\u4eca\u56de\u306f<code>Collection::macro()<\/code>\u3092\u4f7f\u3063\u3066\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u3092\u62e1\u5f35\u3059\u308b\u5f62\u5f0f\u3067DB\u30c7\u30fc\u30bf\u3092\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3067\u304d\u308b\u3088\u3046\u306b\u3057\u307e\u3059\u3002<\/p>\n<p>\u203b <code>Collection::macro()<\/code>\u3092\u4f7f\u3046\u3068\u3001<code>Laravel<\/code>\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3\u306b\u72ec\u81ea\u306e\u30e1\u30bd\u30c3\u30c9\u3092\u8ffd\u52a0\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u306a\u304a\u3001<code>Collection::macro()<\/code>\u306f<code>app\/Providers\/AppServiceProvider.php<\/code>\u306e<code>boot()<\/code>\u5185\u306b\u8a18\u8ff0\u3059\u308b\u3068\u3069\u3053\u304b\u3089\u3067\u3082\u547c\u3073\u51fa\u3059\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u3067\u306f\u3001\u5b9f\u969b\u306b\u3084\u3063\u3066\u3044\u304d\u307e\u3057\u3087\u3046\uff01<\/p>\n<h1>\u6e96\u5099\u3092\u3059\u308b<\/h1>\n<p>\u4eca\u56de<code>CSV<\/code>\u3068<code>Excel<\/code>\u30d5\u30a1\u30a4\u30eb\u306f\u3001\u4ee5\u4e0b\uff12\u3064\u306e\u30d1\u30c3\u30b1\u30fc\u30b8\u3067\u53d6\u308a\u6271\u3044\u307e\u3059\u3002<br \/>\n\u4ee5\u4e0b\u306e\u30b3\u30de\u30f3\u30c9\u3067\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3057\u3066\u304a\u3044\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre><strong>composer require sukohi\/fluent-csv:3.*<\/strong><\/pre>\n<pre><strong>composer require phpoffice\/phpspreadsheet<\/strong><\/pre>\n<p>\u203b \u306a\u304a\u3001<code>PhpSpreadsheet<\/code>\u3067\u3082<code>CSV<\/code>\u3067\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u304c\u53ef\u80fd\u3067\u3059\u304c<a href=\"https:\/\/blog.capilano-fw.com\/?p=67\" target=\"_blank\" rel=\"noopener noreferrer\">Shift_JIS\u306b\u5bfe\u5fdc\u3057\u3066\u3044\u306a\u3044<\/a>\u306e\u3067\u3042\u3048\u3066<code>fluent-csv<\/code>\u3092\u4f7f\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<h1>CSV\u3067\u30a8\u30af\u30b9\u30dd\u30fc\u30c8<\/h1>\n<h2>\u5b9f\u88c5\u30b3\u30fc\u30c9<\/h2>\n<p>\u3067\u306f\u3001\u307e\u305a\u306f<code>CSV<\/code>\u306e\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3067\u3059\u3002<br \/>\n<code>app\/Providers\/AppServiceProvider.php<\/code>\u306e<code>boot()<\/code>\u306b\u6b21\u306e\u3088\u3046\u306b\u8ffd\u52a0\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Providers;\r\n\r\n<strong>use Illuminate\\Database\\Eloquent\\Collection;<\/strong>\r\nuse Illuminate\\Support\\ServiceProvider;\r\n\r\nclass AppServiceProvider extends ServiceProvider\r\n{\r\n    \/\/ \u7701\u7565\r\n\r\n    public function boot()\r\n    {\r\n        <strong>Collection::macro('exportCsv', function($filename = '', $filters = [], $encoding = 'UTF-8') {<\/strong>\r\n\r\n<strong>            if(empty($filename)) {<\/strong>\r\n\r\n<strong>                $filename = $this-&gt;first()-&gt;getTable() .'_'. date('Ymd_His') .'.csv';<\/strong>\r\n\r\n<strong>            }<\/strong>\r\n\r\n<strong>            $fluent = \\FluentCsv::setEncoding($encoding);<\/strong>\r\n\r\n<strong>            $this-&gt;each(function($item) use($fluent, $filters) {<\/strong>\r\n\r\n<strong>                $row_data = [];<\/strong>\r\n\r\n<strong>                if(empty($filters)) {<\/strong>\r\n\r\n<strong>                    $row_data = array_values($item-&gt;toArray());<\/strong>\r\n\r\n<strong>                } else {<\/strong>\r\n\r\n<strong>                    foreach($filters as $filter) {<\/strong>\r\n\r\n<strong>                        if(is_callable($filter)) {<\/strong>\r\n\r\n<strong>                            $row_data[] = $filter($item);<\/strong>\r\n\r\n<strong>                        }<\/strong>\r\n\r\n<strong>                    }<\/strong>\r\n\r\n<strong>                }<\/strong>\r\n\r\n<strong>                $fluent-&gt;addData($row_data);<\/strong>\r\n\r\n<strong>            });<\/strong>\r\n\r\n<strong>            return $fluent-&gt;download($filename);<\/strong>\r\n\r\n<strong>        });<\/strong>\r\n    }\r\n}<\/pre>\n<p>\u3053\u306e\u4e2d\u3067\u306f\u3001\u57fa\u672c\u7684\u306b<code>fluent-csv<\/code>\u306b\u30c7\u30fc\u30bf\u3092\u8ffd\u52a0\u3057\u3066<code>return<\/code>\u3057\u3066\u3044\u308b\u3060\u3051\u3067\u3059\u304c\u3001\u91cd\u8981\u306a\u306e\u304c<code>filters<\/code>\u3067\u3059\u3002\u3053\u308c\u306f\u3001\u4f8b\u3048\u3070\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u30c7\u30fc\u30bf\u3092\u52a0\u5de5\u3057\u305f\u3044\u5834\u5408\u306b\u4f7f\u3046\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<ul>\n<li>\u540d\u524d\u306b\u300c\u69d8\u300d\u3092\u3064\u3051\u305f\u3044<\/li>\n<li>\u897f\u66a6\u3092\u548c\u66a6\u306b\u5909\u63db\u3057\u305f\u3044<\/li>\n<li>\u90f5\u4fbf\u756a\u53f7\u3001\u90fd\u9053\u5e9c\u770c\u3001\u5e02\u533a\u753a\u6751\u306a\u3069\u3092\u7d50\u5408\u3057\u3066\u4f4f\u6240\u3092\u3064\u304f\u308b<\/li>\n<\/ul>\n<h2 id=\"csv_usage\">\u4f7f\u3044\u65b9<\/h2>\n<h3>\u30b7\u30f3\u30d7\u30eb\u306a\u4f7f\u3044\u65b9<\/h3>\n<p>\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u5185\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3057\u3066\u4f7f\u3044\u307e\u3059\u3002\uff08\u3082\u3061\u308d\u3093<code>User<\/code>\u30e2\u30c7\u30eb\u3060\u3051\u3067\u306a\u304f\u3001\u4ed6\u306e\u30e2\u30c7\u30eb\u3067\u3082\u540c\u3058\u3088\u3046\u306b\u4f7f\u3048\u307e\u3059\uff09<\/p>\n<pre><strong>$users = \\App\\User::get();<\/strong>\r\n<strong>return $users-&gt;exportCsv();<\/strong><\/pre>\n<p>\u203b \u3053\u306e\u5834\u5408\u3001\u30d5\u30a1\u30a4\u30eb\u540d\u306f\u300c\u30c6\u30fc\u30d6\u30eb\u540d + \u65e5\u6642.csv\u300d\u3068\u306a\u308a\u307e\u3059\u3002<\/p>\n<h3>\u53d6\u5f97\u30c7\u30fc\u30bf\u3092\u6307\u5b9a\u3059\u308b<\/h3>\n<p>\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u3059\u308b\u3053\u3068\u3067\u3001\u53d6\u5f97\u3059\u308b\u5185\u5bb9\u3092\u7d5e\u308a\u8fbc\u3080\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>$users = \\App\\User::<strong>select('id', 'email', 'name')<\/strong>-&gt;get();\r\nreturn $users-&gt;exportCsv();<\/pre>\n<p>\u5b9f\u969b\u306b\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b\u3068\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5082\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/11\/export_csv_1.png\" alt=\"\" width=\"248\" height=\"181\" \/><\/p>\n<h3>\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u6307\u5b9a\u3059\u308b<\/h3>\n<p>\u30d5\u30a1\u30a4\u30eb\u540d\u3092\u6307\u5b9a\u3059\u308b\u5834\u5408\u306f\u7b2c\uff11\u5f15\u6570\u3067\u6307\u5b9a\u3057\u307e\u3059\u3002<\/p>\n<pre>return $users-&gt;exportCsv(<strong>'test.csv'<\/strong>);<\/pre>\n<h3>\u30c7\u30fc\u30bf\u3092\u52a0\u5de5\u3059\u308b<\/h3>\n<p>\u7b2c\uff12\u5f15\u6570\u306b\u30b3\u30fc\u30eb\u30d0\u30c3\u30af\u95a2\u6570\u3092\u8ffd\u52a0\u3059\u308b\u3068\u30c7\u30fc\u30bf\u3092\u52a0\u5de5\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002\uff08<code>DataTable.js<\/code>\u306b\u4f3c\u305f\u611f\u3058\u3067\u3059\uff09<\/p>\n<pre>$users = \\App\\User::get();\r\n<strong>$filters = [<\/strong>\r\n<strong>    function($user) {<\/strong>\r\n\r\n<strong>        return $user-&gt;name .'\u69d8';<\/strong>\r\n\r\n<strong>    },<\/strong>\r\n<strong>    function($user) {<\/strong>\r\n\r\n<strong>        return $user-&gt;created_at-&gt;format('Y\u5e74m\u6708d\u65e5 H\u6642i\u5206s\u79d2');<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n<strong>];<\/strong>\r\nreturn $users-&gt;exportCsv('test.csv', <strong>$filters<\/strong>);<\/pre>\n<p>\u5b9f\u969b\u306b\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3057\u305f\u3082\u306e\u304c\u3053\u3061\u3089\u3067\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-5083\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/11\/export_csv_2.png\" alt=\"\" width=\"288\" height=\"184\" \/><\/p>\n<h3>\u6587\u5b57\u30b3\u30fc\u30c9\u3092\u6307\u5b9a\u3059\u308b<\/h3>\n<p>\u6587\u5b57\u30b3\u30fc\u30c9\u306f\u7b2c\uff13\u5f15\u6570\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>return $users-&gt;exportCsv('test.csv', [], <strong>'sjis-win'<\/strong>);<\/pre>\n<h3>\uff11\u884c\u76ee\u306b\u9805\u76ee\u540d\u3092\u5165\u308c\u305f\u3044\u5834\u5408<\/h3>\n<p>\u8a2a\u554f\u30e6\u30fc\u30b6\u30fc\u3055\u3093\u304b\u3089\u300c<strong>\uff11\u884c\u76ee\u306b\u4ee5\u4e0b\u306e\u3088\u3046\u306a\u9805\u76ee\u540d\u3092\u5165\u308c\u305f\u3044\u5834\u5408\u306f\u3069\u3046\u3059\u308c\u3070\u3044\u3044\u3067\u3057\u3087\u3046\u304b\uff1f<\/strong>\u300d\u3068\u3054\u8cea\u554f\u3092\u3044\u305f\u3060\u3044\u305f\u306e\u3067\u3001<code>Collection::macro()<\/code>\u3092\u62e1\u5f35\u3057\u3066\u307f\u307e\u3057\u305f\ud83d\ude0a\ud83d\udc4d<\/p>\n<ul>\n<li>ID<\/li>\n<li>\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9<\/li>\n<li>\u540d\u524d<\/li>\n<li>\u767b\u9332\u65e5\u6642<\/li>\n<\/ul>\n<p>\u307e\u305a\u306f\u30b3\u30fc\u30c9\u304b\u3089\u3067\u3059\u3002<\/p>\n<p><strong>app\/Providers\/AppServiceProvider.php<\/strong><\/p>\n<pre>\/\/ \ud83d\udc47 $headings \u3092\u8ffd\u52a0\u3057\u307e\u3057\u305f\r\nCollection::macro('exportCsv', function($filename = '', <strong>$headings = []<\/strong>, $filters = [], $encoding = 'UTF-8') {\r\n\r\n    if(empty($filename)) {\r\n\r\n        $filename = $this-&gt;first()-&gt;getTable() .'_'. date('Ymd_His') .'.csv';\r\n\r\n    }\r\n\r\n    $fluent = \\FluentCsv::setEncoding($encoding);\r\n\r\n    \/\/ \ud83d\udc47 \u3053\u3053\u3082\u8ffd\u52a0\u3057\u307e\u3057\u305f\r\n<strong>    if(!empty($headings)) {<\/strong>\r\n\r\n<strong>        $fluent-&gt;addData($headings);<\/strong>\r\n\r\n<strong>    }<\/strong>\r\n\r\n    $this-&gt;each(function($item) use($fluent, $filters) {\r\n\r\n        $row_data = [];\r\n\r\n        if(empty($filters)) {\r\n\r\n            $row_data = array_values($item-&gt;toArray());\r\n\r\n        } else {\r\n\r\n            foreach($filters as $filter) {\r\n\r\n                if(is_callable($filter)) {\r\n\r\n                    $row_data[] = $filter($item);\r\n\r\n                }\r\n\r\n            }\r\n\r\n        }\r\n\r\n        $fluent-&gt;addData($row_data);\r\n\r\n    });\r\n\r\n    return $fluent-&gt;download($filename);\r\n\r\n});<\/pre>\n<p>\u62e1\u5f35\u3068\u8a00\u3044\u307e\u3057\u305f\u304c\u3001\u5b9f\u969b\u306b\u306f\u5f15\u6570\u306b<code>$headings<\/code>\u3092\u8ffd\u52a0\u3057\u3001\u3082\u3057\u7a7a\u3067\u306f\u306a\u3044\u5834\u5408\u306b<code>addData()<\/code>\u3092\u5b9f\u884c\u3057\u3066\u3044\u308b\u3060\u3051\u3067\u3059\u3002<\/p>\n<p>\u305d\u306e\u305f\u3081\u3001\u4f7f\u3044\u65b9\u3068\u3057\u3066\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre>$users = User::select('id', 'email', 'name', 'created_at')-&gt;get();\r\n$filename = 'test.csv';\r\n<strong>$headings = [<\/strong>\r\n<strong>    'ID',<\/strong>\r\n<strong>    '\u30e1\u30fc\u30eb\u30a2\u30c9\u30ec\u30b9',<\/strong>\r\n<strong>    '\u540d\u524d',<\/strong>\r\n<strong>    '\u767b\u9332\u65e5\u6642'<\/strong>\r\n<strong>];<\/strong>\r\nreturn $users-&gt;exportCsv($filename, $headings);<\/pre>\n<p>\u5b9f\u969b\u306b\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b\u3068\u3053\u306e\u3088\u3046\u306b\u306a\u308a\u307e\u3059\u3002<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-8043\" style=\"border: 3px solid #000;\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/11\/export_csv_3.png\" alt=\"\" width=\"592\" height=\"285\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/11\/export_csv_3.png 592w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2019\/11\/export_csv_3-300x144.png 300w\" sizes=\"auto, (max-width: 592px) 100vw, 592px\" \/><\/p>\n<h1>Excel\u3067\u30a8\u30af\u30b9\u30dd\u30fc\u30c8<\/h1>\n<p>\u7d9a\u3044\u3066<code>Excel<\/code>\u306e\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3067\u3059\u3002<\/p>\n<h2>\u5b9f\u88c5\u30b3\u30fc\u30c9<\/h2>\n<p>\u3053\u3061\u3089\u3082<code>app\/Providers\/AppServiceProvider.php<\/code>\u306e<code>boot()<\/code>\u306e\u4e2d\u306b\u4ee5\u4e0b\u306e\u30b3\u30fc\u30c9\u3092\u8ffd\u52a0\u3057\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Providers;\r\n\r\n<strong>use Illuminate\\Database\\Eloquent\\Collection;<\/strong>\r\n<strong>use PhpOffice\\PhpSpreadsheet\\Spreadsheet;<\/strong>\r\n<strong>use PhpOffice\\PhpSpreadsheet\\Writer\\Xlsx;<\/strong>\r\n<strong>use Symfony\\Component\\HttpFoundation\\StreamedResponse;<\/strong>\r\nuse Illuminate\\Support\\ServiceProvider;\r\n\r\nclass AppServiceProvider extends ServiceProvider\r\n{\r\n    \/\/ \u7701\u7565\r\n\r\n    public function boot()\r\n    {\r\n        <strong>Collection::macro('exportExcel', function($filename = '', $filters = []) {<\/strong>\r\n\r\n<strong>            if(empty($filename)) {<\/strong>\r\n\r\n<strong>                $filename = $this-&gt;first()-&gt;getTable() .'_'. date('Ymd_His') .'.xlsx';<\/strong>\r\n\r\n<strong>            }<\/strong>\r\n\r\n<strong>            $spreadsheet = new Spreadsheet();<\/strong>\r\n<strong>            $sheet = $spreadsheet-&gt;getActiveSheet();<\/strong>\r\n<strong>            $excel_data = [];<\/strong>\r\n\r\n\r\n<strong>            $this-&gt;each(function($item) use(&amp;$excel_data, $filters) {<\/strong>\r\n\r\n<strong>                $row_data = [];<\/strong>\r\n\r\n<strong>                if(empty($filters)) {<\/strong>\r\n\r\n<strong>                    $row_data = array_values($item-&gt;toArray());<\/strong>\r\n\r\n<strong>                } else {<\/strong>\r\n\r\n<strong>                    foreach($filters as $filter) {<\/strong>\r\n\r\n<strong>                        if(is_callable($filter)) {<\/strong>\r\n\r\n<strong>                            $row_data[] = $filter($item);<\/strong>\r\n\r\n<strong>                        }<\/strong>\r\n\r\n<strong>                    }<\/strong>\r\n\r\n<strong>                }<\/strong>\r\n\r\n<strong>                $excel_data[] = $row_data;<\/strong>\r\n\r\n<strong>            });<\/strong>\r\n<strong>            $sheet-&gt;fromArray($excel_data, null, 'A1');<\/strong>\r\n\r\n<strong>            $callback = function() use($spreadsheet) {<\/strong>\r\n\r\n<strong>                $writer = new Xlsx($spreadsheet);<\/strong>\r\n<strong>                $writer-&gt;save('php:\/\/output');<\/strong>\r\n\r\n<strong>            };<\/strong>\r\n<strong>            $status = 200;<\/strong>\r\n<strong>            $headers = [<\/strong>\r\n<strong>                'Content-Type' =&gt; 'application\/vnd.ms-excel',<\/strong>\r\n<strong>                'Content-Disposition' =&gt; 'attachment;filename=\"'. $filename .'\"',<\/strong>\r\n<strong>                'Cache-Control' =&gt; 'max-age=0',<\/strong>\r\n<strong>            ];<\/strong>\r\n<strong>            return new StreamedResponse($callback, $status, $headers);<\/strong>\r\n\r\n<strong>        });<\/strong>\r\n    }\r\n}<\/pre>\n<h2>\u4f7f\u3044\u65b9<\/h2>\n<p>\u4f7f\u3044\u65b9\u306f\u57fa\u672c\u7684\u306b<a href=\"#csv_usage\">CSV\u306e\u30a8\u30af\u30b9\u30dd\u30fc\u30c8<\/a>\u3068\u540c\u3058\u3067\u3059\u304c\u3001\u6587\u5b57\u30b3\u30fc\u30c9\u306e\u6307\u5b9a\u306f\u3067\u304d\u307e\u305b\u3093\u3002\uff08UTF-8\u3067\u56fa\u5b9a\u3067\u3059\uff09<\/p>\n<h1>\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u3064\u304f\u308a\u307e\u3057\u305f<\/h1>\n<p>\u4eca\u56de\u306e\u6a5f\u80fd\u306f\u3001\u518d\u5229\u7528\u3059\u308b\u53ef\u80fd\u6027\u304c\u9ad8\u3044\u3068\u611f\u3058\u307e\u3057\u305f\u306e\u3067\u30d1\u30c3\u30b1\u30fc\u30b8\u5316\u3057\u3066<code>GitHub<\/code>\u3067\u516c\u958b\u3059\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p><a href=\"https:\/\/github.com\/SUKOHI\/EloquentExport\">https:\/\/github.com\/SUKOHI\/EloquentExport<\/a><\/p>\n<p>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u306f\u30b3\u30de\u30f3\u30c9\u4e00\u767a\u3067\u3059\u3002<\/p>\n<pre><strong>composer require sukohi\/eloquent-export:1.*<\/strong><\/pre>\n<p>\u4f7f\u3044\u65b9\u306f\u57fa\u672c\u7684\u306b\u540c\u3058\u3067\u3059\u304c\u3001\u30e1\u30bd\u30c3\u30c9\u540d\u304c<code>export()<\/code>\u306b\u5909\u66f4\u306b\u306a\u3063\u3066\u3044\u3066\u3001\u30d5\u30a1\u30a4\u30eb\u540d\u3060\u3051\u304c\u5fc5\u9808\u306b\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p><strong>\u30a8\u30af\u30bb\u30eb\u30d5\u30a1\u30a4\u30eb\u306e\u5834\u5408<\/strong><\/p>\n<pre><strong>return $users-&gt;export('test.xlsx');\r\n\r\n\/\/ or \r\n\r\nreturn $users-&gt;export('test.xlsx', $filters);\r\n<\/strong><\/pre>\n<p><strong>CSV\u30d5\u30a1\u30a4\u30eb\u306e\u5834\u5408<\/strong><\/p>\n<pre><strong>return $users-&gt;export('test.csv');\r\n\r\n\/\/ or \r\n\r\nreturn $users-&gt;export('test.csv', $filters);\r\n\r\n\/\/ or\r\n\r\nreturn $users-&gt;export('test.csv', $filters, 'sjis-win');\r\n<\/strong><\/pre>\n<h1>\u304a\u308f\u308a\u306b<\/h1>\n<p>\u3068\u3044\u3046\u3053\u3068\u3067\u4eca\u56de\u306f\u30c7\u30fc\u30bf\u30d9\u30fc\u30b9\u306e\u5185\u5bb9\u3092<code>CSV<\/code>\u3068<code>Excel<\/code>\u306e\u30d5\u30a1\u30a4\u30eb\u3067\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b\u6a5f\u80fd\u3092\u4f5c\u3063\u3066\u307f\u307e\u3057\u305f\u3002<\/p>\n<p>\u3061\u306a\u307f\u306b\u3067\u3059\u304c\u3001\u4eca\u56de\u4f7f\u3063\u305f<a href=\"https:\/\/github.com\/PHPOffice\/PhpSpreadsheet\" target=\"_blank\" rel=\"noopener noreferrer\">PhpSpreadsheet<\/a>\u306e\u30e9\u30a4\u30bb\u30f3\u30b9\u304c\u6570\u65e5\u524d\u306b<code>LGPL<\/code>\u304b\u3089<code>MIT<\/code>\u306b\u5909\u66f4\u306b\u306a\u3063\u305f\u3088\u3046\u3067\u3059\u3002\uff08\u5076\u7136\u3067\u3059\u304c\u30bf\u30a4\u30df\u30f3\u30b0\u304c\u3061\u3087\u3046\u3069\u3067\u3057\u305f\ud83d\ude0a\u2728\uff09<\/p>\n<p>\u3082\u3061\u308d\u3093<code>LGPL<\/code>\u3067\u3082\u5185\u5bb9\u306b\u3088\u3063\u3066\u306f\u30af\u30e9\u30a4\u30a2\u30f3\u30c8\u69d8\u306e\u305f\u3081\u306b\u4f7f\u3046\u3053\u3068\u306f\u3067\u304d\u307e\u3059\u304c\u3001\u958b\u767a\u8005\u3068\u3057\u3066\u306f<code>MIT<\/code>\u3068\u660e\u8a00\u3055\u308c\u3066\u3044\u308b\u65b9\u304c\u4f7f\u3044\u3084\u3059\u3044\u5370\u8c61\u306f\u3042\u308a\u307e\u3059\u306d\u3002<\/p>\n<p>\u307e\u305f\u3001\u79c1\u304c\u516c\u958b\u3057\u3066\u3044\u308b<code>Fluent-CSV<\/code>\u306e\u65b9\u3082<code>MIT<\/code>\u30e9\u30a4\u30bb\u30f3\u30b9\u3067\u3059\u306e\u3067\u3001\u30d5\u30ec\u30ad\u30b7\u30d6\u30eb\u306b\u5229\u7528\u3067\u304d\u308b\u304b\u3068\u601d\u3044\u307e\u3059\u3002<\/p>\n<p>\u305c\u3072\u4eca\u56de\u306e\u5185\u5bb9\u3092\u6d3b\u7528\u3057\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u306d\u3002<\/p>\n<p>\u3067\u306f\u3067\u306f\u301c\uff01<\/p>\n<p><img loading=\"lazy\" decoding=\"async\" class=\"alignnone size-full wp-image-2594 aligncenter\" src=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/11\/23.png\" alt=\"\" width=\"182\" height=\"320\" srcset=\"https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/11\/23.png 182w, https:\/\/blog.capilano-fw.com\/wp-content\/uploads\/2018\/11\/23-171x300.png 171w\" sizes=\"auto, (max-width: 182px) 100vw, 182px\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3055\u3066\u3001\u3053\u306e\u9593DB\u30c7\u30fc\u30bf\u3092CSV\u3067\u30a4\u30f3\u30dd\u30fc\u30c8\u3059\u308b\u6a5f\u80fd\u3092\u3064\u304f\u308b\u3068\u3044\u3046\u8a18\u4e8b\u3092\u516c\u958b\u3057\u307e\u3057\u305f\u304c\u3001\u5b9f\u306f\u3044\u3064\u3082\u3088\u308a\u5c11\u3057\u3060\u3051\u30a2\u30af\u30bb\u30b9\u304c\u591a\u304b\u3063\u305f\u3067\u3059\ud83d\ude0a \u305d\u3057\u3066\u3001\u305b\u3063\u304b\u304f\u30a4\u30f3\u30dd &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=5081\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u3010Laravel\u3011DB\u30c7\u30fc\u30bf\u3092CSV\u30fbExcel\u3067\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u3059\u308b&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":5087,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[9,54],"class_list":["post-5081","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-csv","tag-excel"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5081","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=5081"}],"version-history":[{"count":6,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5081\/revisions"}],"predecessor-version":[{"id":8044,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/5081\/revisions\/8044"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/5087"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=5081"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=5081"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=5081"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}