A simple way to output excel in PHP (CSV format)

Source: Internet
Author: User
Tags openid

If you do not read the data we directly use the following code, you need to export the relevant data to Excel table can be, such a simple operation does not need to use those class library or something. Directly with the header of the way can be:header ("Content-type:application/vnd.ms-excel");

The code is as follows Copy Code

<?php
Header ("Content-type:application/vnd.ms-excel");
Header (' Content-disposition:attachment;filename= "Yingcai.xls");

?>
<table border= "1″>
<tr>
<td> User name </td>
<td> Password </td>
</tr>
<tr>
<td>admin</td>
<td>yingcai</td>
</tr>
</table>


Simple and pure PHP export Excel table method, Wood has too much explanation, fancy decorations, a look at the understand

The code is as follows Copy Code


Header ("Content-type:application/vnd.ms-excel");
Header ("content-disposition:attachment; Filename= User Information table. xls ");
require_once ('./class.db.php ');
$db = new db (' localhost ', ' app_vmovies ', ' root ', ' 123456 ');
$sql = ' select * from ' user's ' ORDER BY ' ID ' desc ';
$ret = $db->get_results ($sql); The
 
//output reads as follows:   
echo "ID". T ";
Echo "nickname". " T ";
echo "Gender". T ";
echo "OpenId". T ";
echo "platform". T ";
echo "Registration date." T ";
echo "n";
 
$len = count ($ret);
for ($i = 0; $i < $len; $i + +) {
    echo $ret [$i] [id]. " T ";
    echo $ret [$i] ["nickname"]. " T ";
    echo $ret [$i] ["gender"]. " T ";
    echo $ret [$i] [OpenID]. " T ";
    echo $ret [$i] [PF]. " T ";
    echo $ret [$i] ["RegDate"]. " T ";
    echo "n";
}

If the program is Utf-8 code, also need to use the Iconv function to transcoding, otherwise it will be garbled, garbled.

Another word format import is similar, the header can be specified:

The code is as follows Copy Code

Header ("Content-type:application/msword");
Header ("content-disposition:attachment; Filename=doc.doc ");

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.