Last active
March 28, 2016 18:54
-
-
Save daniellima/9a24fceb968c414e9a5c to your computer and use it in GitHub Desktop.
LaravelExcel modifications to output CSV files with BOM and a helper class to abstract it
This file contains hidden or 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
| LaravelExcel modifications to output CSV files with BOM and a helper class to abstract it |
This file contains hidden or 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
| <?php | |
| namespace App\ExcelExport; | |
| use Maatwebsite\Excel\Classes\PHPExcel; | |
| use Maatwebsite\Excel\Excel; | |
| use Maatwebsite\Excel\Readers\LaravelExcelReader; | |
| class ExcelExporter | |
| { | |
| public function export($name, $headers, $data, $type){ | |
| $excel = new Excel( | |
| app(PHPExcel::class), | |
| app(LaravelExcelReader::class), | |
| //app(\Maatwebsite\Excel\Writers\LaravelExcelWriter::class) | |
| app(LaravelExcelWriterWithBetterCSVSupport::class) | |
| ); | |
| $excel->create($name, function($document) use($data, $name, $headers) { | |
| // normal code here | |
| })->export($type); | |
| } | |
| } |
This file contains hidden or 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
| <?php | |
| namespace App\ExcelExport; | |
| use Maatwebsite\Excel\Writers\LaravelExcelWriter; | |
| class LaravelExcelWriterWithBetterCSVSupport extends LaravelExcelWriter | |
| { | |
| protected function _setWriter() | |
| { | |
| $writer = parent::_setWriter(); | |
| if($this->format == 'CSV') | |
| $writer->setUseBOM(true); | |
| return $writer; | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment