How to read Excel(.xlsx) file in php(codeigniter)?

4k Views Asked by At

I'm using CodeIgniter and I want to read excel file to extract data and save it to PHPmyAdmin database.

I've tried some third party libraries like

1- SimpleXLSX.class.php
2- Excel_reader

but these are not helping me.

Can anybody please give me a good example of how to read excel files in CodeIgniter efficiently?

1

There are 1 best solutions below

1
On

Here is the code to read from .xlsx file in to mysql database.

  $csvFile = fopen("path/to/your/file");
  //skip first line
  fgetcsv($csvFile);
  //read data from csv file line by line
  while(($line = fgetcsv($csvFile)) !== FALSE){        
     //You can have your active record's query to insert it into Db
     $db->query("INSERT INTO tableName (Column1) VALUES ('".$line[0]."')");
  }                
  //close opened csv file
  fclose($csvFile);