标签:
现在,很多地方都需要导出数据,这里说一种简单的方法将数据以Excel的形式导出,方法如下:
1 <?php 2 date_default_timezone_set(‘PRC‘);//设置时区 3 4 /*设置head头信息*/ 5 Header("Content-Type:application/vnd.ms-excel;charset=UTF-8"); 6 Header("Accept-Ranges:bytes"); 7 Header("Content-Disposition:attachment;filename=".date(‘YmdHis‘).".xls"); 8 Header("Pragma:no-cache"); 9 Header("Expires:0"); 10 11 $str = $str2 = null; 12 /*设置表格信息*/ 13 $str.= "ID"."\t"; 14 $str.= "用户名"."\t"; 15 $str.= "密码"."\t"; 16 $str.= "邮箱"."\t\r\n"; 17 $info = iconv("UTF-8","GBK",$str); 18 echo $info; 19 20 /*查询内容导出数据库数据*/ 21 $link = @mysql_connect(‘localhost‘,‘root‘,‘‘) or die(‘Failed to connect to the MySQL‘); 22 mysql_select_db(‘l_test‘,$link); 23 mysql_set_charset(‘utf8‘,$link); 24 $query = ‘select * from lt_user‘; 25 $result = mysql_query($query); 26 while($row = mysql_fetch_assoc($result)){ 27 $list[] = $row; 28 } 29 mysql_free_result($result); 30 mysql_close($link); 31 32 foreach($list as $v){ 33 $str2.= trim(iconv("UTF-8","GBK",$v[‘id‘]))."\t"; 34 $str2.= trim(iconv("UTF-8","GBK",$v[‘username‘]))."\t"; 35 $str2.= trim(iconv("UTF-8","GBK",$v[‘passwd‘]))."\t"; 36 $str2.= trim(iconv("UTF-8","GBK",$v[‘email‘]))."\t\r\n"; 37 } 38 echo $str2; 39 ?>
这个方法相对简单,但在一些小的程序上,作为一个简单的方法还是不错的,希望能对大家有所帮助。
标签:
原文地址:http://www.cnblogs.com/blogforly/p/5910266.html