PHPExcel data validation of numeric character

半城伤御伤魂 提交于 2020-01-06 14:12:39

问题


I tried the code on this page (codeplex phpexcel validation issue for invalid values) to validate input of numeric characters only. I also searched on the PHPExcel developer documentation and similar code appears. But when I applied it with my code and tested it, the cell does not accept even numeric characters.. may you suggest other methods on filtering or validating data input to numeric characters only.

The code below is from the PHPExcel developer documentation:

$objValidation = $objPHPExcel->getActiveSheet()->getCell('B3')
->getDataValidation();

$objValidation->setType( PHPExcel_Cell_DataValidation::TYPE_WHOLE );

$objValidation->setErrorStyle( PHPExcel_Cell_DataValidation::STYLE_STOP );

$objValidation->setAllowBlank(true);

$objValidation->setShowInputMessage(true);

$objValidation->setShowErrorMessage(true);

$objValidation->setErrorTitle('Input error');

$objValidation->setError('Number is not allowed!');

$objValidation->setPromptTitle('Allowed input');

$objValidation->setPrompt('Only numbers between 10 and 20 are allowed.');

$objValidation->setFormula1(10);

$objValidation->setFormula2(20);

回答1:


I was having the very same issue and finally determined by trial & error that it was due to the cell format being set to text and not number that was causing the problem. Adding

$objPHPExcel->getActiveSheet()->getStyle('B3')->getNumberFormat()->setFormatCode('#');

just prior to your $objValidation will solve the problem. Of course you'll need to set the format to what ever applies to your situation. In this case I just need a whole number.




回答2:


Your code only needs to set the operation to perform; Also do not forget to create the Writer with Excel2007 not with Excel5.

Something like this:

$objValidation = $phpexcel->getActiveSheet()->getCell('B3')
->getDataValidation();
$objValidation->setType( \PHPExcel_Cell_DataValidation::TYPE_DECIMAL ); //'or whole'

//This Line 
$objValidation->setOperator(\PHPExcel_Cell_DataValidation::OPERATOR_BETWEEN);

$objValidation->setAllowBlank(true);
$objValidation->setShowInputMessage(true);
$objValidation->setShowErrorMessage(true);
$objValidation->setErrorStyle( \PHPExcel_Cell_DataValidation::STYLE_STOP ); //'stop'
$objValidation->setErrorTitle('Ingrese solo números');
$objValidation->setError('Ingrese solo números');
$objValidation->setPromptTitle('Ingrese solo números');
$objValidation->setPrompt('Ingrese solo números');
$objValidation->setFormula1(0);
$objValidation->setFormula2(999999);
$objValidation->setAllowBlank(true);
$phpexcel->getActiveSheet()->getStyle('B3')->getNumberFormat()->setFormatCode('#');


来源:https://stackoverflow.com/questions/21397882/phpexcel-data-validation-of-numeric-character

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!