亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

maatwebsite laravel excel 導出列與下拉列表

maatwebsite laravel excel 導出列與下拉列表

PHP
呼啦一陣風 2022-05-27 14:56:39
我一直在使用Laravel Excel以 csv 格式導出數據,到目前為止效果很好?,F在我需要以 xlsx 格式導出,以便可以在某些列中包含下拉列表。我看過這個問題,但看起來這是針對舊版本的 Laravel Excel。我還查看了文檔中解釋擴展包的頁面,但我似乎無法弄清楚如何在導出數據時將下拉列表添加到列中。這是我的導出類的簡化版本:namespace App\Exports;use Maatwebsite\Excel\Concerns\FromCollection;use Maatwebsite\Excel\Concerns\WithHeadings;use Maatwebsite\Excel\Concerns\WithStrictNullComparison;class ActionItemExport implements FromCollection, WithHeadings, WithStrictNullComparison{    public function collection()    {        return $this->getActionItems();    }    public function headings(): array    {        $columns = [            'Column 1',            'Column 2',            'Column 3',            'Column 4',            'Column 5',            'Column 6',            'Column 7'        ];        return $columns;    }    private function getActionItems()    {        $select = 'column1, column2, column3, column4, column5, column6, column7';        $query = \DB::table('action_items')->select(\DB::raw($select));        $query->whereNull('action_items.deleted_at');        $ai = $query->orderBy('column1')->get();        return $ai;    }}我想做的是查詢具有 column1 選項的查找表,并將這些值用于列中的下拉列表,以便當用戶想要更改 Excel 工作表時,它們僅限于下拉值.在文檔中它提到使用\Maatwebsite\Excel\Sheetor \Maatwebsite\Excel\Writer,但我什至不確定在哪里使用它們,或者使用哪個。在我的搜索過程中,我似乎無法拼湊出一個解決方案,因此我們將不勝感激。我在用著:maatwebsite/excel 3.1、php 7.2、laravel 5.8
查看完整描述

1 回答

?
滄海一幻覺

TA貢獻1824條經驗 獲得超5個贊

表單事件的實現可能相當混亂,很難找到示例,所以當我看到這樣的帖子時,我會嘗試伸出援手。首先,我會說您真的應該查看這些附加功能的PHPSpreadsheet 文檔。在這里您可以找到所需的重要信息。然后你可以翻譯你在Laravel Excel中使用的內容。


PHPSpreadsheet:在單元格上設置數據驗證 https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#setting-data-validation-on-a-cell


這是一個基于您現有文件的示例。我還加入了一些額外的格式來自動調整列寬——我認為這是必須的。


namespace App\Exports;


use Maatwebsite\Excel\Concerns\FromCollection;

use Maatwebsite\Excel\Concerns\WithHeadings;

use Maatwebsite\Excel\Concerns\WithStrictNullComparison;

use Maatwebsite\Excel\Concerns\WithEvents;

use Maatwebsite\Excel\Events\AfterSheet;

use PhpOffice\PhpSpreadsheet\Cell\Coordinate;

use PhpOffice\PhpSpreadsheet\Cell\DataValidation;


class ActionItemExport implements FromCollection, WithHeadings, WithEvents, WithStrictNullComparison

{

    protected $results;


    public function collection()

    {

        // store the results for later use

        $this->results = $this->getActionItems();


        return $this->results;

    }


    // ...


    public function registerEvents(): array

    {

        return [

            // handle by a closure.

            AfterSheet::class => function(AfterSheet $event) {


                // get layout counts (add 1 to rows for heading row)

                $row_count = $this->results->count() + 1;

                $column_count = count($this->results[0]->toArray());


                // set dropdown column

                $drop_column = 'A';


                // set dropdown options

                $options = [

                    'option 1',

                    'option 2',

                    'option 3',

                ];


                // set dropdown list for first data row

                $validation = $event->sheet->getCell("{$drop_column}2")->getDataValidation();

                $validation->setType(DataValidation::TYPE_LIST );

                $validation->setErrorStyle(DataValidation::STYLE_INFORMATION );

                $validation->setAllowBlank(false);

                $validation->setShowInputMessage(true);

                $validation->setShowErrorMessage(true);

                $validation->setShowDropDown(true);

                $validation->setErrorTitle('Input error');

                $validation->setError('Value is not in list.');

                $validation->setPromptTitle('Pick from list');

                $validation->setPrompt('Please pick a value from the drop-down list.');

                $validation->setFormula1(sprintf('"%s"',implode(',',$options)));


                // clone validation to remaining rows

                for ($i = 3; $i <= $row_count; $i++) {

                    $event->sheet->getCell("{$drop_column}{$i}")->setDataValidation(clone $validation);

                }


                // set columns to autosize

                for ($i = 1; $i <= $column_count; $i++) {

                    $column = Coordinate::stringFromColumnIndex($i);

                    $event->sheet->getColumnDimension($column)->setAutoSize(true);

                }

            },

        ];

    }

}


查看完整回答
反對 回復 2022-05-27
  • 1 回答
  • 0 關注
  • 398 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號