Back to Excel Support

office18072
office18072

Hi, thank you for this plugin!
I would like export data from my custom plugin from backend -
somehow it doesn't work. (it gives me no warning, error etc - it seems to load but it never reaches my downloads folder // short description:
in /controllers/update.htm
I created an ajax enabled button which is calling my
onExportExcel Function which returns
Excel::export(CompetitionExport::class, 'competition', 'xlsx')

When I try it with the same Export Class from frontend as per your definition - it works as expected.
Thanks for advice!

Last updated

vdomah
vdomah

Hello! I haven't used this plugin in backend because there is Import/Export feature in backend from scratch. In case you haven't seen: https://octobercms.com/docs/backend/import-export

Or the basic functionality doesn't work for you? Why?

office18072
office18072

Thanks for your reply!
yes I know this feature import/export - but it does not solve the task I like to accomplish..
(customer likes to get .xlsx file output / from that I can only export .csv from what I know)
Would be really nice to get it to work

vdomah
vdomah

To solve this we need some more info. Have you checked logs for errors?

office18072
office18072

yes I did - there are no error messages - should I send you something?

vdomah
vdomah

To advice anything I need to have at least some info to make a judgment. If you can send something that can bring the cause to light, do it please

office18072
office18072

Here is the button located:
plugins/xxx/xxx/controllers/competitions/update.htm:

            <button
                type="button"
                data-request="onExportExcel"
                data-request-data="id: <?= implode($this->params) ?>"
                data-hotkey="ctrl+e, cmd+e"
                data-load-indicator="Exporting Competition..."
                class="btn btn-primary oc-icon-file-text-o">
                Exportiere Teilnehmer des Bewerbes als Excel Sheet
            </button>

which is calling:
plugins/xxx/xxx/controllers/competitions.php

use Vdomah\Excel\Classes\Excel;
use A711\Classic\Excel\CompetitionExport;

/**
 * Competitions Back-end Controller
 */
class Competitions extends Controller
{        
...
    public function onExportExcel()
    {
        return Excel::export(CompetitionExport::class, 'competition', 'xlsx');
    }

which should return
plugins/xxx/xxx/excel/CompetitionExport.php

use A711\Classic\Models\Competition;
use Maatwebsite\Excel\Concerns\FromCollection;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithHeadings;
use Maatwebsite\Excel\Events\AfterSheet;
use Illuminate\Support\Facades\Input;

class CompetitionExport implements FromCollection, WithHeadings, WithEvents
{
// set the headings
public function headings(): array
{
    return [
        'Bewerb', 'Beschreibung'
    ];
}
// , 'Beifahrer Nachname', 'Beifahrer Vorname', 'PLZ', 'Ort', 'Straße'
// freeze the first row with headings
public function registerEvents(): array
{
    return [
        AfterSheet::class => function (AfterSheet $event) {
            $event->sheet->freezePane('A2', 'A2');
        },
    ];
}
// get the data
public function collection()
{
    $competition = Competition::all();

    $data = [];
    if ($competition->count() > 0) {
        foreach ($competition as $comp) {
            $data[] = [$comp->bewerb, $comp->description];
        }
    }
    return collect($data);
}

thats it .... (of course I like to extend the excel columns, when basic thing is working..)

Thanks, if you have any idea - would be great!

vdomah
vdomah

If this code works on front-end I can't even say what's wrong with backend right away. So need to dig and probably debug all the trace step by step.

And if you want to hide your plugin name and path you better hide your namespace in code snippets also.

office18072
office18072

:) thanks - anyway - if you have an idea please let me know - it would be hard for me to accept that I can use this useful plugin only for frontend matters. If I come up with a solution inbetween I will let you know.

office18072
office18072

okay - so I found this helpful advice:
https://octobercms.com/forum/post/serve-download-in-response-to-backend-ajax-response
what causes me to add another controller function and this makes my button work:

    public function onExportExcel()
    {
         return Backend::redirect('a711/classic/competitions/download-excel');
    }

    public function downloadExcel()
    {
      return Excel::export(CompetitionExport::class, 'competition', 'xlsx');
    }

Only thing - I need a solution that my button (with loading indicator) goes back to normal state after download..

1-10 of 10