Learning & Integrating web technology and code help directory

How to use PHP & Import Excel Spreadsheet Data into Database (Excel.Application)

No comments
How to use PHP & Export Import Spreadsheet Data into Database (Excel.Application) The Learn / tutorial php programming how to using  PHP Import Excel Spreadsheet Data into Database.
ShotDev Focus:
- PHP  & Import Excel Spreadsheet Data into Database.
Example
php_excel_import.php
  1. <html>  
  2. <head>  
  3. <title>ShotDev.Com Tutorial</title>  
  4. </head>  
  5. <body>  
  6. <?  
  7. //*** Get Document Path ***//  
  8. $strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp  
  9. $OpenFile = "MyXls/MyExcelDB.xls";  
  10. //*** Create Exce.Application ***//  
  11. $xlApp = new COM("Excel.Application");  
  12. $xlBook = $xlApp->Workbooks->Open($strPath."/".$OpenFile);  
  13. //$xlBook = $xlApp->Workbooks->Open(realpath($OpenFile));  
  14.   
  15. $xlSheet1 = $xlBook->Worksheets(1);  
  16.   
  17. //*** Insert to MySQL Database ***//  
  18. $objConnect = mysql_connect("localhost","root","root"or die(mysql_error());  
  19. $objDB = mysql_select_db("mydatabase");  
  20.   
  21. $i=2;  
  22. while($xlSheet1->Cells->Item($i,1) != "")  
  23. {  
  24. $strSQL = "";  
  25. $strSQL .= "INSERT INTO customer ";  
  26. $strSQL .= "(CustomerID,Name,Email,CountryCode,Budget,Used) ";  
  27. $strSQL .= "VALUES ";  
  28. $strSQL .= "('".$xlSheet1->Cells->Item($i,1)."','".$xlSheet1->Cells->Item($i,2)."' ";  
  29. $strSQL .= ",'".$xlSheet1->Cells->Item($i,3)."','".$xlSheet1->Cells->Item($i,4)."' ";  
  30. $strSQL .= ",'".$xlSheet1->Cells->Item($i,5)."','".$xlSheet1->Cells->Item($i,6)."') ";  
  31. mysql_query($strSQL);  
  32. $i++;  
  33. }  
  34.   
  35. //*** Close MySQL ***//  
  36. @mysql_close($objConnect);  
  37.   
  38. //*** Close & Quit ***//  
  39. $xlApp->Application->Quit();  
  40. $xlApp = null;  
  41. $xlBook = null;  
  42. $xlSheet1 = null;  
  43. ?>  
  44. Data Import/Inserted.  
  45. </body>  
  46. </html>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP & Add Style & Import Excel Spreadsheet Data into Database

No comments :

Post a Comment