Tuesday, September 12, 2017

How to export data from database as an Excel Sheet

 <?php
ob_start();

include '../connection.php';
$branch22 = $_POST['item83'];
$course22 = $_POST['item88'];

 $output = '';

//get student list via button click values as 'export_studentinfo'
if(isset($_POST["export_studentinfo"]))  
{
                   
             
                    echo "<p > STUDENT LIST</p>";
                    echo "     <table id='studentdata' >";
                    echo "   <thead>";
                    echo "   <tr>";
                    echo "     <th>Student ID</th>";
                    echo "     <th>Student Name</th>";
                    echo "     <th>NIC</th>";
                    echo "    <th>Email</th>";
                    echo "    <th>Phone</th>";
                    echo "    <th>Address</th>";
                    echo "      <th>Date of Birth</th>";
                    echo " <th>Gender</th>";
                    echo "      <th>Occupation</th>";
                    echo "      <th>Registered Branch</th>";
                    echo " <th>Registration Date</th>";
                    echo "</tr>";
                    echo "</thead>";
                    echo "<tbody>";
       

       
$query = "SELECT * FROM student s, course c, student_course sc, student_branch sb, branch b where s.std_id = sb.std_id and sb.branch_id=b.branch_id and s.std_id=sc.std_id and sc.course_id=c.course_id and sc.course_id like '$course22' and sb.branch_id like '$branch22' order by b.name asc , c.course_id  ";

$result = $con->query( $query );

//get number of rows returned

$num_results = $result->num_rows;

if( $num_results > 0)
{
while( $row = $result->fetch_assoc() ){
//extract row
extract($row);
//creating new table row per record

echo "<tr>";

echo "<td>{$std_id}</td>";

echo "<td>{$fname}  {$lname}</td>";

echo "<td>{$nic}</td>";

echo "<td>{$email}</td>";

echo "<td>{$mobile}</td>";

echo "<td>{$address}</td>";

echo "<td>{$dob}</td>";

echo "<td>{$gender}</td>";

echo "<td>{$occupation}</td>";

echo "<td>{$name}</td>";

echo "<td>{$regDate}</td>";

}
echo "</tr>";

//end of while

       echo "</tbody>";
      $output .= '</table>';

           header("Content-Type: application/xls");  
           header("Content-Disposition: attachment; filename=student_details_excel.xls");   
           echo $output;
}

else
{
 echo "<script>alert('NO RECORDS FOUND'); window.location.href = '#'; </script>";
}

}

No comments:

Post a Comment

Implemting Google Drive File Uploader using OAuth

Implementing Google Drive Text File Uploader using OAuth 2.0 Creating Project and Registering your API with Google In this part we wil...