php导入excel一般选用php excel,php excel下载地址:http://phpexcel.codeplex.com
实际调用:
function getExcelData($filename) { $objReader = PHPExcel_IOFactory::createReader('Excel5'); $objReader->setReadDataOnly(true); $objPHPExcel = $objReader->load($filename); $objWorksheet = $objPHPExcel->getActiveSheet(); $highestRow = $objWorksheet->getHighestRow(); $highestColumn = $objWorksheet->getHighestColumn(); $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); if($highestRow==0) return; for($row = 1; $row <= $highestRow; $row++) { for($col = 0; $col < $highestColumnIndex; $col++) { $excelData[$row][] = (string)$objWorksheet->getCellByColumnAndRow($col, $row)->getValue(); } } return $excelData; }
结果发现有些excel文件上传读取失败,直接服务器500。检查看了下,原来php excel在不同的版本,实例化类是不一样的。
2003以前的excel读取实例化excel5类,2003以后需要实例化excel2007类,so,改改$objReader实例化的地方:
$objReader = PHPExcel_IOFactory::createReader('Excel2007'); if(!$objReader->canRead($filename)) { $objReader = PHPExcel_IOFactory::createReader('Excel5'); if(!$objReader->canRead($filename)) return; }
小记,以防他日忘记。
原创文章,作者:ItWorker,如若转载,请注明出处:https://blog.ytso.com/98483.html