Step 1: Create a connection file
<?php
$hostname="localhost";
$username="root";
$password="";
$database="banana1";
@mysql_connect($hostname,$username,$password) or die("connection fails");
@mysql_select_db($database) or die ("no database is created");
?>
step 2: create one more PHP file with your own name and put the below give code
<?php
include 'connection.php';
function export_excel_csv()
{
$sql = "SELECT * FROM sanghar_centers";
$rec = mysql_query($sql) or die (mysql_error());
$num_fields = mysql_num_fields($rec);
header("Content-type: application/octet-stream");
header("Content-Disposition: attachment; filename=reports.xls");
header("Pragma: no-cache");
header("Expires: 0");
for($i = 0; $i < $num_fields; $i++ )
{
$header = mysql_field_name($rec,$i)."\t";
print "$header";
}
print "\n";
while($row = mysql_fetch_row($rec))
{
$line = '';
foreach($row as $value)
{
if((!isset($value)) || ($value == ""))
{
$value = "\t";
}
else
{
$value = str_replace( '"' , '""' , $value );
$value = '"' . $value . '"' . "\t";
}
$line .= $value;
}
$data = trim( $line ) . "\n";
print "$data";
}
print "\n";
$data = str_replace("\\r" , "" , $data);
if ($data == "")
{
$data = "\\n No Record Found!\n";
}
}
export_excel_csv();
?>
Step 3: Once you paste the code given in step:2 do the below given changes in that code.
a) find "SELECT * FROM sanghar_centers";
replace sanghar_centers with you own table name.
now you Success fully done the Export to Excel work. If you have any Queries you can write in comment.
No comments:
Post a Comment