Save the database query result to Excel

Source: Internet
Author: User

Excel can easily process data. If the database data can be read into an excel file, it can be easily processed.

Convert database data to Excel excel. php

<?phpclass excel{    function start()    {      ob_start();     }    function save($path)      {        $data = ob_get_contents();        ob_end_clean();        $this->wirtetoexcel($path,$data);     }   function wirtetoexcel ($fn,$data)     {       $fp=fopen($fn,"wb");       fwrite($fp,$data);       fclose($fp);    }}?>

Create an object of this class, and then call the method of this object to export database data to Excel.

<meta http-equiv="Content-Type" content="text/html; charset=utf-8">  <link href="css/style.css" rel="stylesheet" type="text/css" />  <?php   include("conn.php");  $query=mysql_query("select * from map order by register_date desc") ;   $i=$perpagenum*($page-1)+1;   include_once("excel.php");$Excel=new Excel();$Excel->start();?><table width="600" border="0">  <tr>      <td align="center">companyname_cn</td>      <td align="center">companyname_en"</td>      <td align="center">name</td>      <td align="center">position</td>      <td align="center">tel</td>      <td align="center">fax</td>      <td align="center">email</td>      <td align="center">website</td>      <td align="center">product</td>    </tr> <?phpwhile($myrow = mysql_fetch_array($query)){   ?>    <tr>      <td align="center"><?php echo $myrow["companyname_cn"]; ?></td>      <td align="center"><?php echo $myrow["companyname_en"]; ?></td>      <td align="center"><?php echo $myrow["name"]; ?></td>      <td align="center"><?php echo $myrow["position"]; ?></td>      <td align="center"><?php echo $myrow["tel"]; ?></td>      <td align="center"><?php echo $myrow["fax"]; ?></td>      <td align="center"><?php echo $myrow["email"]; ?></td>      <td align="center"><?php echo $myrow["website"]; ?></td>      <td align="center"><?php echo $myrow["product"]; ?></td>    </tr>  <? }  ?>  </table>  <p>  <?php   $Excel->save("Excel/data.xls"); ?>  </p>

The key code is actually a few lines:

// Contains the class file include_once ("excel. php "); // create an excel Class Object $ Excel = new Excel (); // call the start () method of the object $ Excel-> start (); // call the save () file of the object to claim the excel file. $ Excel-> save ("Excel/data.xls ");

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.