<html> <head> <title>ThaiCreate.Com PHP(COM) Excel.Application Tutorial</title> </head> <body> <?php //*** Get Document Path ***// $strPath = realpath(basename(getenv($_SERVER["SCRIPT_NAME"]))); // C:/AppServ/www/myphp $OpenFile = "MyXls/MyExcelDB.xls"; //*** Connect to ADO ***// $strConn = new COM("ADODB.Connection"); $strConn->Open("DRIVER={Microsoft Excel Driver (*.xls)}; IMEX=1; HDR=NO; Excel 8.0; DBQ=".$strPath."/".$OpenFile.";"); // DBQ=realpath($OpenFile) //*** Select Sheet ***// $strSQL = "SELECT * FROM [Sheet1$]"; $objRec = new COM("ADODB.Recordset"); $objRec->Open($strSQL, $strConn, 1,3); If($objRec->EOF) { echo(" Not found record."); } else { $PageLen = 2; $PageNo = $_GET["Page"]; if(!$PageNo) { $PageNo = 1; } $TotalRecord = $objRec->RecordCount(); $objRec->PageSize = $PageLen; $TotalPage = $objRec->PageCount(); $objRec->AbsolutePage = $PageNo; ?> <table width="420" border="1"> <tr> <td>Column1</td> <td>Column2</td> <td>Column3</td> <td>Column4</td> </tr> <?php While (!($objRec->EOF) and $No < $PageLen) { ?> <tr> <td><?php echo $objRec->Fields["Column1"]->Value;?></td> <!-- $objRec->Fields(0)->Value or $objRec->Fields("Column1")->Value --> <td><?php echo $objRec->Fields["Column2"]->Value;?></td> <td><?php echo $objRec->Fields["Column3"]->Value;?></td> <td><?php echo $objRec->Fields["Column4"]->Value;?></td> </tr> <?php $No = $No + 1; $objRec->MoveNext(); } ?> </table> <?php } $objRec->Close(); $strConn->Close(); $strConn = null; ?> Total : <?php echo $TotalRecord?> Page <?php echo $PageNo?> All Page <?php echo $TotalPage?> <?php if($PageNo > 1){ ?> <a href="<?php echo $_SERVER[SCRIPT_NAME];?>?Page=1"><< First</a> <a href="<?php echo $_SERVER[SCRIPT_NAME];?>?Page=<?php echo $PageNo-1?>">< Back</a> <?php }?> <?php if($PageNo < $TotalPage) { ?> <a href="<?php echo $_SERVER[SCRIPT_NAME];?>?Page=<?php echo $PageNo+1?>">Next ></a> <a href="<?php echo $_SERVER[SCRIPT_NAME];?>?Page=<?php echo $TotalPage?>">Last >></a> <?php }?> <br> Go to <?php for($intID=1;$intID<=$TotalPage;$intID++){?> <?php if($intID == $PageNo){?> <b><?php echo $intID?></b> <?php }else{?> <a href="<?php echo $_SERVER[SCRIPT_NAME];?>?Page=<?php echo $intID?>"><?php echo $intID?></a> <?php }?> <?php }?> </body> </html>
ช่วยกันสนับสนุนรักษาเว็บไซต์ความรู้แห่งนี้ไว้ด้วยการสนับสนุน Source Code 2.0 ของทีมงานไทยครีเอท