Export excel in PHP and export the data in Excel form

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.