Skip to content

Instantly share code, notes, and snippets.

@daniellima
Last active March 28, 2016 18:54
Show Gist options
  • Select an option

  • Save daniellima/9a24fceb968c414e9a5c to your computer and use it in GitHub Desktop.

Select an option

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
LaravelExcel modifications to output CSV files with BOM and a helper class to abstract it
<?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);
}
}
<?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