PHP 5.6 使用PHPExcel 1.8 导入数据库:前端和后端代码示例
if(isset($_POST['import'])){
require_once 'PHPExcel/Classes/PHPExcel.php';
$file = $_FILES['file']['tmp_name'];
// Create a new Reader of the type that has been identified
$inputFileType = PHPExcel_IOFactory::identify($file);
$objReader = PHPExcel_IOFactory::createReader($inputFileType);
// Load the workbook
$objPHPExcel = $objReader->load($file);
// Select worksheet
$worksheet = $objPHPExcel->getSheet(0);
// Get the highest row and column numbers used in the worksheet
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
// Loop through each row of the worksheet
for($row=2; $row<=$highestRow; $row++){
// Retrieve cell values
$colA = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
$colB = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
$colC = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
// Insert the values into the database
$query = "INSERT INTO table_name (colA, colB, colC) VALUES ('$colA', '$colB', '$colC')";
mysqli_query($conn, $query);
}
}
原文地址: https://www.cveoy.top/t/topic/oBKd 著作权归作者所有。请勿转载和采集!