使用PHPExcel读取Excel并使用MeekroDB插入数组

时间:2019-04-28 08:17:32

标签: php mysqli phpexcel meekro

我正在使用PHPExcel从Excel获取列的列表,到目前为止一切都很好,我可以获取数组并在屏幕上查看数据,即使是我感兴趣的列并省略了第一行,问题是我无法将这些数组结果插入数据库中,甚至无法在数组中插入第一条记录。你能帮助我吗?谢谢!

<?php
require_once 'connect.php';
require_once 'Excel/PHPExcel/IOFactory.php';

$objPHPExcel = PHPExcel_IOFactory::load('prices.xlsx');

$worksheet = $objPHPExcel->getSheet(0);

foreach($objPHPExcel->getWorksheetIterator() as $worksheet)
{
    $highestRow = $worksheet->getHighestRow();
    $highestColumn = $worksheet->getHighestColumn();

    for($row=8; $row<=$highestRow; $row++)
    {
        $column1 = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
        $column2 = $worksheet->getCellByColumnAndRow(10, $row)->getValue();
        $column3 = $worksheet->getCellByColumnAndRow(11, $row)->getValue();

        $finaldata[] = array(
            'Key'           =>      trim($column1),
            'Price'         =>      trim($column2),
            'currency'      =>      trim($column3),
        );
    }
}

echo "<pre>";
print_r($finaldata);
die();
?>

我得到以下信息:

Array
(
    [0] => Array
        (
            [Key] => 100001
            [Price] => 40.83
            [currency] => USD
        )

    [1] => Array
        (
            [Key] => 100002
            [Price] => 624.94
            [currency] => USD
        )

    [2] => Array
        (
            [Key] => 100003
            [Price] => 69.74
            [currency] => USD
        )

    [3] => Array
        (
            [Key] => 100004
            [Price] => 150.62
            [currency] => USD
        )

    [4] => Array
        (
            [Key] => 100005
            [Price] => 223.15
            [currency] => USD
        )

    [5] => Array
        (
            [Key] => 100006
            [Price] => 92.94
            [currency] => USD
        )
)

现在,我尝试使用以下代码在数据库中插入所有记录:

<?php
    DB::insert('prices', array(
        'Key' => $column1,
        'Price' => $column2,
        'currency' => $column3
    ));
?>

希望您能为我提供帮助,在此先感谢您!

0 个答案:

没有答案