Saturday 8 April 2017

Export MySQL data to Excel in PHP

public function actionExportpropertyexcel()
{
$Property = YOUR_SQL_QUERY

$filename = 'HEMANT - '.date('Y-m-d h:i:s').' - List.xls';
//header("Content-type: application/vnd-ms-excel");
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".$filename);

echo '<table width="100%" border="1">
<thead>
<tr>
<th>Property id</th>
<th>Property Type</th>
<th>Property Name</th>
<th>Address</th>
<th>Pincode</th>
<th>City</th>
<th>State</th>
<th>Country</th>
<th>Added By</th>
<th>Status</th>
<th>Created On</th>

</tr>
</thead>';
foreach($Property as $prop)
{
echo '
<tr>
<td>'.$prop->id.'</td>
<td>'.$prop->property_type.'</td>
<td>'.$prop->detail->property_name.'</td>
<td>'.$prop->locality.'</td>
<td>'.$prop->pincode.'</td>
<td>'.$prop->city.'</td>
<td>'.$prop->state.'</td>
<td>'.$prop->country.'</td>
<td>'.$prop->property_owner_detail->detail->name.'</td>
<td>'.$prop->status.'</td>
<td>'.$prop->created_at.'</td>
</tr>
';
}
echo '</table>';
}

2 comments: