Laravel excel dropdowns overriding to last one

35 Views Asked by At

foreach loop values override to last one. below is my code, please give me suggestions it's save my day.

    $spreadsheet = new Spreadsheet();
            $worksheetNames = $spreadsheet->getActiveSheet();
            $worksheetNames->setTitle('Field Names');
            $row = 1; // Start from row 1
            $column = 'A';
            
            foreach ($fieldData as $label) {
                    $checkFieldFunction = $label->Field_Function;
                
                    if ($checkFieldFunction == 4) {
                        $table = $label->From_Table;
                        $dropdownValues = DB::table($table)->get()->toArray();
                 $parse   = new DataValidation();
                 $drop_phase1 =  $column . ($row + 1);
                
                $dropdownOptions = array();
                
                foreach ($dropdownValues as $value) {
                     if ($label->From_Table == "Customers") {
                    $field_name = strtolower($label->{'Display Field'});
                            $fieldValue = property_exists($value, $field_name) ? $value->$field_name : null;
                            $dropdownOptions[] = $fieldValue;
                        } else {
                            $field_name = $label->{'Display Field'};
                            $fieldValue = property_exists($value, $field_name) ? $value->$field_name : null;
                            $dropdownOptions[] = $fieldValue;
                        }
                    }
                $validation=$worksheetNames->getCell($drop_phase1)->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(',', $dropdownOptions)));
                $worksheetNames->getCell($drop_phase1)->setDataValidation(clone $validation);
                $worksheetNames->setCellValue($column . $row, $label->fld_label);
                $worksheetNames->setCellValue($column . ($row + 1), $label->Field_Name);
                $column++;
            }
        }  
         $fileName = $tableSelect . '.xlsx';
            $filePath = storage_path('app/' . $fileName);
        
            $writer->save($filePath);
        
            $headers = [
                'Content-Type' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
                'Content-Disposition' => 'attachment; filename="' . $fileName . '"',
            ];
        
            return response()->download($filePath, $fileName, $headers);

enter image description here `` [here dropdown list override to last one of loop](https://i.stack.imgur.com/9GqN7.png)

I want dropdown need not to override, when am change to dynamic column for cell it gives excel error.

0

There are 0 best solutions below