Import data from excel file to php database


You have a excel file and if you want to import that excel file to your database then fetch the cell value using this tutorials. you can directly import to your MySQL, Ms SQL and various database using this library and tutorials. just download library and integrate below code there.

Step 1)

Download the excel read library from here. click here for download

Step 2)

Make a form for upload your excel file as below.

    <form method="post" enctype="multipart/form-data">
                    Select File:
                    <input type="file" name="file" id="file">
                <td colspan="2" align="left">
                    <input type="submit" name="submit" value="Submit">

Step 3)

Now make code for read the excel file cell and import to database as below.


	$inputFileName = $_FILES["file"]["tmp_name"];

	try {
		$inputFileType = PHPExcel_IOFactory::identify($inputFileName);
		$objReader = PHPExcel_IOFactory::createReader($inputFileType);
		$objPHPExcel = $objReader->load($inputFileName);
	} catch (Exception $e) {
		die('Error loading file "' . pathinfo($inputFileName, PATHINFO_BASENAME) . '": ' . 

	$sheet = $objPHPExcel->getSheet(0);
	$highestRow = $sheet->getHighestRow();
	$highestColumn = $sheet->getHighestColumn();

	$newArr = array();
	for($row = 1; $row <= $highestRow; $row++) { 
		$rowData = $sheet->rangeToArray('A' . $row . ':' . $highestColumn . $row, null, true, false);

		array_push($newArr, $rowData);

    echo "<pre>";
    echo "Record Update Successfully...";

Step 4)

Now upload your excel to form and submit the excel it will return you all cell value which you will insert to your database.

One thought on “Import data from excel file to php database

  1. Halina Warner

    I was excited to uncover this web site. I want to to thank you for your time for this wonderful read!! I definitely appreciated every bit of it and I have you book-marked to look at new things in your site.


Leave a Reply

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