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

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

报告相同问题?

悬赏问题

  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 关于大棚监测的pcb板设计
  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题