Tuesday, November 10, 2009

Code to retrieve data from database and display it in excel or MS word

Code to retrieve data from database and display it in excel or MS word

<?php
class example
{
function example($params,$tpl)
{
include("connection.php");
$ab="select ename,cdate from exams";
$ab1=$con->Execute($ab);
$s=0;

    while(!$ab1->EOF)
    {
       $as[$s]=$ab1->fields[0];   
       $aa[$s]=$ab1->fields[1];

       $s++;
       $ab1->MoveNext();
    }

//print_r($aa);

header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=excelfile.xls");
header("Pragma: no-cache");
header("Expires: 0");
echo $header;
echo "<table border='0'></tr><td>ename</td><td>date</td></tr>";
for($i=0;$i<=sizeof($as);$i++)
    {
echo "<tr><td>".$as[$i]."</td><td>".$aa[$i]."</td></tr>";
}
echo "</table>";
}
?>


The fields which are marked as red are very important.

No comments:

Post a Comment