vd/vendor/dipper/excel/README.md
2018-12-29 10:19:35 +08:00

130 lines
3.2 KiB
Markdown

<h1 align="center">
Laravel Excel 3.1
</h1>
<p align="center">
<strong>Supercharged Excel exports and imports</strong><br>
A simple, but elegant wrapper around <a href="https://phpspreadsheet.readthedocs.io/">PhpSpreadsheet</a> with the goal of simplifying
exports and imports.
</p>
- **Easily export collections to Excel.** Supercharge your Laravel collections and export them directly to an Excel or CSV document. Exporting has never been so easy.
- **Supercharged exports.** Export queries with automatic chunking for better performance. You provide us the query, we handle the performance. Exporting even larger datasets? No worries, Laravel Excel has your back. You can queue your exports so all of this happens in the background.
- **Supercharged imports.** Import workbooks and worksheets to Eloquent models with chunk reading and batch inserts! Have large files? You can queue every chunk of a file! Your entire import will happen in the background.
- **Export Blade views.** Want to have a custom layout in your spreadsheet? Use a HTML table in a Blade view and export that to Excel.
## :rocket: 5 minutes quick start for exports
:bulb: Require this package in the `composer.json` of your Laravel project. This will download the package and PhpSpreadsheet.
```
composer require dipper/excel
```
:muscle: Create an export class in `App/Exports`
```
php artisan make:export UsersExport --model=User
```
This should have created:
```php
<?php
namespace App\Exports;
use App\User;
use Dipper\Excel\Concerns\FromCollection;
class UsersExport implements FromCollection
{
public function collection()
{
return User::all();
}
}
```
:fire: In your controller you can call this export now:
```php
use App\Exports\UsersExport;
use Dipper\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class UsersController extends Controller
{
public function export()
{
return Excel::download(new UsersExport, 'users.xlsx');
}
}
```
:page_facing_up: Find your `users.xlsx` in your downloads folder!
## :rocket: 5 minutes quick start for imports
:muscle: Create an import class in `App\Imports`
You may do this by using the `make:import` command.
```
php artisan make:import UsersImport --model=User
```
If you prefer to create the import manually, you can create the following in `App\Imports`:
```php
<?php
namespace App\Imports;
use App\User;
use Illuminate\Support\Facades\Hash;
use Dipper\Excel\Concerns\ToModel;
class UsersImport implements ToModel
{
/**
* @param array $row
*
* @return User|null
*/
public function model(array $row)
{
return new User([
'name' => $row[0],
'email' => $row[1],
'password' => Hash::make($row[2]),
]);
}
}
```
:fire: In your controller you can call this import now:
```php
use App\Imports\UsersImport;
use Dipper\Excel\Facades\Excel;
use App\Http\Controllers\Controller;
class UsersController extends Controller
{
public function import()
{
Excel::import(new UsersImport, 'users.xlsx');
return redirect('/')->with('success', 'All good!');
}
}
```
:page_facing_up: Find the imported users in your database!