Import Excel File into MySQL Database using PHP

You can easily import your data from any Excel file into MySQL Database using a simple PHP script and PHPExcelReader library.

Steps to Import Excel file data into MySQL using PHP Script

1. Download the following library

PHPExcelReader – http://sourceforge.net/projects/phpexcelreader/

2. After you have downloaded the files, please upload them to your web server.  To work around a small bug in PHPExcelReader, copy oleread.inc file from the Excel directory into a new path: Spreadsheet/Excel/Reader/OLERead.php

The PHPExcelReader code will expect OLERead.php to be in the above specific location. Once that is complete, you’re ready to use the PHPExcelReader class.

setOutputEncoding('CP1251');
$data->read('your-excel-file.xls');

$conn = mysql_connect("dataBaseHostName","DataBaseUserName","DataBasePassword");
mysql_select_db("DataBaseName",$conn);

for ($x = 2; $x < = count($data->sheets[0]["cells"]); $x++) {
    $name = $data->sheets[0]["cells"][$x][1];
    $email = $data->sheets[0]["cells"][$x][2];
    $phone = $data->sheets[0]["cells"][$x][3];
    $sql = "INSERT INTO mytable (name,email,phone) 
        VALUES ('$name',$email,'$phone')";
    echo $sql."\n";
    mysql_query($sql);
}
?>
This entry was posted in General. Bookmark the permalink.

Leave a Reply

Your email address will not be published. Required fields are marked *