我有一个包含两张表的 Excel 文件:
-
工作表;
-
List- 将显示的项目列表
Worksheet
工作表作为列表项。
请看下面的图片:
我想使用生成这个PHPExcel
图书馆。我已经尝试过,但没有得到预期的结果。请参阅我的下面的代码:
$objPHPExcel = new PHPExcel();
// Set document properties
$objPHPExcel->getProperties()->setCreator("Soumya Biswas")
->setLastModifiedBy("Soumya Biswas")
->setTitle("Office 2007 XLSX Test Document")
->setSubject("Office 2007 XLSX Test Document")
->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.")
->setKeywords("office 2007 openxml php")
->setCategory("Test result file");
// Create a first sheet
$objPHPExcel->setActiveSheetIndex(0);
$objPHPExcel->getActiveSheet()->setCellValue('A5', "List");
// Set data validation
$objValidation = $objPHPExcel->getActiveSheet()->getCell('B5')->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('"$List.$A$1:$A$10"'); // Make sure to put the list items between " and " !!!
$objPHPExcel->createSheet();
$objPHPExcel->setActiveSheetIndex(1);
$objPHPExcel->getActiveSheet()->setTitle('List');
for ($i = 1; $i <= 10; $i++) {
$objPHPExcel->getActiveSheet()->setCellValue("A{$i}", "List Item {$i}");
}
// Set active sheet index to the first sheet, so Excel opens this as the first sheet
$objPHPExcel->setActiveSheetIndex(0);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="data-validation.xls"');
header('Cache-Control: max-age=0');
$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
$objWriter->save('php://output');
exit;
我认为引用另一张工作表中的单元格范围的正确语法是:
List!$A$1:$A$10
所以你应该尝试:
$objValidation->setFormula1('List!$A$1:$A$10'); // tested it, worked for me
得到了这个想法http://phpexcel.codeplex.com/discussions/320393 http://phpexcel.codeplex.com/discussions/320393:
->setFormula1("Worksheet!A1:{$endCell}1");// 工作....
尽管这个人在使用命名范围时还有另一个问题。
背景:我认为:
$objValidation->setFormula1('"$List.$A$1:$A$10"');
您明确使用引号之间的给定字符串作为列表值,如下所述:here http://phpexcel.codeplex.com/discussions/441114(你可能首先得到这个片段)或者here http://phpexcel.codeplex.com/discussions/447270。
但由于您不想使用固定列表项而是动态引用的列表项,因此您应该省略双引号。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)