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.
0 Comment