Php output excel file _ PHP Tutorial

Source: Internet
Author: User
Php outputs an excel file. If you want to use php to output excel files, you must use headercontent-type: applicationvnd. ms-excel. As follows? Php $ filenamename.. php Tutorial output excel format file
If you want to use php to output an excel file, you must use header content-type: application/vnd. ms-excel. As follows:

$ Filename = name .'.xls ';
Header ("content-type: application/vnd. ms-excel ");
Header ("content-disposition: attachment; filename = $ filename ");
?>

Let's look at the php output excel instance.

Header ("content-type: application/vnd. ms-excel ");
Header ("content-disposition: filename=test.xls ");
Echo "a1tb1tc1tna2ta3ta4tn"; // r t cell, n new row
?>

Require_once ("../config/sys_config.php"); // configuration file
Require_once (".../../include/db_class.php ");
Header ("content-type: text/html; charset = $ page_code"); // page encoding
Header ("content-type: application/vnd. ms-excel ");
Header ("content-disposition: attachment; filename =". mb_convert_encoding ("customer data report", "gbk", $ page_code). ". xls ");
Header ("pragma: no-cache ");
Header ("expires: 0 ");
// $ Usersid = intval ($ _ get ['uid']); // user ID

// The output content is as follows:
// Output header
Echo iconv ("UTF-8", "gb2312", "customer name"). "t ";
Echo iconv ("UTF-8", "gb2312", "telephone"). "t ";
Echo iconv ("UTF-8", "gb2312", "address"). "t ";
Echo iconv ("UTF-8", "gb2312", "add Date"). "t ";
Echo "n"; // line feed

$ Sqlstr = "select * from clients where usersid = 32 order by clientsid desc ";
$ Rows = $ db-> select ($ sqlstr );
$ Num = count ($ rows); // total number of customers
For ($ I = 0; $ I <$ num; $ I ++)
{
Echo iconv ("UTF-8", "gb2312", $ rows [$ I] [clientsname]). "t ";
Echo iconv ("UTF-8", "gb2312", $ rows [$ I] [clientsphone]). "t ";
Echo iconv ("UTF-8", "gb2312", $ rows [$ I] [clientsaddress]). "t ";
Echo iconv ("UTF-8", "gb2312", $ rows [$ I] [clientstime]). "t ";
Echo "n"; // line feed
}
?>


Another simple instance

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

Header ("content-disposition: attachment?filename=users.xls ");

Echo "company name". "t ";

Echo "user name". "t ";

Echo "password". "t ";

Echo "second-level domain name". "t ";

Echo "n ";

Foreach ($ result ['result'] as $ val ){

Echo "$ val-> comname". "t ";

Echo "$ val-> username". "t ";

Echo "$ val-> usertruepw". "t ";

Echo emptyempty ($ val-> domainname )? '': ('Http: // '. $ val-> domainname.' .jiaomai.com ')." t ";

Echo "n ";

}

If you want to use php to output an excel file, you must use header content-type: application/vnd. ms-excel. As follows? Php $ filename = name .'....

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.