Social Icons

Thursday, May 23, 2013

Export HTML table to excel xlsx in php

Many a times, it has been a requirement in PHP, you have to generate a report and down the html table report to excel. Here, I am providing the solution as how can you do it in PHP.

You can achieve this using PHPExcel library. First download PHPExcel library and put in your project folder. Then, apply the below code to generate excel report. Here is the code.

<?php
$arr['name'] = array('Prem', 'Shailesh', 'Rahul');
$arr['age'] = array('23', '21', '25');


$filename ="Excel_Report.xls";
$contents = "Name \t Age \n";


include('PHPExcel.php');
for($i = 0; $i < count($arr['name']; $i++)
{
   $contents.= $arr['name'][$i]. " \t";
   $contents.= $arr['age'][$i]. " \n";
}


header('Content-type: application/ms-excel');
header('Content-Disposition: attachment; filename='.$filename);
echo $contents;
exit;

?>

No comments:

Post a Comment

Total Pageviews