15

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.

Community
  • 1
  • 1
Sankalp Tambe
  • 400
  • 4
  • 15
  • 1
    You could study the Facade structure for the laravel-excel library as this is just a wrapper to the original phpexcel package. – Muffy May 21 '15 at 14:42
  • Laravel-excel provides an Eloquent style API so you will not have a direct translation between the raw phpexcel vs laravel-excel library. – Muffy May 21 '15 at 14:49
  • If your dropdown list is always in the same place, consider making it on the template in Excel and then just adding your data to a copy of the template through your site. Using dynamic named ranges will allow you to have cascading drop down lists where you can just change the data in a spreadsheet range. – Dan May 26 '15 at 08:16

4 Answers4

6

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 . '"');
              
            }
        ];
    }
}
Aasif khan
  • 161
  • 2
  • 8
2
  public function index() {
        \Excel::create('file', function($excel) {
            require_once("/apppath//vendor/phpoffice/phpexcel/Classes/PHPExcel/NamedRange.php");
            require_once("/apppath/vendor/phpoffice/phpexcel/Classes/PHPExcel/Cell/DataValidation.php");

            $excel->sheet('New sheet', function($sheet) {

                $sheet->SetCellValue("A1", "UK");
                $sheet->SetCellValue("A2", "USA");

                $sheet->_parent->addNamedRange(
                        new \PHPExcel_NamedRange(
                        'countries', $sheet, 'A1:A2'
                        )
                );


                $sheet->SetCellValue("B1", "London");
                $sheet->SetCellValue("B2", "Birmingham");
                $sheet->SetCellValue("B3", "Leeds");
                $sheet->_parent->addNamedRange(
                        new \PHPExcel_NamedRange(
                        'UK', $sheet, 'B1:B3'
                        )
                );

                $sheet->SetCellValue("C1", "Atlanta");
                $sheet->SetCellValue("C2", "New York");
                $sheet->SetCellValue("C3", "Los Angeles");
                $sheet->_parent->addNamedRange(
                        new \PHPExcel_NamedRange(
                        'USA', $sheet, 'C1:C3'
                        )
                );
                $objValidation = $sheet->getCell('D1')->getDataValidation();
                $objValidation->setType(\PHPExcel_Cell_DataValidation::TYPE_LIST);
                $objValidation->setErrorStyle(\PHPExcel_Cell_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('countries'); //note this!
            });
        })->download("xlsx");
        return view('home');
    }
user993553
  • 1,077
  • 5
  • 12
2
public function registerEvents(): array
{
    return [
        AfterSheet::class => function (AfterSheet $event) {
            $sheet = $event->sheet;
            $event->sheet->getStyle('A5:E5')->applyFromArray([
                'font' => [
                    'bold' => true,
                ]
            ]);
            for ($j = 6; $j <= 2000; $j++) {
                for ($i = 0; $i < count($this->column_name); $i++) {
                    $objValidation = $sheet->getCell($this->columns[$i] . $j)->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.');
                    $index = $this->lengths[$i] == 0 ? 1 : $this->lengths[$i];
                    $objValidation->setFormula1($this->sheet_names[$i] . '!$A$1:$A$' . $index);
                }
            }
        }
    ];
}`enter code here`
Ahmad Reza Azimi
  • 534
  • 5
  • 16
1

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/

Drey
  • 59
  • 1
  • 9