Import data from excel file to php database

Import data from excel file to php database

Import data from excel file to php database

You have an Excel file, and if you want to import that Excel file to your database, then fetch the cell value using these tutorials. You can directly import to your MySQL, MS SQL, and various databases using this library and tutorials. Just download the library and integrate the below code there.

Step 1)

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

Step 2)

Make a form for uploading your Excel file as below.

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

Step 3)

Now make code to read the Excel file cell and import it to the database as below.

<?php
include('PHPExcel-1.8/Classes/PHPExcel/IOFactory.php');

if(isset($_POST["submit"]))
{
	$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) . '": ' . 
		$e->getMessage());
	}

	$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>";
    print_r($newArr);
	
    echo "Record Update Successfully...";
}
?>

Step 4)

Now upload your Excel to the form and submit the Excel; it will return you all the cell values, which you will insert into your database.

17 responses

17 thoughts on “Import data from excel file to php database

  1. antibiotics for dogs

    Hello! I’ve been following your weblog for a long time now and finally got the bravery to go ahead and give you a shout out from Atascocita Tx! Just wanted to mention keep up the great work!

  2. doxycycline antibiotic

    Hi, I do believe this is a great site. I stumbledupon it 😉 I am going to come back once again since I book marked it. Money and freedom is the best way to change, may you be rich and continue to help other people.

  3. cyclosporine eye drops

    I just could not depart your website before suggesting that I actually enjoyed the standard info an individual supply for your visitors? Is going to be back often in order to investigate cross-check new posts

  4. kamagra oral jelly

    I’ll immediately snatch your rss feed as I can’t find your e-mail subscription link or newsletter service. Do you have any? Please let me recognise in order that I may just subscribe. Thanks.

  5. back biome

    **back biome**

    Backbiome is a naturally crafted, research-backed daily supplement formulated to gently relieve back tension and soothe sciatic discomfort.

Leave a Reply

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