douruye5092 2012-10-12 04:49 采纳率: 100%
浏览 33
已采纳

SQL使用PHPExcel插入从excel读取的数据

I am currently working on reading a excel using php and storing those records in mysql.I came across PHPExcel,a bunch of very nice plugin classes which can very easily help to achive it.I tried to search through but did not something similar to my use case.Also,not very good at object oriented PHP and I am short of time in doing this.

    First Name  Last Name   Nationality Gender  Date of Birth   Time of Birth   Date/Time   PHP Coder   Sanity %Age

Above are my sample database columns and First row of my excel sheet.I want to match the column names of my rows before inserting them to database. My code till now gives me a 2 dimensional array in which I get the column names and values.The reason I want to check the column name before inserting is that,my excels can be in any order of the column names. I used exampleReader01 in the package and some SO reference to achieve this.

  $headingsArray = $objWorksheet->rangeToArray('A1:'.$highestColumn.'1',null, true, true, true);
  $headingsArray = $headingsArray[1];
  $r = -1;
  $namedDataArray = array();
  for ($row = 2; $row <= $highestRow; ++$row) {
  $dataRow = $objWorksheet->rangeToArray('A'.$row.':'.$highestColumn.$row,null, true, true, true);
  if ((isset($dataRow[$row]['A'])) && ($dataRow[$row]['A'] > '')) {
       ++$r;
  foreach($headingsArray as $columnKey => $columnHeading){
      $namedDataArray[$r][$columnHeading] = $dataRow[$row][$columnKey];
    }
  }
}

Now I want some help how can I insert this in the right column. My array is like this

Array
(
 [0] => Array
    (
        [First Name] => Mark
        [Last Name] => Baker
        [Nationality] => British
        [Gender] => M
        [Date of Birth] => 19-Dec-60
        [Time of Birth] => 1:30
        [Date/Time] => 22269.0625
        [PHP Coder] => 1
        [Sanity %Age] => 32%
    )

[1] => Array
    (
        [First Name] => Toni
        [Last Name] => Baker
        [Nationality] => British
        [Gender] => F
        [Date of Birth] => 24-Nov-50
        [Time of Birth] => 20:00
        [Date/Time] => 18591.83333
        [PHP Coder] => 
        [Sanity %Age] => 95%
    )

[2] => Array
    (
        [First Name] => Rachel
        [Last Name] => Baker
        [Nationality] => British
        [Gender] => F
        [Date of Birth] => 7-Dec-82
        [Time of Birth] => 0:15
        [Date/Time] => 30292.01042
        [PHP Coder] => 
        [Sanity %Age] => 100%
    )

 )

Hope I am clear.
Thanks for your time.

  • 写回答

1条回答 默认 最新

  • dqz13288 2012-10-12 05:02
    关注

    If I can assume that your Excel column header names never change, why not simply use a mapping array?

    $dbMapping = array(
        'col1' => header1,
        'col2' => header2,
         ..
        'colN' => headerN
    );
    

    So when you're ready to insert to the database, you iterate through each row with the column header names you already have in your 2D array and pass it into your mapping array i.e. $dbMapping['col1'] and that will get you your header name and you can grab the correct row value.

    psuedo

     foreach ($rows as $row) {
         insert into col1, col2, ... colN
           values ($rows[$dbMapping['col1']], $rows[$dbMapping['col2']], ...
     }
    

    Of course it would be in your best interest to use parameterized values.

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

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程