Are you using the Laravel Excel Maatwebsite package ?
Laravel Excel export multiple sheets
Hi, I'm trying to implement an excel export with multiple sheets like in documentation but something it's not working like it should. I'm getting an excel with 3 worksheets, like I want but they are duplicates of the same worksheet. This is my code:
class SheetsExport implements WithMultipleSheets { use Exportable; /** * @return \Illuminate\Support\Collection */ protected $tipuri; public function __construct($tipuri) { $this->tipuri = $tipuri; }
public function sheets(): array
{
$sheets = [];
foreach($this->tipuri as $tip)
$sheets[] = new FisierListareSheet($tip);
return $sheets;
}
}
and $tipuri is this array $tipuri = array('Posta','Sediu','E-mail');
And the child export method:
class FisierListareSheet implements FromQuery, WithHeadings, WithTitle { /** * @return \Illuminate\Support\Collection */
private $tip;
public function __construct(string $tip)
{
if($tip = 'posta'){
$this->tip = $tip;
}
elseif($tip = 'Sediu')
{
$this->tip = '(10)Sediu';
dd($tip);
}
else{
$this->tip = '(12) E-mail';
}
}
public function headings():array
{
return ['Cod client','Client','Contract nr.','RegCom','CUI','Adresa/sediul social','Cont','Banca','Loc consum','Localitate','Judet','Cod postal','Nr Fact',
'Data Factura','Cota TVA','Nr1','Prod 1','UM1','Cant 1','Pret unitar','Tot1','TVA1','Nr2','Prod 2','UM2','Cant 2','Pret unitar','Tot2','TVA2',
'Nr3','Prod 3','UM3','Cant 3','Pret unitar','Tot3','TVA3','Nr4','Prod 4','UM4','Cant 4','Pret unitar','Tot4','TVA4','Nr5',
'Prod 5','UM5','Cant 5','Pret unitar','Tot5','TVA5','TOTAL 5','Nr6','Prod 6','UM6','Cant 6','Pret unitar','Tot6','TVA6','TOTAL 6','Nr7','Prod 7','UM7',
'Cant 7','Pret unitar','Tot7','TVA7','TOTAL 7','Nr8','Prod 8','UM8','Cant 8','Pret unitar','Tot8','TVA8','TOTAL 8','Nr9','Prod 9','UM9','Cant 9','Pret unitar',
'Tot9','TVA9','TOTAL 9','Total valoare','Subventie','Valoare Subventie','Total TVA','Total factura curenta cu TVA','Denumire-Total de plata in luna',
'Total de plata in luna','Denumire-Sold precedent','Sold precedent','Denumire-Total de plata','Total de plata','Pcs','Consum Lunar(mc)-curent','Serie contor',
'Scadenta','Index vechi(citire/autocitire/estimare/schimbare contract)','Index nou(citire/autocitire/estimare/schimbare contract)','Categorie consum',
'Consum Mc-istoric','Energie(kWh)-istoric','Variatie Consum','Temei legal','CLC','COD DE BARE','Perioada de consum(date consum anual)','Perioada de consum(date consum istoric)'];
}
public function query()
{
$listari = DB::table('assets')
->join('asset_categories','assets.name','=','asset_categories.name')
->join('tip_trimitere','assets.name','=','tip_trimitere.clc')
->where('tip_trimitere.tip',$this->tip)
->select('asset_categories.cod_client','asset_categories.client','asset_categories.nr_contract','asset_categories.reg_com',
'asset_categories.cui','asset_categories.adresa','asset_categories.cont','asset_categories.banca','asset_categories.loc_consum',
'asset_categories.localitate','asset_categories.judet','asset_categories.cod_postal','assets.nr_fact','assets.data_factura','assets.cota_tva',
'assets.nr_1','assets.prod_1','assets.um_1','assets.cant_1','assets.pret_unitar_1','assets.tot_1','assets.tva_1',
'assets.nr_2', 'assets.prod_2','assets.um_2','assets.cant_2','assets.pret_unitar_2','assets.tot_2','assets.tva_2',
'assets.nr_3','assets.prod_3','assets.um_3','assets.cant_3','assets.pret_unitar_3','assets.tot_3','assets.tva_3',
'assets.nr_4','assets.prod_4','assets.um_4','assets.cant_4','assets.pret_unitar_4','assets.tot_4','assets.tva_4',
'assets.nr_5','assets.prod_5','assets.um_5','assets.cant_5','assets.pret_unitar_5','assets.tot_5','assets.tva_5','assets.total_5',
'assets.nr_6','assets.prod_6','assets.um_6','assets.cant_6','assets.pret_unitar_6','assets.tot_6','assets.tva_6','assets.total_6',
'assets.nr_7','assets.prod_7','assets.um_7','assets.cant_7','assets.pret_unitar_7','assets.tot_7','assets.tva_7','assets.total_7',
'assets.nr_8','assets.prod_8','assets.um_8','assets.cant_8','assets.pret_unitar_8','assets.tot_8','assets.tva_8','assets.total_8',
'assets.nr_9','assets.prod_9','assets.um_9','assets.cant_9','assets.pret_unitar_9','assets.tot_9','assets.tva_9','assets.total_9',
'assets.total_valoare','assets.subventie','assets.valoare_subventie','assets.total_tva','assets.total_factura_curenta_cu_tva',
'assets.denumire_total_plata_luna','assets.total_de_plata_in_luna','assets.denumire_sold_precedent','assets.sold_precedent','assets.denumire_total_plata','assets.total_de_plata','assets.pcs',
'assets.consum_lunar_mc_curent','assets.serie_contor','assets.scadenta','assets.index_vechi',
'assets.index_nou','assets.categorie_consum','assets.consum_mc_istoric','assets.energie_k_wh_istoric',
'assets.variatie_consum','assets.temei_legal','asset_categories.name','assets.cod_bare','assets.perioada_de_consum_date_consum_anual','assets.perioada_de_consum_date_consum_istoric')->orderBy('assets.name');
return $listari;
}
public function title(): string
{
if($this->tip = 'posta'){
return 'Posta';
}
elseif( $this->tip = '(10)Sediu')
{
return 'Sediu';
}
else{
return 'E-mail';
}
}
}
Please ignore the number of columns. Appreciate your help!
@LaurentiuDaniel In your public function title(), you have an error in your condition.
Your have to write if ($this->tip == 'posta') instead of if ($this->tip = 'posta').
Please or to participate in this conversation.