Import Excel to PHP with phpoffice/phpspreadsheet – Step-by-Step Guide with Code


Programming Import Excel to PHP with phpoffice/phpspreadsheet – Step-by-Step Guide with Code

06 Aug 2024 10:00 944 reads

 

phpoffice/phpspreadsheet เป็นไลบรารี PHP ที่นิยมใช้สำหรับการทำงานกับไฟล์ Excel ทั้งการสร้างไฟล์ใหม่ การอ่านข้อมูลจากไฟล์ที่มีอยู่ และการแก้ไขข้อมูลต่างๆ ภายในไฟล์ Excel

 

ขั้นตอนการ Import ข้อมูล

1. ติดตั้งไลบรารี

  • Composer: วิธีที่ง่ายที่สุดคือการใช้ Composer เพื่อติดตั้งไลบรารีนี้ เพียงแค่รันคำสั่งต่อไปนี้ใน terminal ที่ directory ของ project
composer require phpoffice/phpspreadsheet

 

2. สร้างไฟล์ PHP

 

  • สร้างไฟล์ index_import.php เป็น form สำหรับ upload file excel ที่เราต้องการโหลดขึ้นเพื่ออ่านข้อมูล

3. เขียนไฟล์ importData.php สำหรับให้ form เรียกมาทำงานที่ไฟล์นี้ โดยจะเป็นตัวอ่านข้อมูลตามที่เรากำหนดไว้เพื่อเรียกข้อมูลตามช่องต่างๆ ของ excel

  • สร้างไฟล์ index_import.php เป็น form สำหรับ upload file excel ที่เราต้องการโหลดขึ้นเพื่ออ่านข้อมูล
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); 
 
?>
อธิบายโค้ด
  • require \vendor/autoload.php';: บรรทัดนี้ใช้เพื่อโหลดไฟล์ autoload.php ซึ่งจะช่วยให้เราสามารถใช้งานคลาสต่างๆ ในไลบรารี phpoffice/phpspreadsheet ได้
  • ใช้ Namespace use PhpOffice\PhpSpreadsheet\Reader\Xlsx; กำหนดการใช้คลาส Xlsx จากไลบรารี PhpSpreadsheet โดยตรง ช่วยให้การเขียนโค้ดสั้นลงและอ่านง่ายขึ้น

เพียงเท่านี้ก็สามารถนำเข้าข้อมูลจากไฟล์ excel มาอ่านได้แล้วและสามารถนำมาใช้ในการทำงานต่อไปได้ ไม่ว่าจะนำเข้า Database หรือ Validate ต่างๆ เป็นต้น


Please rate your satisfaction with this article

Star 1 Star 2 Star 3 Star 4 Star 5

Post a Comment
Your email address will not be displayed to others. Required fields are marked *

CAPTCHA





Recommended Content

What Is Cursor AI? The Intelligent Code Editor That Combines AI Power with VS Code
(02/11/2025 12:45)
Cursor AI is an AI-powered code editor built on top of Visual Studio Code (VS Code). It offers a familiar, flexible int…
Read more
How to reset your iPhone when it is locked with the message
(01/02/2025 12:00)
If your iPhone shows the message "iPhone Unavailable" and you can’t unlock it, you’ll need to perform a Factory Reset a…
Read more
Export HTML Table to Excel with TableExport (No jQuery) — Copy-Ready Code
(15/08/2024 18:00)
In the previous article, we introduced how to import data from an Excel file using the phpoffice/phpspreadsheet library…
Read more
What is og : Open Graph, and what is its significance in SEO for Facebook and social media?
(07/08/2024 11:15)
Open Graph is a protocol developed by Facebook that is used to connect websites with social media platforms through the…
Read more
How to Add the Facebook Comments Plugin to Your Website — Full Guide + Code
(07/08/2024 11:15)
Using the Facebook Comments Plugin on your website is a great way to increase user engagement and make it easy for them…
Read more
How to Create and Submit a Sitemap to Google Search Console – Improve SEO Visibility
(05/08/2024 09:30)
What is a Google Search Console Sitemap? A Google Search Console Sitemap, or simply a sitemap, is like a map you create…
Read more
How to Send LINE Notify Alerts with MQL5 – Step-by-Step Guide
(06/07/2024 08:30)
What is MQL5? MQL5 (MetaQuotes Language 5) is a programming language developed by MetaQuotes Software Corp. for writing…
Read more
Learn MQL5 Programming – Beginner’s Guide to Automated Trading in MetaTrader 5
(20/06/2024 17:30)
MQL5 (MetaQuotes Language 5) is a programming language developed by MetaQuotes Software Corp. for writing automated tra…
Read more