1

我正在使用 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
    ));
?>

我希望你能帮助我,非常感谢你的时间!

4

0 回答 0