我正在使用PHPExcel_IOFactory上传excel,在excel中有一些列应该强制填写,如B,D,I,J等。
我的需要是首先更改B列,如果它是空的,就会发生错误,否则检查D列,如果是空的错误,则检查其他列。
我已经写了下面的代码,但我不需要一次检查一列:
$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet);
for($i=2;$i<=$arrayCount;$i++){
$b = $allDataInSheet[$i]["B"];
$d = $allDataInSheet[$i]["D"];
$i = $allDataInSheet[$i]["I"];
}几乎有20列是必须的,我如何一次检查每一列。
发布于 2016-07-27 12:57:13
注意。您在for($i=2...行中使用了$i,但是在使用$i = $allData...的循环中将其设置为其他值。
为了满足你的要求(包括从第二行开始),我会这样做
$firstK = key($allDataInSheet); //save the first key
foreach($allDataInSheet as $k => $row){
if($k===$firstK) continue; //skip the first row
if(empty($b = $row['B'])){
// Error. column B is empty
}elseif(empty($c = $row['C'])){
// B is not empty. $b has its contents
// however, Error: column C is empty
}elseif(empty($i = $row['I'])){
// B and C are not empty. $b and $c have their contents
// however, Error: column I is empty
...
}else{
// All required columns have data
// $b, $c ... $i ... hold the contents
}
//...continue processing the row. You can use variables $b, $c...
}https://stackoverflow.com/questions/38603852
复制相似问题