Tags
Asked 2 years ago
30 Jun 2021
Views 272
Jackie

Jackie posted

how to import excel file in mysql database using php?

excel file need to imported into MySQL database , how to do it ?
i know how to read file using fopen and etc . don't show just only file reading

$xcl_content=file_get_contents('q.xls')'
// now file reading to database .how to do it ?
.
how to import excel file in MySQL database using php?
duglus

duglus
answered Jul 24 '21 00:00

PHPExcel
use PHPExcel to read excel file.
install it with composer .


$reader = Asan\PHPExcel\Excel::load('abc.xls', function(Asan\PHPExcel\Reader\Xls $reader) {
$count = $reader->count();
for($i=0;$i<=$count;$i++)
$reader->seek(50);
$current = $reader->current();
//make insert query as per need and execute it do it your ways  
$insert="insert into tablename (`field1`,`field2`) values('".$current->field1."','".$current->field1."')";
mysqli_query($connection , $insertquery);

}

}



or else use this code to read xls file , and run insert or update query as per need


$reader = Asan\PHPExcel\Excel::load('abc.xls', function(Asan\PHPExcel\Reader\Xls $reader) {
    //$reader->setRowLimit(5);
    $reader->setColumnLimit(10);

    //$reader->setSheetIndex(1);
});

foreach ($reader as $row) {
//make insert query as per need and execute it do it your ways  
$insert="insert into tablename (`field1`,`field2`) values('".$row->field1."','".$row->field1."')";
mysqli_query($connection , $insertquery);
  
}


Post Answer