[英]PHPExcel : Data validation not working in .xls format
我有兩張紙的Excel文件:
Worksheet
表中顯示為列表項的項目列表。 請看下面的圖片:
我想使用PHPExcel
庫生成此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獲得了這個想法:
-> setFormula1(“ Worksheet!A1:{$ endCell} 1”); //工作。
盡管此人在使用命名范圍時遇到了另一個問題。
背景:我認為:
$objValidation->setFormula1('"$List.$A$1:$A$10"');
您可以明確地使用引號之間的給定字符串作為列表值,如此處所述: 此處 (您可能首先在其中獲得此代碼段)或此處 。 但是,由於您不想使用固定列表項而是動態引用的項,因此應省略雙引號。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.