473,407 Members | 2,320 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,407 software developers and data experts.

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

24
Hi,

I have many Excel files (more than 200 files). I'm using TOAD to import the Excel files into the Oracle table, but this process is boring and time-consuming especially for this huge number of files.

So, I'd like to do a PHP code which takes the path of the Excel file and imports it automatically into the Oracle table without using TOAD or any other programs.

I'm using PHP OCI8 to handle Oracle queries.

Please help me. Thanks in advance.
Jan 30 '11 #1
4 8664
Dormilich
8,658 Expert Mod 8TB
is your PHP host running on windows?

usually, as Excel is a propriatary file format and most PHP hosts can’t run that, you need some kind of file conversion before that (e.g. xls => csv)
Jan 30 '11 #2
goodamr
24
Yes, My PHP HOST is running on Windows Server 2003, IIS 6.0.

OK, after converting it to CSV, how can I read it?

Thanks alot for replying.
Jan 30 '11 #3
Dormilich
8,658 Expert Mod 8TB
on your server you can try to directly load Excel files (via COM), though I have no experience with it (lack of windows)
Jan 31 '11 #4
pa5ha
1
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


Expand|Select|Wrap|Line Numbers
  1. <?php
  2.  
  3. /* 
  4. Author: Rajan Maharjan
  5. Website: http://rznmzn.com | http://rajanmaharjan.com.np
  6. Date: 2010 December, 25
  7. */
  8. include "PHPExcel.php";
  9. $ext = pathinfo($_FILES['file_name']['name'], PATHINFO_EXTENSION);
  10. $newFileName = (microtime() * pow(10,8)).".".$ext;
  11.  
  12. if(! isset($_FILES['file_name']['tmp_name'])){
  13.     $_SESSION['error_message'] = 'No file selected for upload';
  14.     header("location:index.php");
  15.     exit();
  16.     }
  17.  
  18. else if(strtolower($ext) != 'csv' && strtolower($ext) != 'xls' && strtolower($ext) != 'xlsx'){
  19.     $_SESSION['error_message'] = 'Invalid file format. Please try to upload CSV (Comma Separated Value) file.';
  20.     header("location:index.php");
  21.     exit();
  22.     }
  23.  
  24. move_uploaded_file($_FILES['file_name']['tmp_name'],"csvfiles/".$newFileName);
  25.  
  26. $csvFileName="csvfiles/".$newFileName; //defined in the page where this is included
  27.  
  28.  
  29. if(strtolower($ext) == 'xls' || strtolower($ext) == 'xlsx'){
  30.  
  31.     $objPHPExcel = PHPExcel_IOFactory::load($csvFileName);
  32.  
  33.     foreach ($objPHPExcel->getWorksheetIterator() as $worksheet) {
  34.         $worksheetTitle = $worksheet->getTitle();
  35.         $highestRow = $worksheet->getHighestRow(); // e.g. 10
  36.         //$highestColumn = $worksheet->getHighestColumn(); // e.g 'F'
  37.         //$highestColumnIndex = PHPExcel_Cell::columnIndexFromString($highestColumn);
  38.  
  39.         for ($row = 1; $row <= $highestRow; $row++) {            
  40.             if($row==1)
  41.                 continue;
  42.             $arrayData = array();
  43.             $arrayData['first_field']     = $worksheet->getCellByColumnAndRow(1, $row)->getValue();            
  44.             $arrayData['second_field']= $worksheet->getCellByColumnAndRow(2, $row)->getValue();
  45.             $arrayData['third_field']= $worksheet->getCellByColumnAndRow(3, $row)->getValue();
  46.             $arrayData['fourth_field']     = $worksheet->getCellByColumnAndRow(4, $row)->getValue();
  47.             $arrayData['fifth_field'] = $worksheet->getCellByColumnAndRow(5, $row)->getValue();            
  48.  
  49.  
  50.             $object_functions->insert_to_db($arrayData); //insert queries goes here
  51.         }
  52.  
  53.     }
  54. }
  55. else if (strtolower($ext) == 'csv'){
  56.     $row = 1;
  57.     if (($handle = fopen($csvFileName, "r")) !== FALSE) {
  58.         while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) {               
  59.             if($row++ <= 1)
  60.                 continue;        
  61.  
  62.             $arrayData = array();
  63.  
  64.             $arrayData['first_field']     = $data[1];            
  65.             $arrayData['second_field']= $data[2];
  66.             $arrayData['black_list_date_np']= $data[3];
  67.             $arrayData['third_field']     = $data[4];
  68.             $arrayData['fourth_field'] = $data[5];            
  69.             $arrayData['fifth_field']             = $data[6];
  70.  
  71.             $object_functions->insert_to_db($arrayData);            
  72.         }
  73.  
  74.         fclose($handle);
  75.     }
  76. }
  77. unlink($csvFileName);
  78. echo 'Your data has been imported successfully.';
  79. ?>
Aug 28 '12 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Jake | last post by:
Hello, I was wondering if someone had a example of how to import an excel file into a sql database via asp.net? Thanks in advance. Jake
0
by: ramnaresh_t yadav via .NET 247 | last post by:
Hi, I am pasting the code to import data from Text/Excel files into Database(Oracle ) Table... I think some one needs this .. they can use.. it... ======================= Dim dsDB As New DataSet...
0
by: ramyanet | last post by:
Dear All, I have to enable feature of users to import thier excel file in my web application.I have used the Asp.net with Vb. when they click import button file open dialog must appear,then...
2
by: Steve Kershaw | last post by:
I have a need to extract data from an Oracle database using an SQL query. The problem is that I need to insert a parameter into the query. For example: SELECT EMPLOYEE, DED_CODE FROM QUARTDED...
3
by: harshala | last post by:
I want to open excel file using open dialogue box and display records in datagrid
2
by: rmsterling | last post by:
All, Subject: SQL Server 2005 - How do I import data from an Oracle Data Pump file? Tried looking in the SQL help for this and it's not very forthcoming on how you do this. How do I...
8
by: shenkel55 | last post by:
I'm using Access and Excel 2003. Using either the import wizard or code, I have the same problem. This problem only happens with Excel files automatically generated by Corp IT. If I try to do an...
5
by: maryanncanor | last post by:
Hi, I have an input excel file that needs to be imported to Table1. However, the fieldnames or header in the excel file doesnt match the fieldnames in the database. The fieldnames in the excel file...
16
by: giandeo | last post by:
Hello everybody I am trying to import an excel file for days into MS SQL Server 2000, unfortunately, I cannot see a ray of hope till now. I have surfed the WEB for hours, but again no...
5
by: sumanta123 | last post by:
Dear Sir, How to export/import the excel file in oracle database using sql promt. Please guide me the command(export/import) for the neddful. Thanks in Adavance. Regards Sumanta Panda
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.