Get Even More Visitors To Your Blog, Upgrade To A Business Listing >>

PHP scripts for export data into csv file

Here i will explain how to export data into csv file in php

Follow the step which is given below

STEP - I 

     FILE : exortcontact.php

           <?PHP
                 include_once('ExportToExcel.class.php');
                 include_once("../config/connection.php");
                 $exp=new ExportToExcel();
                 $qry="select * from abc";
                 $exp->exportWithQuery($qry,"ExportData.xls",$con);
           ?>
                 
 NOTE : abc is data table which you convert into csv

STEP - II

   FILE : 
          
           <?php

               class ExportToExcel
             {  
                  function exportWithPage($php_page,$excel_file_name)  
                     {  
                           $this->setHeader($excel_file_name);   require_once "$php_page";   
                      }    

                function setHeader($excel_file_name)  
                   {   
                        header("Content-type: application/octet-stream");  
                        header("Content-Disposition: attachment;filename=$excel_file_name");  
                        header("Pragma: no-cache");  
                        header("Expires: 0");    
                    }    

                function exportWithQuery($qry,$excel_file_name,$conn)  
                  {  
                       $tmprst=mysql_query($qry,$conn);  
                        $header="<center><table border=1px><th>ID</th><th>NAME</th><th>EMAIL</th>                                     <th>PHONE</th><th>CITY</th><th>STATE</th><th>KNOW FROM</th>                                             <th>NEWSPAPER NAME</th><th>DATE</th>";  
                        $num_field=mysql_num_fields($tmprst);  
                        $body="";  
                        while($row=mysql_fetch_array($tmprst,MYSQL_BOTH))  
                        {  
                            $body.="<tr>";   
                            for($i=0;$i<$num_field;$i++)   
                            {    
                               $body.="<td>".$row[$i]."</td>";  
                            }   
                            $body.="</tr>";   
                         }  
                        $this->setHeader($excel_file_name);  
                        echo $header.$body."</table>";  
                   }
         } 
         ?>

NOTE : $header use for the formatting your data which you want to convert 

Keep visiting and enjoy coding 





This post first appeared on STAR LINE TECHNOLAB, please read the originial post: here

Share the post

PHP scripts for export data into csv file

×

Subscribe to Star Line Technolab

Get updates delivered right to your inbox!

Thank you for your subscription

×