Two PHP export excel examples

Source: Internet
Author: User
Keywords Network programming PHP tutorial
Tags application content data echo examples export file link

Two php tutorial export excel instance

<? php
header ("content-type: application / vnd.ms-excel; charset = gbk");
header ("content-disposition: attachment; filename = test_data.xls");


$ link = mysql tutorial _connect ('localhost', 'root', 'hhhkkk');
if ($ link) {

mysql_select_db ('dataui', $ link);
mysql_query ("set names 'gbk'");
Echo "database tutorial connection has been successful!";
} else {
echo "database connection failed!";
}

echo "project name". "t";
echo "project details". "t";
$ sql = "select * from php168_item_content where fid = '11 '";
$ query = mysql_query ($ sql);
while ($ rs = mysql_fetch_array ($ query)) {
echo $ rs [title]. "t";
$ sql2 = "select * from php168_item_content_1 where id = '$ rs [id]';
$ query2 = mysql_query ($ sql2);
while ($ rs2 = mysql_fetch_array ($ query2)) {
echo $ rs2 [content]. "t";
echo "n";

}

}


?>

Export excel, if a column is exported ID number, open the excel file will find that the ID number automatically using scientific notation, anyway, modify the column properties, are unable to meet their own requirements. Some people say that online, after the first attribute to the text into the text, then there is no problem in the importation, the actual operation of excel is true, however, the php program can not be exported

<? php
// Experimental data, the actual operation, here should be obtained from the database data
$ emps Tutorial [0] ['id'] = '00001';
$ emps [0] ['name'] = 'abc';
$ emps [0] ['sexual'] = 'male';
$ emps [0] ['age'] = 28;

$ emps [1] ['id'] = '00002';
$ emps [1] ['name'] = 'bbc';
$ emps [1] ['sexual'] = 'male';
$ emps [1] ['age'] = 23;

$ emps [2] ['id'] = '00003';
$ emps [2] ['name'] = 'cba';
$ emps [2] ['sexual'] = 'female';
$ emps [2] ['age'] = 20;

ini_set ('include_path', '/ data / website / htdocs / includes');
require_once ('smarty.php');
$ smarty = new smarty ();

$ smarty-> assign ('emps', $ emps);

Output file header, that is to output excel file
header ("content-type: application / vnd.ms-excel");
header ("content-disposition: attachment; filename = test.xls");
$ smarty-> display ('excel-xml.tpl');
?>

Related Article

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.