|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace Finder\Console\Commands\Prepare; |
|
4
|
|
|
|
|
5
|
|
|
use Illuminate\Console\Command; |
|
6
|
|
|
use Telefonica\Models\Actors\Person; |
|
7
|
|
|
use Finder\Pipelines\Track\PersonTrack; |
|
8
|
|
|
use Illuminate\Support\Facades\Storage; |
|
9
|
|
|
use Stalker\Models\Imagen; |
|
10
|
|
|
|
|
11
|
|
|
class Export extends Command |
|
12
|
|
|
{ |
|
13
|
|
|
/** |
|
14
|
|
|
* The name and signature of the console command. |
|
15
|
|
|
* |
|
16
|
|
|
* @var string |
|
17
|
|
|
*/ |
|
18
|
|
|
protected $signature = 'siexport:finder:export'; |
|
19
|
|
|
|
|
20
|
|
|
/** |
|
21
|
|
|
* The console command description. |
|
22
|
|
|
* |
|
23
|
|
|
* @var string |
|
24
|
|
|
*/ |
|
25
|
|
|
protected $description = 'Exportar a porra toda !'; |
|
26
|
|
|
|
|
27
|
|
|
/** |
|
28
|
|
|
* Create a new command instance. |
|
29
|
|
|
* |
|
30
|
|
|
* @return void |
|
|
|
|
|
|
31
|
|
|
*/ |
|
32
|
|
|
public function __construct() |
|
33
|
|
|
{ |
|
34
|
|
|
parent::__construct(); |
|
35
|
|
|
} |
|
36
|
|
|
|
|
37
|
|
|
/** |
|
38
|
|
|
* Execute the console command. |
|
39
|
|
|
* |
|
40
|
|
|
* @return mixed |
|
41
|
|
|
*/ |
|
42
|
|
|
public function handle() |
|
43
|
|
|
{ |
|
44
|
|
|
$folder = 'import'; |
|
45
|
|
|
$this->export($folder); |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* Tirardaqui |
|
50
|
|
|
*/ |
|
51
|
|
|
public function export($folder) |
|
|
|
|
|
|
52
|
|
|
{ |
|
53
|
|
|
|
|
54
|
|
|
} |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* Export a Model to .xlsx file: |
|
59
|
|
|
|
|
60
|
|
|
use Rap2hpoutre\FastExcel\FastExcel; |
|
61
|
|
|
use App\Models\User; |
|
62
|
|
|
|
|
63
|
|
|
// Load users |
|
64
|
|
|
$users = User::all(); |
|
65
|
|
|
|
|
66
|
|
|
// Export all users |
|
67
|
|
|
(new FastExcel($users))->export('file.xlsx'); |
|
68
|
|
|
* |
|
69
|
|
|
* |
|
70
|
|
|
* Export |
|
71
|
|
|
Export a Model or a Collection: |
|
72
|
|
|
|
|
73
|
|
|
$list = collect([ |
|
74
|
|
|
[ 'id' => 1, 'name' => 'Jane' ], |
|
75
|
|
|
[ 'id' => 2, 'name' => 'John' ], |
|
76
|
|
|
]); |
|
77
|
|
|
|
|
78
|
|
|
(new FastExcel($list))->export('file.xlsx'); |
|
79
|
|
|
Export xlsx, ods and csv: |
|
80
|
|
|
|
|
81
|
|
|
$invoices = App\Invoice::orderBy('created_at', 'DESC')->get(); |
|
82
|
|
|
(new FastExcel($invoices))->export('invoices.csv'); |
|
83
|
|
|
Export only some attributes specifying columns names: |
|
84
|
|
|
|
|
85
|
|
|
(new FastExcel(User::all()))->export('users.csv', function ($user) { |
|
86
|
|
|
return [ |
|
87
|
|
|
'Email' => $user->email, |
|
88
|
|
|
'First Name' => $user->firstname, |
|
89
|
|
|
'Last Name' => strtoupper($user->lastname), |
|
90
|
|
|
]; |
|
91
|
|
|
}); |
|
92
|
|
|
Download (from a controller method): |
|
93
|
|
|
|
|
94
|
|
|
return (new FastExcel(User::all()))->download('file.xlsx'); |
|
95
|
|
|
Import |
|
96
|
|
|
import returns a Collection: |
|
97
|
|
|
|
|
98
|
|
|
$collection = (new FastExcel)->import('file.xlsx'); |
|
99
|
|
|
Import a csv with specific delimiter, enclosure characters and "gbk" encoding: |
|
100
|
|
|
|
|
101
|
|
|
$collection = (new FastExcel)->configureCsv(';', '#', '\n', 'gbk')->import('file.csv'); |
|
102
|
|
|
Import and insert to database: |
|
103
|
|
|
|
|
104
|
|
|
$users = (new FastExcel)->import('file.xlsx', function ($line) { |
|
105
|
|
|
return User::create([ |
|
106
|
|
|
'name' => $line['Name'], |
|
107
|
|
|
'email' => $line['Email'] |
|
108
|
|
|
]); |
|
109
|
|
|
}); |
|
110
|
|
|
Facades |
|
111
|
|
|
You may use FastExcel with the optional Facade. Add the following line to config/app.php under the aliases key. |
|
112
|
|
|
|
|
113
|
|
|
'FastExcel' => Rap2hpoutre\FastExcel\Facades\FastExcel::class, |
|
114
|
|
|
Using the Facade, you will not have access to the constructor. You may set your export data using the data method. |
|
115
|
|
|
|
|
116
|
|
|
$list = collect([ |
|
117
|
|
|
[ 'id' => 1, 'name' => 'Jane' ], |
|
118
|
|
|
[ 'id' => 2, 'name' => 'John' ], |
|
119
|
|
|
]); |
|
120
|
|
|
|
|
121
|
|
|
FastExcel::data($list)->export('file.xlsx'); |
|
122
|
|
|
Global helper |
|
123
|
|
|
FastExcel provides a convenient global helper to quickly instantiate the FastExcel class anywhere in a Laravel application. |
|
124
|
|
|
|
|
125
|
|
|
$collection = fastexcel()->import('file.xlsx'); |
|
126
|
|
|
fastexcel($collection)->export('file.xlsx'); |
|
127
|
|
|
Advanced usage |
|
128
|
|
|
Export multiple sheets |
|
129
|
|
|
Export multiple sheets by creating a SheetCollection: |
|
130
|
|
|
|
|
131
|
|
|
$sheets = new SheetCollection([ |
|
132
|
|
|
User::all(), |
|
133
|
|
|
Project::all() |
|
134
|
|
|
]); |
|
135
|
|
|
(new FastExcel($sheets))->export('file.xlsx'); |
|
136
|
|
|
Use index to specify sheet name: |
|
137
|
|
|
|
|
138
|
|
|
$sheets = new SheetCollection([ |
|
139
|
|
|
'Users' => User::all(), |
|
140
|
|
|
'Second sheet' => Project::all() |
|
141
|
|
|
]); |
|
142
|
|
|
Import multiple sheets |
|
143
|
|
|
Import multiple sheets by using importSheets: |
|
144
|
|
|
|
|
145
|
|
|
$sheets = (new FastExcel)->importSheets('file.xlsx'); |
|
146
|
|
|
You can also import a specific sheet by its number: |
|
147
|
|
|
|
|
148
|
|
|
$users = (new FastExcel)->sheet(3)->import('file.xlsx'); |
|
149
|
|
|
Export large collections with chunk |
|
150
|
|
|
Export rows one by one to avoid memory_limit issues using yield: |
|
151
|
|
|
|
|
152
|
|
|
function usersGenerator() { |
|
153
|
|
|
foreach (User::cursor() as $user) { |
|
154
|
|
|
yield $user; |
|
155
|
|
|
} |
|
156
|
|
|
} |
|
157
|
|
|
|
|
158
|
|
|
// Export consumes only a few MB, even with 10M+ rows. |
|
159
|
|
|
(new FastExcel(usersGenerator()))->export('test.xlsx'); |
|
160
|
|
|
*/ |
|
161
|
|
|
|
Adding a
@returnannotation to a constructor is not recommended, since a constructor does not have a meaningful return value.Please refer to the PHP core documentation on constructors.