douwen5833 2015-12-13 19:08
浏览 111
已采纳

如何通过第1行phpexcel中的数据查找正确的列

I have excel file with some certain data i need.

"Category" is in first row of excel file.

Thing is that this data can be in different columns.

Is it possible to get column name based on data in first row ?

Example sheet:

enter image description here

For example, i need to get aircraft type and category, wich in this case are columns I & H.

At the moment i do it like this:

$allDataInSheet = $objPHPExcel->getActiveSheet()->toArray(null,true,true,true);
$arrayCount = count($allDataInSheet);
for($i=2;$i<=$arrayCount;$i++){
   $aircraftType = trim($allDataInSheet[$i]["I"]);
   $flightCategory = trim($allDataInSheet[$i]["H"]);

   #insert all data in these columns to database
   $query = "INSERT INTO {$databasetable} (aircraftType, flightCategory) 
      VALUES('$aircarftType', '$flightCategory')";  
   mysql_query($query) or trigger_error(mysql_error()." in ".$query);
}

But how can i do it, when i dont know column letter? Maybe in next file aircraftType is in K and flightCategory is in L...

So is there a way to get column letter based on data in first row.

I think i could propably do it if i save all data to database, and then take it out with some where clauses, but maybe there is better way of doing this directly with PHPExcel ?

I am not really good explaining things, so sorry if this is confusing, please ask and i try to explain more.

Thanks in advance for help.

  • 写回答

1条回答 默认 最新

  • dth96108 2015-12-13 19:17
    关注

    A simple call using the worksheet's rangeToArray() will give you an array of the headers

    $headers = $objPHPExcel->getActiveSheet()
        ->rangeToArray(
            'A1:'.$objPHPExcel->getActiveSheet()->getHighestColumn().'1',
            null,
            false,
            false,
            true
        );
    $headers = $headers[1];
    

    The resultant array has the column letter as the key, and the cell content as the value; and you can then use it to map the appropriate columns to your data

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥40 复杂的限制性的商函数处理
  • ¥15 程序不包含适用于入口点的静态Main方法
  • ¥15 素材场景中光线烘焙后灯光失效
  • ¥15 请教一下各位,为什么我这个没有实现模拟点击
  • ¥15 执行 virtuoso 命令后,界面没有,cadence 启动不起来
  • ¥50 comfyui下连接animatediff节点生成视频质量非常差的原因
  • ¥20 有关区间dp的问题求解
  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码