PHPexcel自动判断excel类型并读取excel所有sheet内容
<pre> include "PHPExcel.php";
/*
* 读取excel表数据
*/
public function readExcel($filename){
$pathinfo = pathinfo(strtolower($filename));
if($pathinfo['extension'] == 'xlsx'){
$PHPReader = new PHPExcel_Reader_Excel2007();
//if(!$PHPReader->canRead($filePath)){
}elseif($pathinfo['extension'] == 'xls'){
$PHPReader = new PHPExcel_Reader_Excel5();
}else{
return 'not support file type';
}
$PHPExcel = $PHPReader->load($filename);
// echo is_object($PHPExcel).'mmm';exit; //查看是否初始化成功
//获取工作表的数目
$sheetCount = $PHPExcel->getSheetCount();
for($i = 0; $i < $sheetCount; $i++){
/**读取excel文件中的第一个工作表*/
$currentSheet = $PHPExcel->getSheet($i);
/**取得最大的列号*/
$allColumn = $currentSheet->getHighestColumn();
/**取得一共有多少行*/
$allRow = $currentSheet->getHighestRow();
/**从第二行开始输出,因为excel表中第一行为列名*/
for($currentRow = 1; $currentRow <= $allRow; $currentRow++){
/**从第A列开始输出*/
$row = [];
for($currentColumn= 'A'; $currentColumn <= $allColumn; $currentColumn++){
/**ord()将字符转为十进制数*/
$val = $currentSheet->getCellByColumnAndRow(ord($currentColumn) - 65,$currentRow)->getValue();
/**如果输出汉字有乱码,则需将输出内容用iconv函数进行编码转换,如下将gb2312编码转为utf-8编码输出*/
//$val = $currentSheet->getCell($currentColumn.$currentRow)->getValue();
if($val instanceof PHPExcel_RichText){//富文本转换字符串
$val = $val->__toString();
}
$row[] = $val;
}
//echo "</br>";
if(!empty($row)) $sheetdata[] = $row;
unset($row);
}
$data['sheet'.$i] = $sheetdata;
$currentSheet = $sheetdata = null;
}
return $data;
}
打印结果$data:
Array
(
=> Array
(
=> Array
(
=> id
=> uid
=> 昵称
=> 状态
=> 原因
=> ip
=> 手机
=> 时间
)
=> Array
(
=> 592
=> 25
=> 0000null
=> 正常
=> 没有原因的原因
=> 195.168.1.5
=> 13800138000
=> 2016-11-23 10:11:49
)
)
=> Array
(
=> Array
(
=> 列1
=> 列2
=> 列3
)
=> Array
(
=> 111值1
=> 222佱2
=> 333值3
)
)
)
青云大叔出品,必属精品。</pre>
页:
[1]