自定义网站 laravel excel 导出带有下拉列表的列
maatwebsite laravel excel export columns with drop down list
我一直在使用 Laravel Excel to export data in csv format and it has been great so far. Now I need to export in xlsx format so that I can include dropdown lists in some of the columns. I have looked at this question but it looks like that is for an older version of Laravel Excel. I also looked at the page in the docs that explains extending the package,但我似乎无法弄清楚如何在导出数据时向列添加下拉列表。
这是我导出的简化版本class:
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 选项的查找 table 并将这些值用于列中的下拉列表,以便当用户想要更改 excel sheet,它们仅限于下拉值。
在文档中它提到使用 \Maatwebsite\Excel\Sheet
或 \Maatwebsite\Excel\Writer
,但我什至不确定在哪里使用它们,或者使用哪个。
在我的整个搜索过程中,我似乎无法拼凑出一个解决方案,因此我们将不胜感激。
我正在使用:
maatwebsite/excel 3.1,
php7.2,
laravel 5.8
sheet 事件的实现可能相当混乱,很难找到示例,所以当我看到这样的 post 时,我会尽力提供帮助。首先,我会说你真的应该看看 PHPSpreadsheet documentation for these additional features. It's where you're going to find the important information you need. Then you can translate what you find for use in 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);
}
},
];
}
}
我一直在使用 Laravel Excel to export data in csv format and it has been great so far. Now I need to export in xlsx format so that I can include dropdown lists in some of the columns. I have looked at this question but it looks like that is for an older version of Laravel Excel. I also looked at the page in the docs that explains extending the package,但我似乎无法弄清楚如何在导出数据时向列添加下拉列表。
这是我导出的简化版本class:
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 选项的查找 table 并将这些值用于列中的下拉列表,以便当用户想要更改 excel sheet,它们仅限于下拉值。
在文档中它提到使用 \Maatwebsite\Excel\Sheet
或 \Maatwebsite\Excel\Writer
,但我什至不确定在哪里使用它们,或者使用哪个。
在我的整个搜索过程中,我似乎无法拼凑出一个解决方案,因此我们将不胜感激。
我正在使用:
maatwebsite/excel 3.1, php7.2, laravel 5.8
sheet 事件的实现可能相当混乱,很难找到示例,所以当我看到这样的 post 时,我会尽力提供帮助。首先,我会说你真的应该看看 PHPSpreadsheet documentation for these additional features. It's where you're going to find the important information you need. Then you can translate what you find for use in 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);
}
},
];
}
}