Learning & Integrating web technology and code help directory

How to use PHP & Get Data From excel Worksheet (Microsoft Excel Driver (*.xls))

No comments
How to use PHP & Get Data From excel Worksheet (Microsoft Excel Driver (*.xls)) The Learn / tutorial php programming how to using  PHP Get Data From excel Worksheet.
ShotDev Focus:
- PHP  & Get Data From excel Worksheet.
PHP & Add Style & Get Data From excel Worksheet
Example
php_excel_get_data.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.   
  11. //*** Connect to ADO ***//  
  12. $strConn = new COM("ADODB.Connection");  
  13. $strConn->Open("DRIVER={Microsoft Excel Driver (*.xls)}; IMEX=1;  
  14. HDR=NO; Excel 8.0; DBQ=".$strPath."/".$OpenFile.";");  
  15. // DBQ=realpath($OpenFile)  
  16.   
  17. //*** Select Sheet ***//  
  18. $strSQL = "SELECT * FROM [Sheet1$]";  
  19. $objRec = new COM("ADODB.Recordset");  
  20. $objRec->Open($strSQL$strConn, 1,3);  
  21.   
  22. If($objRec->EOF)  
  23. {  
  24. echo(" Not found record.");  
  25. }  
  26. else  
  27. {  
  28.   
  29. $PageLen = 2;  
  30. $PageNo = $_GET["Page"];  
  31. if(!$PageNo)  
  32. {  
  33. $PageNo = 1;  
  34. }  
  35. $TotalRecord = $objRec->RecordCount();  
  36. $objRec->PageSize = $PageLen;  
  37. $TotalPage = $objRec->PageCount();  
  38. $objRec->AbsolutePage = $PageNo;  
  39. ?>  
  40. <table width="420" border="1">  
  41. <tr>  
  42. <td>CustomerID</td>  
  43. <td>Name</td>  
  44. <td>Email</td>  
  45. <td>CountryCode</td>  
  46. <td>Budget</td>  
  47. <td>Used</td>  
  48. </tr>  
  49. <?  
  50. While (!($objRec->EOF) and $No < $PageLen)  
  51. {  
  52. ?>  
  53. <tr>  
  54. <td><?=$objRec->Fields["CustomerID"]->Value;?></td> <!-- $objRec->Fields(0)->Value or $objRec->Fields("Column1")->Value -->  
  55. <td><?=$objRec->Fields["Name"]->Value;?></td>  
  56. <td><?=$objRec->Fields["Email"]->Value;?></td>  
  57. <td><?=$objRec->Fields["CountryCode"]->Value;?></td>  
  58. <td><?=$objRec->Fields["Budget"]->Value;?></td>  
  59. <td><?=$objRec->Fields["Used"]->Value;?></td>  
  60. </tr>  
  61. <?  
  62. $No = $No + 1;  
  63. $objRec->MoveNext();  
  64. }  
  65.   
  66. ?>  
  67. </table>  
  68. <?  
  69. }  
  70. $objRec->Close();  
  71. $strConn->Close();  
  72. $strConn = null;  
  73. ?>  
  74. Total : <?=$TotalRecord?> Page <?=$PageNo?> All Page <?=$TotalPage?>  
  75. <? if($PageNo > 1){ ?>  
  76. <a href="<?=$_SERVER[SCRIPT_NAME];?>?Page=1"><< First</a>  
  77. <a href="<?=$_SERVER[SCRIPT_NAME];?>?Page=<?=$PageNo-1?>">< Back</a>  
  78. <? }?>  
  79. <? if($PageNo < $TotalPage) { ?>  
  80. <a href="<?=$_SERVER[SCRIPT_NAME];?>?Page=<?=$PageNo+1?>">Next ></a>  
  81. <a href="<?=$_SERVER[SCRIPT_NAME];?>?Page=<?=$TotalPage?>">Last >></a>  
  82. <?}?>  
  83. <br>  
  84. Go to  
  85. <? for($intID=1;$intID<=$TotalPage;$intID++){?>  
  86. <? if($intID == $PageNo){?>  
  87. <b><?=$intID?></b>  
  88. <?}else{?>  
  89. <a href="<?=$_SERVER[SCRIPT_NAME];?>?Page=<?=$intID?>"><?=$intID?></a>  
  90. <?}?>  
  91. <?}?>  
  92. </body>  
  93. </html>  
Create a php file and save to path root-path/myphp/
Screenshot
PHP & Add Style & Get Data From excel Worksheet
.

No comments :

Post a Comment