使用PHPExcel导入时,使用数据映射表列

时间:2015-11-02 06:45:07

标签: php mysql mysqli phpexcel

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

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

现在,我的问题是当我将excel表格中的数据导入到特定格式的数据库时。像,

表名:-Mytable

id |名字|姓氏|电子邮件|日期|

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

id |姓氏|名字|日期|电子邮件|

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

1 个答案:

答案 0 :(得分:0)

首先读取标题,并遍历每个标题条目以获取列名称,然后当您循环行时,您可以构建该行的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;
}
相关问题