The simplest php method for exporting excel files

Source: Internet
Author: User
Tags php excel
There are many php class libraries for operating excel or other files on the Internet, which are also well-developed. For example, incomparable cool PHPExcel, official website: http://www.codeplex.com/PHPExcel online there are a lot of php operations excel or other file class libraries, also done very well. For example, incomparable cool PHPExcel, official website: http://www.codeplex.com/PHPExcel, pear Spreadsheet_Excel_Writer class. However, we only use some of the functions, which will make the program a little bloated. When you call these class libraries, no matter how simple you operate, it will consume a huge amount of memory, which is not desirable for us.
For example, if I need a php excel export program, I only need to export the relevant data to an excel table. in this simple operation, I don't need to use the class libraries or anything. You can simply use the header.

After export, you will find a problem. if the data is a number, unexpected situations may occur. For example, "012345" will be changed to "12345" in excel. if you enter a long number such as an ID card number, it will be represented by scientific notation in excel, in addition, the last four digits may see deviations, 0000 displacement, and so on. In this case, you need to set the cell to the text format.
If the program is UTF-8 encoded, you also need to use the iconv function for transcoding, otherwise it will be garbled.
In other word format, you can specify the header.
Header ("Conten t-Type: application/msword ");
Header ("Content-Disposition: attachment; filename=doc.doc ");

<? Php
Header ("Content-type: application/vnd. ms-excel ");
Header ("Content-Disposition: attachment?filename=export_test.xls ");
$ Tab = "\ t"; $ br = "\ n ";
$ Head = "no.". $ tab. "Remarks". $ br;
// The output content is as follows:
Echo $ head. $ br;
Echo "test321318312". $ tab;
Echo "string1 ";
Echo $ br;
 
Echo "330181199006061234". $ tab; // The direct output is recognized as a number by Excel.
Echo "number ";
Echo $ br;
 
Echo "= \" 330181199006061234 \ "". $ tab; // The original output needs to be processed.
Echo "string2 ";
Echo $ br;
?>

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.