Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel excel library(Maatwebsite) : How to create a drop down list in exports

I am creating an excel template which should contain a dropdown list. I see its possible with the phpexcel library (PHPExcel Multiple Dropdown list that dependent). I was wondering if it could be done with the laravel-excel library provided by maatwebsite. I need the syntax for functions like dropdown,NamedRange, datavalidation,setFormula, etc.

like image 518
Sankalp Tambe Avatar asked Apr 23 '15 06:04

Sankalp Tambe


People also ask

Can you import data to multiple tables from one Excel spreadsheet using laravel?

Below you can find complete step by step process of how to use Laravel Maatwebsite package for import Excel sheet data into multiple tables of database in Laravel. This will create two tables named as customers and courses. We can change the constants of the table as following.


2 Answers

Just want to share regarding namedRange for =INDIRECT function dropdown list, when you're trying to use it with registerEvents, here's my sample code

AfterSheet::class => function(AfterSheet $event) {
       $event->sheet->getDelegate()->getParent()->addNamedRange(
             new PhpOffice\PhpSpreadsheet\NamedRange(
                'mynamerange',
                $event->sheet->getDelegate(),
               '=$A$1:$A$10')
       );
},

use $ when calling cell in namedRange to take affect on your cell accurately. I hope it'll help some of you. reference: https://phpspreadsheet.readthedocs.io/en/latest/topics/defined-names/#named-ranges

here for INDIRECT excel function reference: https://www.exceldemy.com/excel-data-validation-based-on-another-cell/

like image 69
Drey Avatar answered Sep 22 '22 07:09

Drey


for maatwebite version V.3.1 use below code

<?php

namespace App\Exports;

use App\Models\Category;

use Illuminate\Contracts\View\View;
use Maatwebsite\Excel\Concerns\FromView;
use Maatwebsite\Excel\Concerns\WithEvents;
use Maatwebsite\Excel\Concerns\WithTitle;
use Maatwebsite\Excel\Events\AfterSheet;
use PhpOffice\PhpSpreadsheet\Cell\DataValidation;

class BulkUploadProductExport implements FromView, WithEvents , WithTitle
{
  

    public function view(): View
    {
        return view('exports', [
            'categories' => Category::all()
        ]);
    }

    public function title(): string
    {
        return 'bulkupload';
    }

    public function registerEvents(): array
    {

        //$event = $this->getEvent();
        return [
            AfterSheet::class => function (AfterSheet $event) {
               
                /** @var Sheet $sheet */
                $sheet = $event->sheet;

                /**
                 * validation for bulkuploadsheet
                 */

                $sheet->setCellValue('B5', "SELECT ITEM");
                $configs = "DUS800, DUG900+3xRRUS, DUW2100, 2xMU, SIU, DUS800+3xRRUS, DUG900+3xRRUS, DUW2100";
                $objValidation = $sheet->getCell('B5')->getDataValidation();
                $objValidation->setType(DataValidation::TYPE_LIST);
                $objValidation->setErrorStyle(DataValidation::STYLE_INFORMATION);
                $objValidation->setAllowBlank(false);
                $objValidation->setShowInputMessage(true);
                $objValidation->setShowErrorMessage(true);
                $objValidation->setShowDropDown(true);
                $objValidation->setErrorTitle('Input error');
                $objValidation->setError('Value is not in list.');
                $objValidation->setPromptTitle('Pick from list');
                $objValidation->setPrompt('Please pick a value from the drop-down list.');
                $objValidation->setFormula1('"' . $configs . '"');
              
            }
        ];
    }
}
like image 40
Aasif khan Avatar answered Sep 22 '22 07:09

Aasif khan