doumeba0486 2018-07-07 14:57
浏览 107

创建Excel动态选项

I am creating an excel with PhpSpreadsheet that contains 2 sheets. The first is the selection and insertion of data, the second is a list of options that feed combos of the first page, until now everything is working very well, but I am presented with a case and I do not know if it is possible to do it.

In my database I have a table called Base_type and it contains Id and Name

I also have a table called Type_Base_List and it contains Id, Id_Type_Base and Id_List

And finally, I have the Model table that contains Id, Name and Id_Type_Base

With this I get that when the user selects a specific model I can get with that id to the lists that have associated (the model has base_type_id, and in the table base_list_type I have all the list_id associated with base_type_id)

This logic works very well in my application but I do not know if it is possible to apply that logic in a generated excel. I mean, when the person goes to cell A1 (which is the model) once you select a value, in B1, all the LISTS associated with that model should be listed (with the logic explained above)

To generate the options in the excel:

//1 SHEET
$spreadsheet = new Spreadsheet();
$sheet = $spreadsheet->getActiveSheet(0);
$sheet->setTitle('Actividades');

$sheet->getColumnDimension('A')->setWidth(15);
$sheet->getColumnDimension('B')->setWidth(30);

$sheet->setCellValue('A1', 'Model');
$sheet->setCellValue('B1', 'List');

//2 sheet
$spreadsheet->createSheet(1);
$spreadsheet->setActiveSheetIndex(1);
$sheet = $spreadsheet->getActiveSheet();
$sheet->setTitle('Combos');

//Models
$sheet->mergeCells('A1:B1');
$sheet->setCellValue('A1', "Models");
$i = 2;

foreach ($models as &$model) {
    $sheet->setCellValue('A'.$i, $model['name']);
    $sheet->setCellValue('B'.$i, $model['id']);
    $i++;
}

$sheet->mergeCells('C1:D1');
$sheet->setCellValue('C1', "Lists");
$i = 2;

foreach ($lists as &$list) {
    $sheet->setCellValue('C'.$i, $list['name']);
    $sheet->setCellValue('D'.$i, $list['id']);
    $i++;
}

$spreadsheet->setActiveSheetIndex(0);
$sheet = $spreadsheet->getActiveSheet();

$i = 2;
while ($i <= $minimoListas + 2) {

   //Options Models
   $objValidation = $sheet->getCell("A".$i)->getDataValidation();
   $objValidation->setType( \PhpOffice\PhpSpreadsheet\Cell\DataValidation::TYPE_LIST);
   $objValidation->setErrorStyle( \PhpOffice\PhpSpreadsheet\Cell\DataValidation::STYLE_INFORMATION );
   $objValidation->setAllowBlank(false);
   $objValidation->setShowInputMessage(true);
   $objValidation->setShowErrorMessage(true);
   $objValidation->setShowDropDown(true);
   $objValidation->setErrorTitle('error entrada');
   $objValidation->setError('Valor no es válido');
   $objValidation->setPromptTitle('Select Model');
   $objValidation->setFormula1('Combos!$A$2:$A$'.$countUnidades);

   //Options List
   $objValidation = $sheet->getCell("B".$i)->getDataValidation();
   $objValidation->setType( \PhpOffice\PhpSpreadsheet\Cell\DataValidation::TYPE_LIST);
   $objValidation->setErrorStyle( \PhpOffice\PhpSpreadsheet\Cell\DataValidation::STYLE_INFORMATION );
   $objValidation->setAllowBlank(false);
   $objValidation->setShowInputMessage(true);
   $objValidation->setShowErrorMessage(true);
   $objValidation->setShowDropDown(true);
   $objValidation->setErrorTitle('error entrada');
   $objValidation->setError('Valor no es válido');
   $objValidation->setPromptTitle('Select List');
   $objValidation->setFormula1('Combos!$C$2:$C$'.$countListas);


   $i++;
} 
  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 BP神经网络控制倒立摆
    • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
    • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
    • ¥30 Unity接入微信SDK 无法开启摄像头
    • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
    • ¥20 cad图纸,chx-3六轴码垛机器人
    • ¥15 移动摄像头专网需要解vlan
    • ¥20 access多表提取相同字段数据并合并
    • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
    • ¥20 Java-Oj-桌布的计算