Files
simrs-jatim/ExportToExcel.class.php
2024-04-19 14:04:41 +07:00

53 lines
1.6 KiB
PHP

<?php
/*Author: Raju Mazumder
email:rajuniit@gmail.com
Class:A simple class to export mysql query and whole html and php page to excel,doc etc*/
class ExportToExcel
{
function exportWithPage($php_page,$excel_file_name)
{
$this->setHeader($excel_file_name);
require_once "$php_page";
}
function setHeader($excel_file_name)//this function used to set the header variable
{
header("Content-type: text/csv");
header("Content-type: application/octet-stream");//A MIME attachment with the content type "application/octet-stream" is a binary file.
header("Content-type: application/x-msdownload");
header('Content-Type: application/vnd.ms-excel; charset=UTF-8');
header("Content-type: application/x-msdownload");
//Typically, it will be an application or a document that must be opened in an application, such as a spreadsheet or word processor.
header("Content-Disposition: attachment; filename=$excel_file_name");//with this extension of file name you tell what kind of file it is.
header("Pragma: no-cache");//Prevent Caching
header("Expires: 0");//Expires and 0 mean that the browser will not cache the page on your hard drive
}
function exportWithQuery($qry,$excel_file_name,$conn)//to export with query
{
$tmprst = $db->query($qry,$conn);
$header="<center><table border=1px><th>Personal Details</th>";
$num_field=mysqli_num_fields($tmprst);
foreach($tmprst,MYSQL_BOTH->fetchAll() as $row)
{
$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";
}
}
?>