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