检查上传的excel文件中的列数

我正在创build一个网页,您必须上传一个Excel文件(csv,xls或xlsx)。 然后我必须检查文件中的列数,因为can只能包含两列。 我已经find了如何做到这一点,但仅限于csv文件:

if (($file = fopen($path, "r")) !== FALSE){ while ($line = fgetcsv($file)){ $numcols = count($line); if ($numcols != 2) { echo "The number of columns in your file is not correct. The file must contain only 2 columns. "; break; } $col = $line[0]; echo "right! numcols = " . $numcols . "<br>"; break; } fclose($file); } 

现在,我的问题是:是否有可能使用类似的function,可以为所有3种文件格式?

提前致谢

绝对使用一个可以读取不同格式的库。 正如Mark Ba​​ker所指出的, PHPExcel对于Excel格式来说是一个不错的select。

就像是:

 $colCount = checkExcelFile($path) function checkExcelFile($path){ $workbook = PHPExcel_IOFactory::load($path); // $column should then contain the highest column that is being used. // It will be in the form A or B or AA etc. but if you are only expecting 2 columns, // then you would be expected 'B', so that would need to be converted to a number $column = $workbook->getActiveSheet()->getHighestDataColumn(); $colNumber = PHPExcel_Cell::columnIndexFromString($column); return $colNumber; }