How to import the data of an Excel file into Oracle table using PHP

You can also do this with PHPExcel. Please find the code below which works fine to me.
The code below does not contain a file browser and PHPExcel.php
This can be automated to your system by making loop with reading number of files in a folder

  • <?php
  • /*
  • Author: Rajan Maharjan
  • Website: http://rajanmaharjan.com.np
  • Date: 2010 December, 25
  • */
  • include “PHPExcel.php”;
  • $ext = pathinfo($_FILES[‘file_name’][‘name’], PATHINFO_EXTENSION);
  • $newFileName = (microtime() * pow(10,8)).”.”.$ext;
  • if(! isset($_FILES[‘file_name’][‘tmp_name’])){
  •     $_SESSION[‘error_message’] = ‘No file selected for upload’;
  •     header(“location:index.php”);
  •     exit();
  •     }
  • else if(strtolower($ext) != ‘csv’ && strtolower($ext) != ‘xls’ && strtolower($ext) != ‘xlsx’){
  •     $_SESSION[‘error_message’] = ‘Invalid file format. Please try to upload CSV (Comma Separated Value) file.’;
  •     header(“location:index.php”);
  •     exit();
  •     }
  • move_uploaded_file($_FILES[‘file_name’][‘tmp_name’],”csvfiles/”.$newFileName);
  • $csvFileName=”csvfiles/”.$newFileName; //defined in the page where this is included
  • if(strtolower($ext) == ‘xls’ || strtolower($ext) == ‘xlsx’){
  •     $objPHPExcel = PHPExcel_IOFactory::load($csvFileName);
  •     foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
  •         $worksheetTitle = $worksheet->getTitle();
  •         $highestRow = $worksheet->getHighestRow(); // e.g. 10
  •         //$highestColumn = $worksheet->getHighestColumn(); // e.g ‘F’
  •         //$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
  •         for ($row = 1; $row <= $highestRow; $row++) {
  •             if($row==1)
  •                 continue;
  •             $arrayData = array();
  •             $arrayData[‘first_field’]     = $worksheet->getCellByColumnAndRow(1, $row)->getValue();
  •             $arrayData[‘second_field’]= $worksheet->getCellByColumnAndRow(2, $row)->getValue();
  •             $arrayData[‘third_field’]= $worksheet->getCellByColumnAndRow(3, $row)->getValue();
  •             $arrayData[‘fourth_field’]     = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
  •             $arrayData[‘fifth_field’] = $worksheet->getCellByColumnAndRow(5, $row)->getValue();
  •             $object_functions->insert_to_db($arrayData); //insert queries goes here
  •         }
  •     }
  • }
  • else if (strtolower($ext) == ‘csv’){
  •     $row = 1;
  •     if (($handle = fopen($csvFileName, “r”)) !== FALSE) {
  •         while (($data = fgetcsv($handle, 1000, “,”)) !== FALSE) {
  •             if($row++ <= 1)
  •                 continue;
  •             $arrayData = array();
  •             $arrayData[‘first_field’]     = $data[1];
  •             $arrayData[‘second_field’]= $data[2];
  •             $arrayData[‘black_list_date_np’]= $data[3];
  •             $arrayData[‘third_field’]     = $data[4];
  •             $arrayData[‘fourth_field’] = $data[5];
  •             $arrayData[‘fifth_field’]             = $data[6];
  •             $object_functions->insert_to_db($arrayData);
  •         }
  •         fclose($handle);
  •     }
  • }
  • unlink($csvFileName);
  • echo ‘Your data has been imported successfully.’;
  • ?>

Leave a Reply

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

Related Posts

Begin typing your search term above and press enter to search. Press ESC to cancel.

Back To Top