mysql to excel exporting data using php getting html code also
Posted
by pmms
on Stack Overflow
See other posts from Stack Overflow
or by pmms
Published on 2010-04-30T13:45:51Z
Indexed on
2010/04/30
13:47 UTC
Read the original article
Hit count: 200
hi all following is code for getting xlsheet from mysql using php `
if( ($_POST['Submit']=='generateexcel')) { $tblname=$_GET['generateexcel'];
global $obj_mysql;
$table="tbl_js_login"; // this is the tablename that you want to export to csv from mysql.
function exportMysqlToCsv($table,$filename = 'export.csv') { $csv_terminated = "\n"; $csv_separator = ","; $csv_enclosed = '"'; $csv_escaped = "\"; $sql_query = "select fld_id, fld_fname,fld_lname from $table";
// Gets the data from the database
$result = mysql_query($sql_query);
$fields_cnt = mysql_num_fields($result);
$schema_insert = '';
for ($i = 0; $i < $fields_cnt; $i++)
{
$l = $csv_enclosed . str_replace($csv_enclosed, $csv_escaped . $csv_enclosed,
stripslashes(mysql_field_name($result, $i))) . $csv_enclosed;
$schema_insert .= $l;
$schema_insert .= $csv_separator;
} // end for
$out = trim(substr($schema_insert, 0, -1));
$out .= $csv_terminated;
// Format the data
while ($row = mysql_fetch_array($result))
{
$schema_insert = '';
for ($j = 0; $j < $fields_cnt; $j++)
{
if ($row[$j] == '0' || $row[$j] != '')
{
if ($csv_enclosed == '')
{
$schema_insert .= $row[$j];
} else
{
$schema_insert .= $csv_enclosed .
str_replace($csv_enclosed, $csv_escaped . $csv_enclosed, $row[$j]) . $csv_enclosed;
}
} else
{
$schema_insert .= '';
}
if ($j < $fields_cnt - 1)
{
$schema_insert .= $csv_separator;
}
} // end for
$out .= $schema_insert;
$out .= $csv_terminated;
$out1 = strip_tags($out); } // end while
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Length: " . strlen($out1));
// Output to browser with appropriate mime type, you choose ;)
header("Content-type: text/x-csv");
//header("Content-type: text/csv");
//header("Content-type: application/csv");
header("Content-Disposition: attachment; filename=$filename");
echo $out1;
exit;
}
exportMysqlToCsv($table);
}
include_once $path."includes/jobseeker_form.php";
/* function is_duplicate($login_name)
{
global $obj_mysql;
$sql="SELECT * FROM tbl_admin_details
WHERE fld_login ='$login_name'";
$num=$obj_mysql->get_num_rows($sql);
if($num==0)
return false;
else
return true;
}*/
?>`
the above code we are using for genrating the xlsheet along with xlsheet we are getting html at th top . following is the screen shot of xlsheet
please provide some help how to remove the html code from xlsheet
© Stack Overflow or respective owner