Posted in

Simple Excel Export en Laravel

Para instalar el módulo desde Github: https://github.com/Maatwebsite/Laravel-Excel

Para ver la documentación: http://www.maatwebsite.nl/laravel-excel/docs/export

Un ejemplo práctico:

public function exportFactures(){
Excel::create('Factures', function($excel) {

$excel->sheet('Totes les factures', function($sheet) {

$sheet->row(1, array(
 'Num. factura', 'Concepte', 'Notes', 'Client', 'Import base', 'Iva', 'Total', 'Data factura'
));
$dades = Factura::with('client')->get();
$data = array();
foreach($dades as $dada){
$base = DB::table('conceptes')->where('factura_id', '=', $dada->id)->sum('total');
$iva = ($base / 100) * $dada->iva;
$total = number_format($base + $iva, 2, ',', '');
$dadaIn = array(
$dada->num_factura . '/' . substr(date("Y", strtotime($dada->fecha_factura)), -2), 
$dada->concepto, 
$dada->notas, 
$dada->client->company, 
$base, 
$dada->iva . '%', 
$total, 
$dada->fecha_factura
);
array_push($data, $dadaIn);
}

$sheet->setOrientation('landscape');
$sheet->fromArray($data, null, 'A1', true);
$sheet->row(1, function($row) {
$row->setBackground('#5fb37b');
$row->setFontColor('#ffffff');

Un ejemplo más optimizado:

 Excel::create('Inscripcions', function($excel) {
            $excel->sheet('Sheetname', function($sheet) {

            $assistents = Asistent::where('trobades_id', $id)->select('nom','cognoms','email','telefon')->get();

            foreach ($assistents as $assistent)

                $sheet->fromArray($assistent, null, 'A1', true);
                $sheet->row(1, array());
                $sheet->row(1, function($row) {
                    $row->setBackground('#5fb37b'); 
                    $row->setFontColor('#ffffff');          
                });
            });
        })->export('xls');

Deixa un comentari

L'adreça electrònica no es publicarà. Els camps necessaris estan marcats amb *


The reCAPTCHA verification period has expired. Please reload the page.