1、先去 https://github.com/PHPOffice/PHPExcel 下载 PHPExcel ;
2、如果是命名空间自动加载,$objPHPExcel = \PHPExcel_IOFactory::load($uploadfile); 记得在加“\” 反斜杠;
3、案例:
/**
* 读取excel $filename 路径文件名 $encode 返回数据的编码 默认为utf8
*以下基本都不要修改
*/
public function readExcel($uploadfile)
{
vendor("PHPExcel.Classes.PHPExcel",'vendor/','.php');
vendor("PHPExcel.Classes.PHPExcel.IOFactory",'vendor/','.php');
vendor("PHPExcel.Classes.PHPExcel.Reader.Excel2007",'vendor/','.php');
$objReader = \PHPExcel_IOFactory::createReader('Excel2007');//use excel2003 和 2007 format Excel5 use excel2003
$objReader->setReadDataOnly(true);
//$objPHPExcel = $objReader->load($uploadfile); //Excel 路径 //这个容易造成httpd崩溃
$objPHPExcel = \PHPExcel_IOFactory::load($uploadfile);//改成这个写法就好了
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // 取得总行数
$highestColumn = $objWorksheet->getHighestColumn();// 取得总列数
$highestColumnIndex = \PHPExcel_Cell::columnIndexFromString($highestColumn);//总列数
$excelData = [];
for ($row = 1;$row <= $highestRow;$row++) {
$strs=array();
//注意highestColumnIndex的列数索引从0开始
for ($col = 0;$col < $highestColumnIndex;$col++) {
$strs[$col] =$objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
if(!empty($strs)){
$excelData[] = $strs;
unset($strs);
}
}
// p($excelData);
// die();
return $excelData;
}