//读取xlsx文件并以第一排为上标显示 function read_xlsx($file_name) { vendor("PHPExcel.PHPExcel"); vendor("PHPExcel.PHPExcel.Writer.IWriter"); vendor("PHPExcel.PHPExcel.Writer.Abstract"); vendor("PHPExcel.PHPExcel.Writer.Excel5"); vendor("PHPExcel.PHPExcel.Writer.Excel2007"); vendor("PHPExcel.PHPExcel.IOFactory"); vendor("PHPExcel.PHPExcel.Cell"); $inputFileType = \PHPExcel_IOFactory::identify($file_name); if ($inputFileType !== "Excel5" && $inputFileType !== "Excel2007" ) { unlink($filePath) && str_alert(-1,"请确保导入的文件格式正确!"); } $objReader = \PHPExcel_IOFactory::createReader($inputFileType); $PHPExcel = $objReader->load($file_name); /**读取excel文件中的第一个工作表*/ $currentSheet = $PHPExcel->getSheet(0); /**取得最大的列号*/ $allColumn = $currentSheet->getHighestColumn(); /**取得一共有多少行*/ $allRow = $currentSheet->getHighestRow(); $dd=array(); $key=array(); //循环读取每个单元格的内容。注意行从1开始,列从A开始 for($rowIndex=1;$rowIndex<=$allRow;$rowIndex++){ $d=array(); for($colIndex='A';$colIndex<=$allColumn;$colIndex++){ $addr = $colIndex.$rowIndex; $cell = $currentSheet->getCell($addr)->getValue(); if($cell instanceof PHPExcel_RichText) //富文本转换字符串 $cell = $cell->__toString(); if($rowIndex==1) { $key[$colIndex]=$cell; }else{ if($key[$colIndex]) { $d[$key[$colIndex]]=$cell; } } } if(count($d)>0) { $dd[]=$d; } } return $dd; }