โปรแกรมมิ่ง › นำเข้าไฟล์ Excel ด้วย PHP: ใช้ phpoffice/phpspreadsheet แบบง่ายพร้อมโค้ดตัวอย่าง

phpoffice/phpspreadsheet เป็นไลบรารี PHP ที่นิยมใช้สำหรับการทำงานกับไฟล์ Excel ทั้งการสร้างไฟล์ใหม่ การอ่านข้อมูลจากไฟล์ที่มีอยู่ และการแก้ไขข้อมูลต่างๆ ภายในไฟล์ Excel
ขั้นตอนการ Import ข้อมูล
composer require phpoffice/phpspreadsheet
load($_FILES['file']['tmp_name']);
$worksheet = $spreadsheet->getActiveSheet();
$worksheet_arr = $worksheet->toArray();
// Remove header row
unset($worksheet_arr[0]);
foreach($worksheet_arr as $row){
$first_name = $row[0];
$last_name = $row[1];
$email = $row[2];
$phone = $row[3];
$status = $row[4];
echo $first_name.'
';
echo $last_name.'
';
echo $email.'
';
echo $phone.'
';
echo $status.'
';
}
}
}
}
// Redirect to the listing page
header("Location: index_import.php".$qstring);
?>
เพียงเท่านี้ก็สามารถนำเข้าข้อมูลจากไฟล์ excel มาอ่านได้แล้วและสามารถนำมาใช้ในการทำงานต่อไปได้ ไม่ว่าจะนำเข้า Database หรือ Validate ต่างๆ เป็นต้น