if(empty(STATIC_URL.$path) or !file_exists($path)){die('file not exists');}
$PHPReader = new \PHPExcel_Reader_Excel2007(); //建立reader对象
if(!$PHPReader->canRead($path)){
$PHPReader = new \PHPExcel_Reader_Excel5();
if(!$PHPReader->canRead(.$path)){
echo 'no Excel';
return ;
}
}
$sheet=0;
$objRead = new \PHPExcel_Reader_Excel2007(); //建立 reader 对象
if (!$objRead -> canRead($file)) {
$objRead = new \PHPExcel_Reader_Excel5();
if (!$objRead -> canRead($file)) {
die('No Excel!');
}
}
$cellName = ['A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z', 'AA', 'AB', 'AC', 'AD', 'AE', 'AF', 'AG', 'AH', 'AI', 'AJ', 'AK', 'AL', 'AM', 'AN', 'AO', 'AP', 'AQ', 'AR', 'AS', 'AT', 'AU', 'AV', 'AW', 'AX', 'AY', 'AZ'];
$obj = $objRead -> load($file); //建立 excel 对象
$currSheet = $obj -> getSheet($sheet); //获取指定的 sheet 表
//$columnH = $currSheet -> getHighestColumn(); //取得最大的列号 获取最大列的时候可能获取到无限大,有两种思路一种就是
//导入的时候有多少列,就写成多少列,一种就是判断列有没有值,没有值的时候不处理
$columnH = 'AE';
$columnCnt = array_search($columnH, $cellName);
$rowCnt = $currSheet -> getHighestRow(); //获取总行数117
for ($_row = 2; $_row <= $rowCnt; $_row++) { //读取内容
for ($_column = 0; $_column <= $columnCnt; $_column++) {
$cellId = $cellName[$_column] . $_row;
$cellValue = $currSheet -> getCell($cellId) -> getValue();
//$cellValue = $currSheet->getCell($cellId)->getCalculatedValue(); #获取公式计算的值
if ($cellValue instanceof \PHPExcel_RichText) { //富文本转换字符串
$cellValue = $cellValue -> __toString();
}
if ($cellValue != null) {
$data[$_row][$cellName[$_column]] = $cellValue;
} else {
continue;
}
}
}
数据导入 处理获取最大列问题
最新推荐文章于 2023-07-02 13:35:19 发布