If you have Excel files that need to be imported into MySQL, you can
import them easily with PHP. First, you will need to download some
prerequisites:
Step 1 : you can use PHPExcel (download library )
Ster 2 : Create import.php file
<?php
mysql_connect("localhost","root","");
mysql_select_db("test") or die(mysql_error());
require_once 'Classes/PHPExcel/IOFactory.php';
$objPHPExcel = PHPExcel_IOFactory::load("test.xlsx");
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
for ($row = 2; $row <= $highestRow; ++ $row)
{
$name = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
$age = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
$email = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
$sql_get = "INSERT INTO user (name,code,sub_dept) VALUES ('".$name."','".$age."','".$email."')";
mysql_query($sql_get) or die(mysql_error());
}
}
?>
Step 1 : you can use PHPExcel (download library )
Ster 2 : Create import.php file
<?php
mysql_connect("localhost","root","");
mysql_select_db("test") or die(mysql_error());
require_once 'Classes/PHPExcel/IOFactory.php';
$objPHPExcel = PHPExcel_IOFactory::load("test.xlsx");
foreach ($objPHPExcel->getWorksheetIterator() as $worksheet)
{
$worksheetTitle = $worksheet->getTitle();
$highestRow = $worksheet->getHighestRow();
$highestColumn = $worksheet->getHighestColumn();
$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
$nrColumns = ord($highestColumn) - 64;
for ($row = 2; $row <= $highestRow; ++ $row)
{
$name = $worksheet->getCellByColumnAndRow(0, $row)->getValue();
$age = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
$email = $worksheet->getCellByColumnAndRow(2, $row)->getValue();
$sql_get = "INSERT INTO user (name,code,sub_dept) VALUES ('".$name."','".$age."','".$email."')";
mysql_query($sql_get) or die(mysql_error());
}
}
?>
No comments:
Post a Comment