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.
Example
php_excel_get_data.php
<html>
<head>
<title>ShotDev.Com Tutorial</title>
</head>
<body>
<?
//*** 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>CustomerID</td>
<td>Name</td>
<td>Email</td>
<td>CountryCode</td>
<td>Budget</td>
<td>Used</td>
</tr>
<?
While (!($objRec->EOF) and $No < $PageLen)
{
?>
<tr>
<td><?=$objRec->Fields["CustomerID"]->Value;?></td> <!-- $objRec->Fields(0)->Value or $objRec->Fields("Column1")->Value -->
<td><?=$objRec->Fields["Name"]->Value;?></td>
<td><?=$objRec->Fields["Email"]->Value;?></td>
<td><?=$objRec->Fields["CountryCode"]->Value;?></td>
<td><?=$objRec->Fields["Budget"]->Value;?></td>
<td><?=$objRec->Fields["Used"]->Value;?></td>
</tr>
<?
$No = $No + 1;
$objRec->MoveNext();
}
?>
</table>
<?
}
$objRec->Close();
$strConn->Close();
$strConn = null;
?>
Total : <?=$TotalRecord?> Page <?=$PageNo?> All Page <?=$TotalPage?>
<? if($PageNo > 1){ ?>
<a href="<?=$_SERVER[SCRIPT_NAME];?>?Page=1"><< First</a>
<a href="<?=$_SERVER[SCRIPT_NAME];?>?Page=<?=$PageNo-1?>">< Back</a>
<? }?>
<? if($PageNo < $TotalPage) { ?>
<a href="<?=$_SERVER[SCRIPT_NAME];?>?Page=<?=$PageNo+1?>">Next ></a>
<a href="<?=$_SERVER[SCRIPT_NAME];?>?Page=<?=$TotalPage?>">Last >></a>
<?}?>
<br>
Go to
<? for($intID=1;$intID<=$TotalPage;$intID++){?>
<? if($intID == $PageNo){?>
<b><?=$intID?></b>
<?}else{?>
<a href="<?=$_SERVER[SCRIPT_NAME];?>?Page=<?=$intID?>"><?=$intID?></a>
<?}?>
<?}?>
</body>
</html>
Create a php file and save to path root-path/myphp/
Run
http://localhost/myphp/php_excel_get_data.php
Screenshot



