class ExcelRead { /** * 获取Excel文件内容 * @param $file * @return mixed * @throws PHPExcel_Reader_Exception * @author Starfish * @date 2020-06-09 11:29 */ public function getExcelFileData($file) { $file_types = explode ( ".", $file['name'] ); $file_type = strtolower(end($file_types)); $excel_type = array('xls','csv','xlsx'); if (!in_array($file_type,$excel_type)){ echo 'file type error';exit; } $inputFileType = PHPExcel_IOFactory::identify($file['tmp_name']); if($file_type == 'csv') { $objReader = PHPExcel_IOFactory::createReader($inputFileType) ->setDelimiter(',') ->setInputEncoding('GBK') //新版本可删除 ->setSheetIndex(0); }else { $objReader = PHPExcel_IOFactory::createReader($inputFileType); } $objPHPExcel = $objReader->load($file['tmp_name']); $res = $objPHPExcel->getActiveSheet()->toArray(); foreach ($res as $k => $v) { // 空值处理 if(!$v['0']) { unset($res[$k]); } } return $res; } }