Php - 读取 Excel 并存储到数据库

Php - Read Excel and Strore to Db

error_reporting(E_ALL);
ini_set('display_errors', TRUE);
ini_set('display_startup_errors', TRUE);
$inputFileName ="../brief/phaseupload/" . $filename;
//  Read your Excel workbook
        try {
            $inputFileType = PHPExcel_IOFactory::identify($inputFileName);
            $objReader = PHPExcel_IOFactory::createReader($inputFileType);
            $objPHPExcel = $objReader->load($inputFileName);
        } catch(Exception $e) {
            die('Error loading file "'.pathinfo($inputFileName,PATHINFO_BASENAME).'": '.$e->getMessage());
        }

//  Get worksheet dimensions
$sheet = $objPHPExcel->getSheet(0);
$highestRow = $sheet->getHighestRow();
$highestColumn = $sheet->getHighestColumn();
//  Loop through each row of the worksheet in turn
for ($row = 3; $row <= $highestRow; $row++){
//  Read a row of data into an array
 $rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, NULL, TRUE, FALSE);
print_r($rowData);

我使用上面的代码通过 PHPExcel 读取 excel 文件。我设法读取了 excel 值并打印出来。现在的问题是我需要将值存储在数据库中。如何获取值,在本例中是从 $rowData?
以下是示例输出:

Array ( [0] => Array ( [0] => 854273_19 [1] => Beds [2] => 61 [3] => Autumn Winter 2012 [4] => Divans [5] => Fabric [6] => Storage Bedding [7] => Single Divan [8] => Not Required [9] => Fabric [10] => White [11] => Not Required [12] => Not Required [13] => [14] => Not Required [15] => Divan With Mattress [16] => Not Required [17] => Not Required [18] => Not Required ) ) 0

看到你已经有了数组中的行数,你可以在循环内继续这个。

$value1 = $rowData[$row]["A"];
$value2 = $rowData[$row]["B"];
   //and so on...

确保在插入数据库时​​清理字符串。