{"id":378,"date":"2018-03-23T19:11:16","date_gmt":"2018-03-23T10:11:16","guid":{"rendered":"https:\/\/blog.capilano-fw.com\/?p=378"},"modified":"2018-03-23T19:11:16","modified_gmt":"2018-03-23T10:11:16","slug":"%e6%9c%80%e6%96%b0%ef%bc%81laravel-excel-3-0%e3%81%ae%e6%96%b0%e6%a9%9f%e8%83%bd%e3%82%92%e8%a9%a6%e3%81%97%e3%81%a6%e3%81%bf%e3%81%9f","status":"publish","type":"post","link":"https:\/\/blog.capilano-fw.com\/?p=378","title":{"rendered":"\u6700\u65b0\uff01Laravel Excel 3.0\u306e\u65b0\u6a5f\u80fd\u3092\u8a66\u3057\u3066\u307f\u305f"},"content":{"rendered":"<p>\u3055\u3066\u3001\u3044\u3064\u3082\u958b\u767a\u3067\u304a\u4e16\u8a71\u306b\u306a\u3063\u3066\u308bLaravel\u3067\u3059\u304c\u3001\u3053\u306eLaravel\u958b\u767a\u3067\u30a8\u30af\u30bb\u30eb\u3092\u6271\u3046\u3068\u304d\u306e\u5b9a\u756a\u30d1\u30c3\u30b1\u30fc\u30b8\u300cLaravel Excel\u300d\u304c\u30e1\u30b8\u30e3\u30fc\u30a2\u30c3\u30d7\u30c7\u30fc\u30c8\u3055\u308c\u3001\u30d0\u30fc\u30b8\u30e7\u30f3\uff13\uff0e\uff10\u306b\u306a\u308a\u307e\u3057\u305f\u3002<\/p>\n<p>\u3053\u308c\u304b\u3089\u306e\u958b\u767a\u3067\u304d\u3063\u3068\u5fc5\u8981\u3068\u306a\u3063\u3066\u304f\u308b\u306f\u305a\u306a\u306e\u3067\u3001\u4eca\u56de\u306f\u3053\u306e\u6700\u65b0\u7248Laravel Excel\u306e\u65b0\u6a5f\u80fd\u3092\u3092\u4f53\u9a13\u3057\u3066\u307f\u308b\u3053\u3068\u306b\u3057\u307e\u3057\u305f\u3002<\/p>\n<p><strong>\u3010\u5b9f\u884c\u74b0\u5883\u3011<\/strong><br \/>\nLaravel 5.6<br \/>\nPHP 7.2<\/p>\n<p><strong>\u3010\u524d\u63d0\u3011<\/strong><br \/>\nModel\u3068\u3057\u3066\u3001Item.php\u304c\u4f5c\u6210\u6e08\u307f\u3067\u3001DB\u30c6\u30fc\u30d6\u30ebitems\u306b\u30c7\u30fc\u30bf\u304c\u3042\u308b\u3002<\/p>\n<h1>\u3069\u3053\u304c\u5909\u66f4\u306b\u306a\u3063\u305f\uff1f<\/h1>\n<p>\u4eca\u56de\u306e\u30a2\u30c3\u30d7\u30c7\u30fc\u30c8\u3067\u306f\u3001\u4e3b\u306b\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u90e8\u5206\u304c\u65b0\u3057\u304f\u306a\u3063\u3066\u3044\u307e\u3059\u3002<\/p>\n<p>\u524d\u306e\u30d0\u30fc\u30b8\u30e7\u30f3\u306e\u57fa\u672c\u7684\u306a\u4f7f\u3044\u65b9\u3067\u306f\u3001\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u95a2\u6570\u5185\u306b\u3044\u308d\u3044\u308d\u3068\u30b3\u30fc\u30c9\u3092\u66f8\u304f\u30b9\u30bf\u30a4\u30eb\u3067\u3057\u305f\u3002<\/p>\n<pre><strong>Excel::create('Filename', function($excel) {<\/strong>\r\n\r\n<strong>    \/\/ \u3053\u3053\u306b\u4f55\u304b<\/strong>\r\n\r\n<strong>});<\/strong><\/pre>\n<p>\u305d\u308c\u304c\u3001\uff13\uff0e\uff10\u304b\u3089\u306f\u72ec\u81ea\u30af\u30e9\u30b9\u3092\u5b9a\u7fa9\u3059\u308b\u5f62\u306b\u306a\u3063\u305f\u3088\u3046\u3067\u3059\u3002<br \/>\n\u3067\u306f\u3001\u5b9f\u969b\u306b\u898b\u3066\u3044\u304d\u307e\u3057\u3087\u3046\uff01<\/p>\n<h1>\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb<\/h1>\n<p>\u307e\u305a\u306f\u3001\u30a4\u30f3\u30b9\u30c8\u30fc\u30eb\u3067\u3059\u3002<br \/>\n\u3068\u3044\u3063\u3066\u3082Auto-Discovery\uff08ServiceProvider\u3092\u81ea\u52d5\u767b\u9332\u3057\u3066\u304f\u308c\u308b\u6a5f\u80fd\uff09\u304c\u3042\u308b\u3093\u3067\u3001\u672c\u5f53\u306b<strong>composer<\/strong>\u4e00\u767a\u3067\u6e96\u5099\u5b8c\u4e86\u3067\u3059\u3002<\/p>\n<pre><strong>composer require maatwebsite\/excel<\/strong><\/pre>\n<h1>\u30a8\u30af\u30b9\u30dd\u30fc\u30c8<\/h1>\n<p>\u307e\u305a\u306f\u3001\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u5c02\u7528\u306e\u30af\u30e9\u30b9\u3092<strong>\uff08\u30eb\u30fc\u30c8\u30d5\u30a9\u30eb\u30c0\uff09\/app\/Exports<\/strong>\u306b\u4f5c\u6210\u3057\u307e\u3059\u3002<\/p>\n<p>\u203b\u3053\u306e\u4f8b\u306f<strong>FromCollection<\/strong>\u3092\u4f7f\u3063\u3066\u307e\u3059\u3002collection()\u306e\u4e2d\u3067\u30c7\u30fc\u30bf\u3068\u306a\u308b<a href=\"https:\/\/laravel.com\/docs\/5.6\/collections\" target=\"_blank\" rel=\"noopener\">Laravel\u306e\u30b3\u30ec\u30af\u30b7\u30e7\u30f3<\/a>\u3092\u8fd4\u3057\u3066\u3042\u3052\u307e\u3057\u3087\u3046\u3002<\/p>\n<p><strong>\uff08app\/Exports\/ItemsExport.php\uff09<\/strong><\/p>\n<pre><strong>&lt;?php<\/strong>\r\n\r\n<strong>namespace App\\Exports;<\/strong>\r\n\r\n<strong>use Maatwebsite\\Excel\\Concerns\\FromCollection;<\/strong>\r\n\r\n<strong>class ItemsExport implements FromCollection<\/strong>\r\n<strong>{<\/strong>\r\n<strong>    public function collection()<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        return \\App\\Item::all();<\/strong>\r\n<strong>    }<\/strong>\r\n<strong>}<\/strong><\/pre>\n<p>\u305d\u3057\u3066\u3001\u30b3\u30f3\u30c8\u30ed\u30fc\u30e9\u30fc\u5185\u3067\u4ee5\u4e0b\u306e\u3088\u3046\u306b\u8a18\u8ff0\u3059\u308b\u3060\u3051\u3067OK\u3067\u3059\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Http\\Controllers;\r\n\r\n<strong>use App\\Exports\\ItemsExport;<\/strong>\r\n\r\nclass HomeController extends Controller\r\n{\r\n    public function excel_download() {\r\n\r\n        <strong>return \\Excel::download(new ItemsExport, 'items.xlsx');<\/strong>\r\n\r\n    }<\/pre>\n<p>\u3059\u3054\u304f\u30b7\u30f3\u30d7\u30eb\u3067\u3059\u306d\u266a<\/p>\n<p>\u305d\u3057\u3066\u3001\u3082\u3063\u3068\u77ed\u3044\u30b3\u30fc\u30c9\u3067\u3059\u307e\u305b\u305f\u3044\u4eba\u306b\u306f\u3001<strong>Exportable\u30c8\u30ec\u30a4\u30c8<\/strong>\u3082\u7528\u610f\u3055\u308c\u3066\u3044\u307e\u3059\u3002\u3084\u308a\u65b9\u306f\u30c8\u30ec\u30a4\u30c8\u3092\u8aad\u307f\u8fbc\u3080\u3060\u3051\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Exports;\r\n\r\n<strong>use Maatwebsite\\Excel\\Concerns\\Exportable;<\/strong>\r\nuse Maatwebsite\\Excel\\Concerns\\FromCollection;\r\n\r\nclass ItemsExport implements FromCollection\r\n{\r\n    <strong>use Exportable;<\/strong>\r\n\r\n    public function collection()\r\n    {\r\n        return \\App\\Item::all();\r\n    }\r\n}<\/pre>\n<p>\u3059\u308b\u3068\u3001\u6b21\u306e\u3088\u3046\u306a\u77ed\u3044\u30b3\u30fc\u30c9\u3067Excel\u30d5\u30a1\u30a4\u30eb\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Http\\Controllers;\r\n\r\n<strong>use App\\Exports\\ItemsExport;<\/strong>\r\n\r\nclass HomeController extends Controller\r\n{\r\n    public function excel_download() {\r\n\r\n        <strong>return (new ItemsExport)-&gt;download('items.xlsx');<\/strong>\r\n\r\n    }<\/pre>\n<h1>\u6761\u4ef6\u4ed8\u304d\u306e\u5834\u5408\u306f\u3069\u3046\u3059\u308b\uff1f<\/h1>\n<p>\u30b3\u30f3\u30b9\u30c8\u30e9\u30af\u30bf\u306b\u5f15\u6570\u3092\u5165\u308c\u3066\u3084\u308b\u3053\u3068\u3067\u3001<strong>where\u53e5<\/strong>\u306a\u3069\u3092\u53ef\u5909\u306b\u3059\u308b\u3053\u3068\u304c\u3067\u304d\u307e\u3059\u3002<\/p>\n<p>\u203b\u3053\u306e\u4f8b\u306fFromQuery\u3092\u4f7f\u3063\u3066\u307e\u3059\u3002query()\u3067DB\u306equery\u3092\u8fd4\u3057\u3066\u3042\u3052\u307e\u3057\u3087\u3046\u3002<\/p>\n<pre>&lt;?php\r\n\r\nnamespace App\\Exports;\r\n\r\nuse Maatwebsite\\Excel\\Concerns\\Exportable;\r\nuse Maatwebsite\\Excel\\Concerns\\FromQuery;\r\n\r\nclass ItemsExport implements FromQuery\r\n{\r\n    use Exportable;\r\n\r\n<strong>    public function __construct(string $name)<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        $this-&gt;name = $name; \/\/ Where\u53e5\u306e\u30c7\u30fc\u30bf<\/strong>\r\n<strong>    }<\/strong>\r\n\r\n    <strong>public function query()<\/strong>\r\n<strong>    {<\/strong>\r\n<strong>        return \\App\\Item::where('name', $this-&gt;name);<\/strong>\r\n<strong>    }<\/strong>\r\n}<\/pre>\n<p>\u305d\u3057\u3066\u3001\u4f7f\u3044\u65b9\u306f\u3053\u3046\u306a\u308a\u307e\u3059\u3002<\/p>\n<pre><strong>return (new ItemsExport('\u540d\u524d'))-&gt;download('items.xlsx');<\/strong><\/pre>\n<h1>CSV\u30d5\u30a1\u30a4\u30eb\u306e\u30a8\u30af\u30b9\u30dd\u30fc\u30c8\u306f\uff1f<\/h1>\n<p>\u8a66\u3057\u3066\u307f\u305f\u3068\u3053\u308d\u3001\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3057\u305f\u3044\u30d5\u30a1\u30a4\u30eb\u540d\u306e\u62e1\u5f35\u5b50\u3092<strong>.csv<\/strong>\u306b\u3059\u308b\u3060\u3051\u3002\u306a\u3093\u3068\u3082\u4fbf\u5229\u3067\u3059\u306d\u3002<\/p>\n<pre>return (new ItemsExport)-&gt;download('<strong>items.csv<\/strong>');<\/pre>\n<p>\u3057\u304b\u3057\u3001\u6b8b\u5ff5\u306a\u304c\u3089\u3001\u6211\u3005\u65e5\u672c\u4eba\u306e\u61f8\u6848\u4e8b\u9805\u3067\u3042\u308b<strong>Shift_JIS\u306b\u306f\u5bfe\u5fdc\u3057\u3066\u306a\u3044<\/strong>\u306e\u3067\u3001\u6587\u5b57\u5217\u3092mb_convert_encoding\u3067\u5909\u63db\u3057\u3066\u3082\u7a7a\u767d\u306b\u306a\u3063\u3066\u3057\u307e\u3044\u307e\u3059\u3002\uff08\u2190\u3069\u3046\u3084\u3089\u89aa\u30d1\u30c3\u30b1\u30fc\u30b8\u306ephpoffice\/phpspreadsheet\u304c\u5bfe\u5fdc\u3057\u3066\u306a\u3044\u3088\u3046\u3067\u3059\uff09<\/p>\n<p>\u3082\u3057Shift_JIS\u306eCSV\u3092\u30c0\u30a6\u30f3\u30ed\u30fc\u30c9\u3055\u305b\u305f\u3044\u5834\u5408\u306f\u3001\u79c1\u304c\u516c\u958b\u3057\u3066\u3044\u308b\u30d1\u30c3\u30b1\u30fc\u30b8\u300c<a href=\"https:\/\/github.com\/SUKOHI\/FluentCsv\" target=\"_blank\" rel=\"noopener\">FluentCsv<\/a>\u300d\u3092\u4f7f\u3063\u3066\u307f\u3066\u304f\u3060\u3055\u3044\u3002<\/p>\n<p>\u53c2\u8003\uff1a\u00a0<a href=\"http:\/\/Laravel-Excel\">Laravel-Excel \u306e CSV\u6587\u5b57\u5316\u3051\u306b\u7d76\u671b\u3057\u305f\u306e\u3067\u30d1\u30c3\u30b1\u30fc\u30b8\u3092\u4f5c\u3063\u305f\uff01<\/a><\/p>\n<p>\u305d\u308c\u306b\u3057\u3066\u3082\u3001\u65e5\u672c\u306f\u3044\u3064\u307e\u3067Shift_JIS\u306b\u7121\u99c4\u306a\u6642\u9593\u3092\u5272\u304b\u306a\u304d\u3083\u3044\u3051\u306a\u3044\u3093\u3060\u308d\u3046\u304b\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\u30fb\uff08\u6c57\uff09<br \/>\n\u3082\u3057\u304b\u3059\u308b\u3068\u30df\u30c9\u30eb\u30a6\u30a7\u30a2\u3068\u304b\u3092\u3044\u3058\u308c\u3070\u5b9f\u73fe\u53ef\u80fd\u304b\u306a\uff1f\uff1f\uff1f<\/p>\n<p>\u3068\u3044\u3063\u305f\u3068\u3053\u308d\u3067\u3001\u4eca\u56de\u306f\u4ee5\u4e0a\u3067\u3059\u3002<br \/>\n\u3067\u306f\u3067\u306f\u301c\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"<p>\u3055\u3066\u3001\u3044\u3064\u3082\u958b\u767a\u3067\u304a\u4e16\u8a71\u306b\u306a\u3063\u3066\u308bLaravel\u3067\u3059\u304c\u3001\u3053\u306eLaravel\u958b\u767a\u3067\u30a8\u30af\u30bb\u30eb\u3092\u6271\u3046\u3068\u304d\u306e\u5b9a\u756a\u30d1\u30c3\u30b1\u30fc\u30b8\u300cLaravel Excel\u300d\u304c\u30e1\u30b8\u30e3\u30fc\u30a2\u30c3\u30d7 &hellip; <\/p>\n<p class=\"link-more\"><a href=\"https:\/\/blog.capilano-fw.com\/?p=378\" class=\"more-link\"><span class=\"screen-reader-text\">&#8220;\u6700\u65b0\uff01Laravel Excel 3.0\u306e\u65b0\u6a5f\u80fd\u3092\u8a66\u3057\u3066\u307f\u305f&#8221; \u306e<\/span>\u7d9a\u304d\u3092\u8aad\u3080<\/a><\/p>\n","protected":false},"author":1,"featured_media":379,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[3],"tags":[54],"class_list":["post-378","post","type-post","status-publish","format-standard","has-post-thumbnail","hentry","category-laravel","tag-excel"],"_links":{"self":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/378","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=378"}],"version-history":[{"count":1,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/378\/revisions"}],"predecessor-version":[{"id":380,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/posts\/378\/revisions\/380"}],"wp:featuredmedia":[{"embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=\/wp\/v2\/media\/379"}],"wp:attachment":[{"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=378"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=378"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.capilano-fw.com\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=378"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}