Php native two ways to export excel files:
$ Filename = 'File name'; $ filetitle = 'Your title'; if ($ _ POST) {set_time_limit (10000); $ title = ''; ini_set ('memory _ limit ', '300m'); header ('content-Type: application/vnd. ms-excel; charset = utf-8 '); $ name = $ title. ". xls "; header ('content-Disposition: attachment; filename = '. $ name. ''); header ('cache-Control: max-age = 0'); $ where =" 1 = 1 "; $ SQL =" "; $ query = DB :: query ($ SQL); // php file handle. php: // output indicates that the file is directly output to the browser $ fp = fopen ('php: // output', 'A '); // output the Excel column header information $ head = array ('id'); // replace the character $ p_new_lines = array ("\ r \ n", "\ n ", "\ t", "\ r", "\ r \ n ","","
","
","
","
"); $ P_change_line_in_excel_cell =''; foreach ($ head as $ v) {echo iconv ('utf-8', 'gb2312 ', $ v ). "\ t" ;}echo "\ n"; // counter $ cnt = 0; // refresh the output buffer at every $ limit row to save resources $ limit = 100000; // Extract data row by row to save memory while ($ res = mysql_fetch_assoc ($ query) {$ cnt ++; if ($ limit = $ cnt) {// refresh the output buffer to prevent problems caused by excessive data. ob_flush (); flush (); $ cnt = 0;} echo trim ($ res ['id']). "\ t"; echo "\ n ";}}
Method 2:
$ Filename = 'File name'; $ filetitle = 'Your title'; if ($ _ POST) {$ title = ''; ini_set ('memory _ limit ', '300m'); header ('content-Type: application/vnd. ms-excel; charset = utf-8 '); $ name = $ title. ". xls "; header ('content-Disposition: attachment; filename = '. $ name. ''); header ('cache-Control: max-age = 0'); echo'
'; $ Where = "1 = 1"; $ SQL = ""; mysql_query ('set names "utf8"'); mysql_set_charset ('utf8'); $ query = DB:: Query ($ SQL); // php file handle. php: // output indicates that the file is directly output to the browser $ fp = fopen ('php: // output', 'A '); // output the Excel column header information $ head = array ('id', 'XXX'); // replace the character $ p_new_lines = array ("\ r \ n ", "\ n", "\ t", "\ r", "\ r \ n ","","
","
","
","
"); $ P_change_line_in_excel_cell =''; echo"
"; Echo"
"; Foreach ($ head as $ v) {echo"
| ". Iconv ('utf-8', 'gb2312 ', $ v )." | ";} Echo"
"; // Extract data row by row, saving memory while ($ res = mysql_fetch_assoc ($ query) {echo"
"; Echo"
| ". $ Res ['id']." | "; Echo"
". Iconv ('utf-8', 'gb2312', $ res ['XXX']." | "; Echo"
";} Echo"
";}
Related articles:
How to deal with the scientific counting method when exporting excel using php
Detailed sample code for PHPExcel Excel reading time
Code example for obtaining excel document content through php