Laravel-excel: [QUESTION] Excel Messed Up / ExcelExport on Nayjest/Grids does not work

Created on 3 Oct 2018  路  7Comments  路  Source: Maatwebsite/Laravel-Excel

Prerequisites

  • [ ] Able to reproduce the behaviour outside of your code, the problem is isolated to Laravel Excel.
  • [X] Checked that your issue isn't already filed.
  • [X] Checked if no PR was submitted that fixes this problem.

Versions

  • PHP version: 7.2.10
  • Laravel version: 5.6
  • Package version: 3.0

Description

Hey guys. I'm using Nayjest/Grids and I can't use the ExcelExport. It's with a depreciated function there, so I'm trying manually. I followed the documentation and it works. But.... it's all messed up. How can I fix this?

Expected behavior:
I want this way:

ID | Created At | Updated At | Initials | Quantity
2 | 2018-06-20 09:45:30 | 2018-06-20 09:45:30 | EN | 聽 | 7
3 | 2018-06-20 09:45:30 | 2018-06-20 09:45:30 | IN | 聽 | 35

In other words, I need a header or an identification.

Actual behavior:

When I download the xlsx, I have this:

2 | 2018-06-20 09:45:30 | 2018-06-20 09:45:30 | EN | 聽 | 7
3 | 2018-06-20 09:45:30 | 2018-06-20 09:45:30 | IN | 聽 | 35

Additional Information

In the Nayjest/Grids I have this code:

protected function renderExcel()
    {
        /** @var Excel $excel */        
        $excel = app('excel');
        $excel
            ->create($this->getFileName(), $this->getOnFileCreate())
            ->export(new PesoFuncaoExport, 'peso_funcao.xls');     

    }

So, this code does not work because the method create is depreciated. When I try to use the other methods, I cannot download the file, I click on the button, the page is refreshed but I can't download.

I'm using this code

 protected function renderExcel()
    {
        /** @var Excel $excel */        
        $excel = app('excel');
        $excel->download(new PesoFuncaoExport, 'peso_funcao.xlsx' );
            // ->create($this->getFileName(), $this->getOnFileCreate())
            // ->export(new PesoFuncaoExport, 'peso_funcao.xls');  

    }

Anyone can Help?

Most helpful comment

Thanks guys. It worked. I tried the FromQuery Interface and works perfectly for me. Betten than FromCollection.

Thanks a bunch for the help

All 7 comments

Hi @RafaelRodriguesFraga ,

Have you tried implementing the WithHeadings concern in your export class? (see Export concerns
You should then add a method to your export class with an array of heading names, e.g:

public function headings(): array
{
    return [
        'ID',
        'Created At',
        'Updated At',
        'Initials',
        'Quantity'
    ];
}

Please note that it looks like your content does have an extra (empty) column, compared to your heading row.

By the way, you should return the results of the download method as well.

Thanks for the answer and for the advice. It worked, thanks a bunch.

Another question:

  public function collection()
    {
        return Invoice::all();
    }

This will give me all records of my Model, right? I'm asking because I'm getting records that I don't need. How can I ignore these?

peso_funcao

And Can I change the order of the records?

Showing Like This
ID | Created At | Updated At | Initials | Quantity
2 | 2018-06-20 09:45:30 | 2018-06-20 09:45:30 | EN | | 7
3 | 2018-06-20 09:45:30 | 2018-06-20 09:45:30 | IN | | 35

Changing quantity to be after ID, for example

ID | Quantity | Created At | Updated At | Initials
2 | 7 | 2018-06-20 | 09:45:30 | 2018-06-20 09:45:30 | EN
3 | 35 | 2018-06-20 09:45:30 | 2018-06-20 09:45:30 | IN

Hi @RafaelRodriguesFraga,
I haven't tested it myself (yet), but most likely this article will help you suit your needs:
Mapping data

You indeed need the WithMapping concern.

I had read this article, but didn't understand at the first time. I'll try here. Thanks guys

Thanks guys. It worked. I tried the FromQuery Interface and works perfectly for me. Betten than FromCollection.

Thanks a bunch for the help

Was this page helpful?
0 / 5 - 0 ratings

Related issues

pamekar picture pamekar  路  3Comments

kurianic picture kurianic  路  3Comments

disto picture disto  路  3Comments

contifico picture contifico  路  3Comments

alejandri picture alejandri  路  3Comments