最新消息:本站所有跳转向bbs.ykit.cn的附件将全面停止,附件已转移到https://www.qingsj.com

PHP中导出Excel,将数据以Excel形式导出

PHP 有客 108浏览 0评论
<?php 
date_default_timezone_set('PRC');//设置时区

/*设置head头信息*/
Header("Content-Type:application/vnd.ms-excel;charset=UTF-8");
Header("Accept-Ranges:bytes");
Header("Content-Disposition:attachment;filename=".date('YmdHis').".xls");
Header("Pragma:no-cache");
Header("Expires:0");

$str = $str2 = null;
/*设置表格信息*/
$str.= "ID"."\t";
$str.= "用户名"."\t";
$str.= "密码"."\t";
$str.= "邮箱"."\t\r\n";
$info = iconv("UTF-8","GBK",$str);
echo $info;

/*查询内容导出数据库数据*/
$link = @mysql_connect('localhost','root','') or die('Failed to connect to the MySQL'); 
mysql_select_db('l_test',$link);
mysql_set_charset('utf8',$link);
$query = 'select * from lt_user';
$result = mysql_query($query);
while($row = mysql_fetch_assoc($result)){
$list[] = $row; 
}
mysql_free_result($result);
mysql_close($link);

foreach($list as $v){
$str2.= trim(iconv("UTF-8","GBK",$v['id']))."\t";
$str2.= trim(iconv("UTF-8","GBK",$v['username']))."\t";
$str2.= trim(iconv("UTF-8","GBK",$v['passwd']))."\t";
$str2.= trim(iconv("UTF-8","GBK",$v['email']))."\t\r\n";
}
echo $str2;
?>

转载请注明:有客帮 » PHP中导出Excel,将数据以Excel形式导出

您必须 登录 才能发表评论!