Tuesday, June 5, 2012

To Export Data In Excel Sheet From Sql Table

 <?php
if(isset($_REQUEST['spreadsheet']))
{
mysql_connect("localhost","root","");
mysql_select_db('sunsoft1_greeprivate');
$query="select * from table_name";

$res=mysql_query($query);

/* $DbConnection->Select("*","payment_student","","","");
$registredData = $DbConnection->GetRows(); */
//echo $registredData[0][name];
 function xlsBOF() {
    echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
    return;
}

function xlsEOF() {
    echo pack("ss", 0x0A, 0x00);
    return;
}

function xlsWriteNumber($Row, $Col, $Value) {
    echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
    echo pack("d", $Value);
    return;
}

function xlsWriteLabel($Row, $Col, $Value ) {
    $L = strlen($Value);
    echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
    echo $Value;
return;
}

$filename=time();
 

    // Send Header
    header("Pragma: public");
    header("Expires: 0");
    header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
    header("Content-Type: application/force-download");
    header("Content-Type: application/octet-stream");
    header("Content-Type: application/download");;
    header("Content-Disposition: attachment;filename=".$filename.".xls");
    header("Content-Transfer-Encoding: binary ");

    // XLS Data Cell

                xlsBOF();
               // xlsWriteLabel(1,0,"Event :".$eventInfo['fldTitle']);
              //  xlsWriteLabel(2,0,"Registred Members");
                xlsWriteLabel(0,0,"booking_id");
                xlsWriteLabel(0,1,"pay_amount");
                xlsWriteLabel(0,2,"payment_date");
                xlsWriteLabel(0,3,"payer_email");
                xlsWriteLabel(0,4,"item_name");
              
              
                /*xlsWriteLabel(6,5,"Company Function");
                xlsWriteLabel(6,6,"Telephone");
                xlsWriteLabel(6,7,"Aum");
                xlsWriteLabel(6,8,"Registration Date");
                xlsWriteLabel(6,9,"Data Protection");*/
                $xlsRow = 1;
               // foreach($registredData as $record)
               while($record=mysql_fetch_array($res)){
            
                     ++$i;
        //if($record['protection']=="YES") { $protection="Yes";} else { $protection="No"; }
      
        xlsWriteLabel($xlsRow,0,$record[booking_id]);
        xlsWriteLabel($xlsRow,1,$record[pay_amount]);
        xlsWriteLabel($xlsRow,2,$record[payment_date]);
        xlsWriteLabel($xlsRow,3,$record[payer_email]);
        xlsWriteLabel($xlsRow,4,$record[item_name]);
      
        /*xlsWriteLabel($xlsRow,5,$record['companyfunction']);
        xlsWriteLabel($xlsRow,6,$record['telephone']);
        xlsWriteLabel($xlsRow,7,$record['aum']);
        xlsWriteLabel($xlsRow,8,date("M d Y",strtotime($record['dated'])));
        xlsWriteLabel($xlsRow,9,$protection);*/
                    $xlsRow++;
                    }
                     xlsEOF();
                 exit();


}


after that in the form you have to call the file

<form action="excel.php">
<input type="submit" name="submit">
</form>

?>