【问题标题】:Laravel excel library(Maatwebsite) : How to create a drop down list in exportsLaravel excel 库(Maatwebsite):如何在导出中创建下拉列表
【发布时间】:2015-04-23 06:16:59
【问题描述】:

我正在创建一个应包含下拉列表的 excel 模板。我看到它可以使用 phpexcel 库 (PHPExcel Multiple Dropdown list that dependent)。 我想知道是否可以使用maatwebsite 提供的 laravel-excel 库来完成。 我需要 dropdown、NamedRange、datavalidation、setFormula 等函数的语法。

【问题讨论】:

  • 你可以研究 laravel-excel 库的 Facade 结构,因为它只是原始 phpexcel 包的包装器。
  • Laravel-excel 提供了一个 Eloquent 风格的 API,因此您不会在原始 phpexcel 与 laravel-excel 库之间进行直接转换。
  • 如果您的下拉列表始终在同一个位置,请考虑在 Excel 中的模板上制作它,然后通过您的站点将数据添加到模板的副本中。使用动态命名范围将允许您拥有级联下拉列表,您可以在其中更改电子表格范围中的数据。

标签: php laravel maatwebsite-excel


【解决方案1】:

对于 maatwebite 版本 V.3.1,请使用以下代码

<?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 . '"');
              
            }
        ];
    }
}

【讨论】:

    【解决方案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 如何将我的数据数组传递给它。
    • @user993553 你能帮我在 maatwebsite v.3.1 中创建下拉菜单
    【解决方案3】:

    只想分享关于namedRange for =INDIRECT 函数下拉列表, 当您尝试将它与 registerEvents 一起使用时,这是我的示例代码

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

    在 namedRange 中调用单元格时使用 $ 以准确影响您的单元格。我希望它会帮助你们中的一些人。 参考:https://phpspreadsheet.readthedocs.io/en/latest/topics/defined-names/#named-ranges

    这里是 INDIRECT excel 函数参考: https://www.exceldemy.com/excel-data-validation-based-on-another-cell/

    【讨论】:

    • 谢谢。为您解答。它对我很有帮助。它的工作就像一个魅力!
    • 很高兴听到这个消息,干杯!
    猜你喜欢
    • 1970-01-01
    • 2020-09-04
    • 2017-11-08
    • 2019-05-20
    • 2012-01-02
    • 2016-11-23
    • 1970-01-01
    • 1970-01-01
    • 2021-05-01
    相关资源
    最近更新 更多