3

我想阅读在 Excel 表的下拉列表中给出的选项列表。

例如: - 有一个单元格(86,G)未尝试作为选项,但它是一个下拉列表。因此,无论用户选择什么,我都想从下拉列表中读取最后一个选项。

在此处输入图像描述

如何做到这一点?

谢谢

4

2 回答 2

1
    $bankAccounts = "bankAccounts, bankAccounts2";
    $products = "product1, product2";         

     \Excel::create('file', function ($excel) use ($bankAccounts, $products) {
        $excel->sheet('Sale Forecasting', function ($sheet) use ($bankAccounts, $products) {
            $sheet->SetCellValue('A1', 'name');
            $sheet->SetCellValue('A2', 'testing');
            $sheet->SetCellValue('B1', 'valid_from');
            $sheet->SetCellValue('B2', '2020-01');
            $sheet->SetCellValue('C1', 'valid_till');
            $sheet->SetCellValue('C2', '2020-02');
            $sheet->SetCellValue('D1', 'amount');
            $sheet->SetCellValue('D2', '1000');
            $sheet->SetCellValue('E1', 'bank_account');
            $sheet->SetCellValue('F1', 'product');
            $sheet->SetCellValue('G1', 'description');
            $sheet->SetCellValue('G2', 'Lorem ipsum');
            $col_count = 1000;
            for ($i = 2; $i <= $col_count; $i++) {
                $objValidation = $sheet->getCell('E'.$i)->getDataValidation();
                $objValidation->setType(\PHPExcel_Cell_DataValidation::TYPE_LIST);
                $objValidation->setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION);
                $objValidation->setAllowBlank(false);
                $objValidation->setShowInputMessage(true);
                $objValidation->setShowErrorMessage(true);
                $objValidation->setShowDropDown(true);
                $objValidation->setErrorTitle('Input error');
                $objValidation->setError('Value is not in list.');
                $objValidation->setPromptTitle('Pick from list');
                $objValidation->setPrompt('Please pick a value from the drop-down list.');
                $objValidation->setFormula1('"'.$bankAccounts.'"');
            }

            $col_count = 1000;
            for ($i = 2; $i <= $col_count; $i++) {
                $objValidation = $sheet->getCell('F'.$i)->getDataValidation();
                $objValidation->setType(\PHPExcel_Cell_DataValidation::TYPE_LIST);
                $objValidation->setErrorStyle(\PHPExcel_Cell_DataValidation::STYLE_INFORMATION);
                $objValidation->setAllowBlank(false);
                $objValidation->setShowInputMessage(true);
                $objValidation->setShowErrorMessage(true);
                $objValidation->setShowDropDown(true);
                $objValidation->setErrorTitle('Input error');
                $objValidation->setError('Value is not in list.');
                $objValidation->setPromptTitle('Pick from list');
                $objValidation->setPrompt('Please pick a value from the drop-down list.');
                $objValidation->setFormula1('"'.$products.'"');
            }
        });
    })->download('xlsx');
于 2020-03-06T11:31:01.143 回答
1

按照以下步骤阅读 PHPExcel 中的下拉菜单

第 1 步:将下拉数据存储在单独的工作表中

第 2 步:在单独的工作表中创建下拉菜单

请参考以下代码。

$newSheet=$objPHPExcel->createSheet();            
$objPHPExcel->setActiveSheetIndex(1);
$newSheet->setTitle("CountriesList");

$objPHPExcel->setActiveSheetIndex(1)
->SetCellValue("A1", "UK")
->SetCellValue("A2", "USA")
->SetCellValue("A3", "CANADA")
->SetCellValue("A4", "INDIA")
->SetCellValue("A5", "POLAND")
->SetCellValue("A6", "ENGLAND");// Drop down data in sheet 1

$objPHPExcel->addNamedRange( 
new PHPExcel_NamedRange(
'countries', 
$objPHPExcel->setActiveSheetIndex(1), 
'A1:A6'
) 
);
$objPHPExcel->setActiveSheetIndex(0)->SetCellValue("A1", "UK");

$objPHPExcel->setActiveSheetIndex(0);// Drop down in sheet 0
$objValidation = $objPHPExcel->getSheet(0)->getCell('A1')->getDataValidation();
$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_LIST );
$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_INFORMATION );
$objValidation->setAllowBlank(false);
$objValidation->setShowInputMessage(true);
$objValidation->setShowErrorMessage(true);
$objValidation->setShowDropDown(true);
$objValidation->setErrorTitle('Input error');
$objValidation->setError('Value is not in list.');
$objValidation->setFormula1("=countries");

在创建 xls 时应遵循这些步骤。

当您要阅读下拉菜单时,请使用存储在第二张表中的数据而不是下拉菜单。

于 2015-09-07T12:43:12.417 回答