PHP将mysql转换excel文件格式xls

来源:互联网 发布:淘宝天猫优惠券怎么做 编辑:程序博客网 时间:2024/06/05 07:52
<SCRIPT type=text/javascript><!--google_ad_client = "pub-4490194096475053";/* 内容页,300x250,第一屏 */google_ad_slot = "3685991503";google_ad_width = 300;google_ad_height = 250;//--></SCRIPT><SCRIPT src="http://pagead2.googlesyndication.com/pagead/show_ads.js" type=text/javascript></SCRIPT><SCRIPT> window.google_render_ad(); </SCRIPT><IFRAME name=google_ads_frame marginWidth=0 marginHeight=0 src="http://pagead2.googlesyndication.com/pagead/ads?client=ca-pub-4490194096475053&amp;dt=1226984271390&amp;lmt=1226285475&amp;prev_slotnames=1891601125&amp;output=html&amp;slotname=3685991503&amp;correlator=1226984271343&amp;url=http%3A%2F%2Fwww.corange.cn%2Farchives%2F2008%2F08%2F1533.html&amp;ea=0&amp;ref=http%3A%2F%2Fwww.corange.cn%2Fhtml%2Fcorange__90.html&amp;frm=0&amp;ga_vid=764259772.1226901159&amp;ga_sid=1226984058&amp;ga_hid=2045914579&amp;ga_fc=true&amp;flash=9.0.124.0&amp;u_h=768&amp;u_w=1024&amp;u_ah=715&amp;u_aw=1024&amp;u_cd=32&amp;u_tz=480&amp;u_java=true&amp;dtd=16" frameBorder=0 width=300 scrolling=no height=250 allowTransparency></IFRAME>
代码如下
<?php
$DB_Server = "localhost";
$DB_Username = "mydowns";
$DB_Password = "";
$DB_DBName = "mydowns";
$DB_TBLName = "user";
$Connect = @mysql_connect($DB_Server, $DB_Username, $DB_Password)
or die("Couldn't connect.");
$Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database.");
$file_type = "vnd.ms-excel";
$file_ending = "xls";
header("Content-Type: application/$file_type");
header("Content-Disposition: attachment; filename=mydowns.$file_ending");
header("Pragma: no-cache");
header("Expires: 0");
$now_date = date('Y-m-d H:i');
$title = "数据库名:$DB_DBName,数据表:$DB_TBLName,备份日期:$now_date";
$sql = "Select * from $DB_TBLName";
$ALT_Db = @mysql_select_db($DB_DBName, $Connect)
or die("Couldn't select database");
$result = @mysql_query($sql,$Connect)
or die(mysql_error());
echo("$title/n");
$sep = "/t";
for ($i = 0; $i < mysql_num_fields($result); $i++) {
echo mysql_field_name($result,$i) . "/t";
}
print("/n");
$i = 0;
while($row = mysql_fetch_row($result))
{
$schema_insert = "";
for($j=0; $j<mysql_num_fields($result);$j++)
{
if(!isset($row[$j]))
$schema_insert .= "NULL".$sep;
elseif ($row[$j] != "")
$schema_insert .= "$row[$j]".$sep;
else
$schema_insert .= "".$sep;
}
$schema_insert = str_replace($sep."$", "", $schema_insert);
$schema_insert .= "/t";
print(trim($schema_insert));
print "/n";
$i++;
}
return (true);
?>
原创粉丝点击