forked from SpartnerNL/Laravel-Excel
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c6b43f9
commit dbea0fb
Showing
9 changed files
with
217 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
# NewExcelFile injections | ||
|
||
Following the Laravel 5.0 philosophy with its new awesome FormRequest injections, we introduce you NewExcelFile injections. | ||
|
||
## NewExcelFile class | ||
|
||
This NewExcelFile is a wrapper for a new Excel file. Inside the `getFilename()` you can declare the wanted filename. | ||
|
||
class UserListExport extends \Maatwebsite\Excel\Files\NewExcelFile { | ||
|
||
public function getFilename() | ||
{ | ||
return 'filename'; | ||
} | ||
} | ||
|
||
## Usage | ||
|
||
You can inject these NewExcelFiles inside the __constructor or inside the method (when using Laravel 5.0), in e.g. the controller. | ||
|
||
class ExampleController extends Controller { | ||
|
||
public function exportUserList(UserListExport $export) | ||
{ | ||
// work on the export | ||
$export->sheet('sheetName', function($sheet) | ||
{ | ||
|
||
}); | ||
} | ||
|
||
} | ||
|
||
## Export Handlers | ||
|
||
To decouple your Excel-export code completely from the controller, you can use the export handlers. | ||
|
||
class ExampleController extends Controller { | ||
|
||
public function exportUserList(UserListExport $export) | ||
{ | ||
// Handle the export | ||
$export->handleExport(); | ||
} | ||
|
||
} | ||
|
||
The `handleExport()` method will dynamically call a handler class which is your class name appended with `Handler` | ||
|
||
class UserListExportHandler implements \Maatwebsite\Excel\Files\ExportHandler { | ||
|
||
public function handle(UserListExport $export) | ||
{ | ||
// work on the export | ||
$export->sheet('sheetName', function($sheet) | ||
{ | ||
|
||
}); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,9 +1,13 @@ | ||
@include:Importing a file|basics | ||
@include:ExcelFile injections|injection | ||
@include:Handling results|results | ||
@include:Selecting sheets and columns|select | ||
@include:Dates|dates | ||
@include:Calculation|calculation | ||
@include:Caching and cell caching|cache | ||
@include:Chunk importing|chunk | ||
@include:Batch import|batch | ||
@include:Import by config|config | ||
@include:Edit existing files|edit | ||
@include:Converting|convert | ||
@include:Extra|extra |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
# Chunk importer | ||
|
||
When dealing with big files, it's better to import the data in big chunks. You can enable this with `filter('chunk'); | ||
To import it into chunks you can use `chunk($size, $callback)` instead of the normal `get()`. The first parameter is the size of the chunk. The second parameter is a closure which will return the results. | ||
|
||
Excel::filter('chunk')->load('file.csv')->chunk(250, function($results) | ||
{ | ||
foreach($results as $row) | ||
{ | ||
// do stuff | ||
} | ||
}); | ||
|
||
When working with ExcelFile injections (instead the constructor or as method injection), you can enable the chunk filter inside the ExcelFile class | ||
|
||
ExcelFile class example: | ||
|
||
class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile { | ||
|
||
public function getFile() | ||
{ | ||
return 'file.csv'; | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return [ | ||
'chunk' | ||
]; | ||
} | ||
|
||
} | ||
|
||
Injected ExcelFile example: | ||
|
||
public function importUserList(UserListImport $import) | ||
{ | ||
$import->chunk(250, function($results) | ||
{ | ||
// do stuff | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Converting | ||
|
||
You can convert from one filetype to another by using `->convert()` | ||
|
||
Excel::load('file.csv', function($file) { | ||
|
||
// modify stuff | ||
|
||
})->convert('xls'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,9 @@ | ||
# Editing existing files | ||
|
||
You can edit existing Excel files, by loading them and after modication exporting them. | ||
|
||
Excel::load('file.csv', function($file) { | ||
|
||
// modify stuff | ||
|
||
})->export('csv'); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
# ExcelFile injections | ||
|
||
Following the Laravel 5.0 philosophy with its new awesome FormRequest injections, we introduce you ExcelFile injections. | ||
|
||
## ExcelFile class | ||
|
||
This class is a wrapper for a file on your server. Inside the `getFile()` method you return the filename and it's location. Inside the `getFilters()` method you can enable filters, like the chunk filter. | ||
|
||
class UserListImport extends \Maatwebsite\Excel\Files\ExcelFile { | ||
|
||
public function getFile() | ||
{ | ||
return storage_path('exports') . '/file.csv'; | ||
} | ||
|
||
public function getFilters() | ||
{ | ||
return [ | ||
'chunk' | ||
]; | ||
} | ||
|
||
} | ||
|
||
If you want to have the `getFile()` dynamic based on user's input, you can easily do: | ||
|
||
public function getFile() | ||
{ | ||
// Import a user provided file | ||
$file = Input::file('report'); | ||
$filename = $this->doSomethingLikeUpload($file); | ||
|
||
// Return it's location | ||
return $filename; | ||
} | ||
|
||
## Usage | ||
|
||
You can inject these ExcelFiles inside the __constructor or inside the method (when using Laravel 5.0), in e.g. the controller. | ||
|
||
class ExampleController extends Controller { | ||
|
||
public function importUserList(UserListImport $import) | ||
{ | ||
// get the results | ||
$results = $import->get(); | ||
} | ||
|
||
} | ||
|
||
## Import Handlers | ||
|
||
To decouple your Excel-import code completely from the controller, you can use the import handlers. | ||
|
||
class ExampleController extends Controller { | ||
|
||
public function importUserList(UserListImport $import) | ||
{ | ||
// Handle the import | ||
$import->handleImport(); | ||
} | ||
|
||
} | ||
|
||
The `handleImport()` method will dynamically call a handler class which is your class name appended with `Handler` | ||
|
||
class UserListImportHandler implements \Maatwebsite\Excel\Files\ImportHandler { | ||
|
||
public function handle(UserListImport $import) | ||
{ | ||
// get the results | ||
$results = $import->get(); | ||
} | ||
|
||
} |