0
我有一张有大量工作表的工作簿。我想遍历每张工作表并将行数据插入MySQL数据库。目前,我的脚本在数据库中插入了1000行空行。PHPExcel:解析电子表格数据并存储到数据库
$objReader = PHPExcel_IOFactory::createReader('Excel2007');
$objReader->setReadDataOnly(true);
$objPHPExcel = $objReader->load("upload/".$workbook);
$cells = array();
$worksheetCount = $objPHPExcel->getSheetCount();
for($i=0; $i < $worksheetCount; $i++) {
$objPHPExcel->setActiveSheetIndex($i);
$objWorksheet = $objPHPExcel->getActiveSheet();
$highestRow = $objWorksheet->getHighestRow(); // e.g. 10
$highestColumn = $objWorksheet->getHighestColumn(); // e.g 'F'
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn); // e.g. 5
for ($row = 1; $row <= $highestRow; ++$row) {
for ($col = 0; $col <= $highestColumnIndex; ++$col) {
$cells[] = $objWorksheet->getCellByColumnAndRow($col, $row)->getValue();
}
$stmt = $mysqli->prepare('INSERT INTO players(player_name,Birthdate,Org,Level,Position,
BT,Status,Service,League_Group_ID,Player_ID,Team_ID,NL_Position,
NL_Slot,NL_SU_Order,AL_Position,AL_Slot,AL_SU_Order,SP_Order,RP_Order)
VALUES (?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?,?)');
$stmt->bind_param('sssssssssssssssssss',$cells[0],$cells[1],$cells[2],$cells[3],$cells[4],$cells[5],
$cells[6],$cells[7],$cells[8],$cells[9],$cells[10],$cells[11],$cells[12],$cells[13],$cells[14],
$cells[15],$cells[16],$cells[17],$cells[18]);
$stmt->execute();
$stmt->close();
reset($cells);
}
}
?>
在电子表格中的标题匹配数据库中的列。哪里不对?
更新 - 刚刚意识到我不应该在列迭代中进行SQL插入,所以我纠正了它现在将76行标题行插入到数据库中。 (我在电子表格中有76行)。
更新2: - 我在那里有一个reset(),它不是必需的。似乎是我所有问题的原因。它现在似乎正在工作。
我可以探索,作为一个选项,但我也有点好奇,为什么这是行不通的。 – stephenthedev