【问题标题】:Mapping Table columns with data while Importing using PHPExcel使用 PHPExcel 导入时将表列与数据映射
【发布时间】:2015-11-02 06:45:07
【问题描述】:

我有一个 php 应用程序,我将数据从 excel 表导入数据库。我正在为此使用 phpExcel。

我已经为此实现了一个简单的导入功能。

现在,我的问题是当我以特定格式将数据表单 excel 表导入数据库时​​。喜欢,

表名:-Mytable

标识 |姓名 |姓氏 |电子邮件 |日期 |

和我用来导入的 excel 文件具有相同的格式。但是格式可以改变 Like,

标识 |姓氏 |姓名 |日期 |电子邮件 |

如何将excel文件中的列映射到数据库表中,以便正确的数据进入数据表。

【问题讨论】:

    标签: php mysql mysqli phpexcel


    【解决方案1】:

    首先读取标题,然后遍历每个标题条目以获取列名,然后当您循环您的行时,您可以构建一个适当映射的该行的 StdClass 对象的关联数组

    $headers = $objPHPExcel->getActiveSheet()->toArray('A1');
    $highestRow = $sheet->getHighestRow();
    
    //  Loop through each row of the worksheet in turn
    for ($row = 1; $row <= $highestRow; $row++){ 
        //  Read a row of data into an array
        $rowData = $sheet->rangeToArray('A' . $row);
    
        //  Map the row data array to the headers array
        $dataMapArray = array_combine(
            $header,
            $rowData
        );
        // If you prefer objects, then convert that associative array to an object
        $dataMapObject = (object) $dataMapArray;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-06-18
      • 2022-08-15
      • 1970-01-01
      • 2021-02-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多