In php, you can use a professional excel processing class to add, delete, or process excel-related data. If you only want to generate an excel file, you can use the following method to make it simple and fast.
If we do not read the data, we can use the following code directly. We only need to export the relevant data to an excel table. In this simple operation, we do not need to use the class libraries or anything. You can use the header directly: header ("Content-type: application/vnd. ms-excel ");
The Code is as follows: |
Copy code |
<? Php Header ("Content-type: application/vnd. ms-excel "); Header ('content-Disposition: attachment=filename=policyingcai.xls "'); ?> <Table border = "1"> <Tr> <Td> User Name </td> <Td> password </td> </Tr> <Tr> <Td> admin </td> <Td> yingcai </td> </Tr> </Table> |
Simple and pure php excel sheet export method, with too many explanations and fancy decoration, can be understood at a glance
The Code is as follows: |
Copy code |
Header ("Content-type: application/vnd. ms-excel "); Header ("Content-Disposition: attachment; filename ...xls "); Require_once ('./class. db. php '); $ Db = new DB ('localhost', 'app _ vmovies ', 'root', '123 '); $ SQL = 'select * from 'user' order by 'id' desc '; $ Ret = $ db-> get_results ($ SQL );
// The output content is as follows: Echo "ID". "t "; Echo "nickname". "t "; Echo "gender". "t "; Echo "OpenId". "t "; Echo "Platform". "t "; Echo "Registration Date". "t "; Echo "n ";
$ Len = count ($ ret ); For ($ I = 0; $ I <$ len; $ I ++ ){ Echo $ ret [$ I] ["id"]. "t "; Echo $ ret [$ I] ["nickname"]. "t "; Echo $ ret [$ I] ["gender"]. "t "; Echo $ ret [$ I] ["openid"]. "t "; Echo $ ret [$ I] ["pf"]. "t "; Echo $ ret [$ I] ["regdate"]. "t "; Echo "n "; }
|
If the program is UTF-8 encoded, you also need to use the iconv function for transcoding, otherwise it will be garbled.
In addition, if the word format is similar, you can specify the header:
The Code is as follows: |
Copy code |
Header ("Content-Type: application/msword "); Header ("Content-Disposition: attachment; filename=doc.doc "); |