Now, many places need to export data, here is a simple way to export data as Excel, the method is as follows:
1<?PHP2Date_default_timezone_set (' PRC ');//Setting the time zone3 4 /*Set Head header information*/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");Ten One $str=$str 2=NULL; A /*Set up table information*/ - $str. = "ID". " \ t "; - $str. = "User name". " \ t "; the $str. = "Password". " \ t "; - $str. = "Mailbox". " \t\r\n "; - $info=Iconv("UTF-8", "GBK",$str); - Echo $info; + - /*Query Content export database data*/ + $link= @mysql_connect(' localhost ', ' root ', ') or die(' Failed to connect to the MySQL '); A mysql_select_db(' L_test ',$link); atMysql_set_charset (' UTF8 ',$link); - $query= ' SELECT * from Lt_user '; - $result=mysql_query($query); - while($row=Mysql_fetch_assoc($result)){ - $list[] =$row; - } in Mysql_free_result($result); - Mysql_close($link); to + foreach($list as $v){ - $str 2.=Trim(Iconv("UTF-8", "GBK",$v[' ID '])). " \ t "; the $str 2.=Trim(Iconv("UTF-8", "GBK",$v[' username ']). " \ t "; * $str 2.=Trim(Iconv("UTF-8", "GBK",$v[' passwd ']). " \ t "; $ $str 2.=Trim(Iconv("UTF-8", "GBK",$v[' email ']). " \t\r\n ";Panax Notoginseng } - Echo $str 2; the?>
This method is relatively simple, but in some small program, as a simple method is still good, hope to be helpful to everyone.
Export excel in PHP and export the data in Excel form