Seh*_*ael 3 php excel laravel phpspreadsheet
我一直在使用Laravel Excel以 csv 格式导出数据,到目前为止效果非常好。现在我需要以 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;
}
}
Run Code Online (Sandbox Code Playgroud)
我想做的是查询一个包含column1选项的查找表,并将这些值用于该列中的下拉列表,这样当用户想要更改Excel工作表时,它们仅限于下拉值。
在文档中它提到使用\Maatwebsite\Excel\Sheetor \Maatwebsite\Excel\Writer,但我什至不确定在哪里使用它们,或者使用哪一个。
在我的搜索过程中,我似乎无法拼凑出解决方案,因此任何帮助将不胜感激。
我在用着:
maatwebsite/excel 3.1、php 7.2、laravel 5.8
mat*_*ard 11
工作表事件的实现可能相当令人困惑,并且很难找到示例,因此当我看到这样的帖子时,我会尝试提供帮助。首先,我会说您确实应该查看PHPSpreadsheet 文档以了解这些附加功能。您可以在这里找到所需的重要信息。然后您可以翻译您找到的内容以在Laravel Excel中使用。
\n\nPHPSpreadsheet:在单元格上设置数据验证\n https://phpspreadsheet.readthedocs.io/en/latest/topics/recipes/#setting-data-validation-on-a-cell
\n\n这是一个基于现有文件的示例。我还添加了一些额外的格式来自动调整列宽度 \xe2\x80\x94 在我看来这是必须的。
\n\nnamespace App\\Exports;\n\nuse Maatwebsite\\Excel\\Concerns\\FromCollection;\nuse Maatwebsite\\Excel\\Concerns\\WithHeadings;\nuse Maatwebsite\\Excel\\Concerns\\WithStrictNullComparison;\nuse Maatwebsite\\Excel\\Concerns\\WithEvents;\nuse Maatwebsite\\Excel\\Events\\AfterSheet;\nuse PhpOffice\\PhpSpreadsheet\\Cell\\Coordinate;\nuse PhpOffice\\PhpSpreadsheet\\Cell\\DataValidation;\n\nclass ActionItemExport implements FromCollection, WithHeadings, WithEvents, WithStrictNullComparison\n{\n protected $results;\n\n public function collection()\n {\n // store the results for later use\n $this->results = $this->getActionItems();\n\n return $this->results;\n }\n\n // ...\n\n public function registerEvents(): array\n {\n return [\n // handle by a closure.\n AfterSheet::class => function(AfterSheet $event) {\n\n // get layout counts (add 1 to rows for heading row)\n $row_count = $this->results->count() + 1;\n $column_count = count($this->results[0]->toArray());\n\n // set dropdown column\n $drop_column = \'A\';\n\n // set dropdown options\n $options = [\n \'option 1\',\n \'option 2\',\n \'option 3\',\n ];\n\n // set dropdown list for first data row\n $validation = $event->sheet->getCell("{$drop_column}2")->getDataValidation();\n $validation->setType(DataValidation::TYPE_LIST );\n $validation->setErrorStyle(DataValidation::STYLE_INFORMATION );\n $validation->setAllowBlank(false);\n $validation->setShowInputMessage(true);\n $validation->setShowErrorMessage(true);\n $validation->setShowDropDown(true);\n $validation->setErrorTitle(\'Input error\');\n $validation->setError(\'Value is not in list.\');\n $validation->setPromptTitle(\'Pick from list\');\n $validation->setPrompt(\'Please pick a value from the drop-down list.\');\n $validation->setFormula1(sprintf(\'"%s"\',implode(\',\',$options)));\n\n // clone validation to remaining rows\n for ($i = 3; $i <= $row_count; $i++) {\n $event->sheet->getCell("{$drop_column}{$i}")->setDataValidation(clone $validation);\n }\n\n // set columns to autosize\n for ($i = 1; $i <= $column_count; $i++) {\n $column = Coordinate::stringFromColumnIndex($i);\n $event->sheet->getColumnDimension($column)->setAutoSize(true);\n }\n },\n ];\n }\n}\nRun Code Online (Sandbox Code Playgroud)\n
| 归档时间: |
|
| 查看次数: |
10306 次 |
| 最近记录: |