CARA IMPORT FILE EXCEL KE DATABASE CODE IGNITER

For those who work with databases and need to constantly import and export data, knowing how to import Excel data into MySQL databases is a crucial skill. This task can be easily accomplished using PHP, a popular web programming language.

Why is importing Excel data to MySQL important?

First of all, it saves time and minimizes the risk of errors that may come with manual data entry. Secondly, it allows users to quickly migrate data from an Excel file to a MySQL database, making the information accessible and easy to work with.

How to Import Excel Data into MySQL using PHP?

The first step is to create a PHP script that will access the Excel file and convert its contents into a format that can be inserted into a MySQL database. This can be achieved using the PHPExcel library, a collection of PHP classes that allows developers to read and write Excel files.

Assuming that the Excel file already exists and contains the data that needs to be imported, the following steps outline how to create a PHP script that will import the data:

Baca Juga :  CARA MEMBUAT APLIKASI PENJUALAN DENGAN EXCEL 2007

Step 1: Include the PHPExcel library

Begin by including the PHPExcel library at the top of the PHP script:

<?php
    require_once 'path/to/PHPExcel/Classes/PHPExcel.php';
?>

Step 2: Load the Excel file

The next step is to load the Excel file into a PHPExcel object:

<?php
    // Create a new PHPExcel object
    $objPHPExcel = new PHPExcel();

    // Load the Excel file
    $objPHPExcel = PHPExcel_IOFactory::load('path/to/excel/file.xls');
?>

Step 3: Prepare the database connection

Before inserting the data into the database, establish a connection to the MySQL database:

<?php
    // Establish a connection to the database
    $servername = "localhost";
    $username = "username";
    $password = "password";
    $dbname = "database_name";

    $conn = new mysqli($servername, $username, $password, $dbname);

    // Check connection
    if ($conn->connect_error) 
        die("Connection failed: " . $conn->connect_error);
    
?>

Step 4: Prepare the MySQL query

Before inserting the data, prepare the MySQL query that will insert the data into the table:

<?php
    // Prepare the MySQL query
    $query = "INSERT INTO table_name (column_1, column_2, column_3) VALUES (?, ?, ?)";

    // Prepare the MySQL statement
    $stmt = $conn->prepare($query);
?>

Step 5: Iterate through the Excel data and insert it into the database

The final step is to iterate through the rows in the Excel file and insert the data into the MySQL database:

<?php
    // Iterate through the rows of the Excel file
    foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) 
        $worksheetTitle = $worksheet->getTitle();
        $highestRow = $worksheet->getHighestRow();
        $highestColumn = $worksheet->getHighestColumn();
        $highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);

        // Iterate through each row of the Excel file
        for ($row = 2; $row <= $highestRow; ++$row) 
            // Get the values from the Excel file
            $colA = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
            $colB = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
            $colC = $worksheet->getCellByColumnAndRow(2, $row)->getValue();

            // Bind the values to the MySQL statement
            $stmt->bind_param("sss", $colA, $colB, $colC);

            // Execute the MySQL statement
            $stmt->execute();
        
    
?>

FAQ

1. Can I import Excel data into a specific MySQL table?

Yes, you can specify the table into which the data should be imported by modifying the MySQL query in Step 4 of the PHP script. Replace “table_name” with the name of the table you want to import the data into.

Baca Juga :  Cara Membuat Inventory Stok Gudang Excel Otomatis

2. What if my Excel file contains multiple worksheets?

If your Excel file contains multiple worksheets, you can iterate through each worksheet and import the data into the database as shown in Step 5 of the PHP script. To iterate through each worksheet, use the getWorksheetIterator() method.

Video Tutorial

For a visual demonstration of how to import Excel data into a MySQL database using PHP, watch this tutorial: