PHP the simplest and quickest way to export a datasheet to an Excel table (without plug-ins) _php instance

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.

Copy Code code as follows:

<?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

 //linked Database
  $link = mysql_connect ($cfg _dbhost, $cfg _dbuser, $cfg _dbpwd);
 mysql_select_db ($cfg _dbname);
 //Select encoding
 mysql_query ("Set names". $cfg _db_language);

 //users Table
  $sql = "desc users";

  $res = mysql_query ($sql);
 echo "<table><tr>";
 //Export Headers (that is, fields owned in the table)
 while ($row = mysql_fetch_array ($res)) {
   $t _field[] = $row [' The F in Field ']; //field is uppercase, otherwise there will be no result
  echo ' <th> '. $row [' Field ']. " </th> ";
 }
 echo "</tr>";
 //Export 100 data
  $sql = "SELECT * from users limit";
  $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.