How does PHP export data from a datasheet to an Excel table?

Source: Internet
Author: User

First, the header information is defined to indicate that an Excel is output. Then in the form of a table to the database information loop echo out, just fine.

The code is as follows Copy Code

<?php

Header ("Content-type:application/vnd.ms-excel");

Header ("Content-disposition:filename=xls_region.xls");

$cfg _dbhost = ' localhost ';

$cfg _dbname = ' TestDB ';

$cfg _dbuser = ' root ';

$cfg _dbpwd = ' root ';

$cfg _db_language = ' UTF8 ';

End configuration


Link Database

$link = mysql_connect ($cfg _dbhost, $cfg _dbuser, $cfg _dbpwd);

mysql_select_db ($cfg _dbname);

Select Encoding Www.111Cn.net

mysql_query ("Set names". $cfg _db_language);

Users table

$sql = "desc users";

$res = mysql_query ($sql);

echo "<table><tr>";

Export the header (that is, the fields that are owned in the table)

while ($row = Mysql_fetch_array ($res)) {

$t _field[] = $row [' field ']; The F in field is capitalized, otherwise there will be no result

echo "<th>". $row [' Field ']. " </th> ";

}

echo "</tr>";

Export 100 Data

$sql = "SELECT * from users limit 100";

$res = mysql_query ($sql);

while ($row = Mysql_fetch_array ($res)) {

echo "<tr>";

foreach ($t _field as $f _key) {

echo "<td>" $row [$f _key]. " </td> ";

}

echo "</tr>";

}

echo "</table>";

?>

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.