如果未填充,则Excel Excel Reader 2读取无法读取列

时间:2012-10-24 12:01:10

标签: php excel

我有一个像这样的excel文件:Excel example

当我这样做时,我想阅读F,G,H,J列:

              for ($i = 3; $i <= $data->sheets[0]['numRows']; $i++) {
                error_log(
                    FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][6]).' - '.
                    FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][7]).' - '.
                    FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][8]).' - '.
                    FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][9]));
                $count++;
                }

我遇到的问题是我收到了错误:

  

PHP注意:未定义的偏移量:7 PHP注意:未定义的偏移量:8   PHP注意:未定义的偏移量:9

F列总是满的,但正如你所看到的那样,我遇到了其他问题。如何避免此错误,因此仅在字段填充数据时才会检查字段?

2 个答案:

答案 0 :(得分:1)

  for ($i = 3; $i <= $data->sheets[0]['numRows']; $i++) {
    $err=array;
    for($j=6; $j<=9; $j++){
      if(isset(FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][$j])))
         $err[]=FW_Class_Voucher_Code::cleanCode($data->sheets[0]['cells'][$i][$j]) ;
    }
    if(!empty($err){
      $msg=implode('-',$err);
      error_log($msg);
    } 
    $count++; //I don't know what you're using $count for
  } 

答案 1 :(得分:0)

    if (isset($data->sheets[0]['cells'][$i][7])) {
                        $col7 = $data->sheets[0]['cells'][$i][7];
                    } else {
                        $col7 = '';
                    }
                    if (isset($data->sheets[0]['cells'][$i][8])) {
                        $col8 = $data->sheets[0]['cells'][$i][8];
                    } else {
                        $col8 = '';
                    }
                    if (isset($data->sheets[0]['cells'][$i][9])) {
                        $col9 = $data->sheets[0]['cells'][$i][9];
                    } else {
                        $col9 = '';
                    }

看起来很丑,但确实有效。