前端代码:

<form method="post" enctype="multipart/form-data">
    <input type="file" name="file" required>
    <button type="submit" name="import">Import</button>
</form>

后端代码:

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);
    }
}
``
php56使用PHPEXCEL 18导入数据库前段和后端代码

原文地址: http://www.cveoy.top/t/topic/g8H0 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录