maatwebsite laravel excel带有下拉列表的导出列

问题描述 投票:0回答:1

我一直在使用Laravel Excel以csv格式导出数据,到目前为止,它一直很棒。现在,我需要以xlsx格式导出,以便可以在某些列中包括下拉列表。我看过this question,但它似乎适用于Laravel Excel的旧版本。我还查看了文档中解释extending the package的页面,但似乎无法弄清楚在导出数据时如何向列中添加下拉列表。

这是我的导出类的简化版本:

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\Sheet\Maatwebsite\Excel\Writer,但我什至不知道在哪里使用或使用哪个。

在搜索过程中,我似乎无法拼凑出解决方案,因此,我们将不胜感激。

我正在使用:

maatwebsite / excel 3.1,PHP 7.2,laravel 5.8

php excel laravel phpspreadsheet
1个回答
0
投票

工作表事件的实现可能会非常混乱,并且很难找到示例,因此,当我看到这样的帖子时,我会尽力帮助。首先,我要说的是您真的应该在PHPSpreadsheet documentation上查看这些附加功能。您将在这里找到所需的重要信息。然后,您可以翻译要在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)));

                // apply validation to additional 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);
                }
            },
        ];
    }
}
© www.soinside.com 2019 - 2024. All rights reserved.