<?PHPfunctionXlsbof () {Echo Pack("Ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); return;}functionxlseof () {Echo Pack("SS", 0x0A, 0x00); return;}functionFormat$STR ){ $STR=Str_replace( "\"", "",$STR ); if(Strpos($STR, "," ) ){ $STR= "\"".$STR." \""; } $STR=Iconv("Utf-8", "gb2312",$STR ); return $STR;}functionXlswritenumber ($Row,$Col,$Value) { Echo Pack("Sssss", 0x203, 14,$Row,$Col, 0x0); Echo Pack("D",$Value); return;}functionXlswritelabel ($Row,$Col,$Value ) { $L=strlen($Value); Echo Pack("Ssssss", 0x204, 8 +$L,$Row,$Col, 0x0,$L); Echo $Value; return;}functionWrite_excel_line ($hang,$lie,$val){ if(Is_numeric($val) {xlswritenumber ($hang,$lie,$val); }Else{Xlswritelabel ($hang,$lie,$val); }}$mktime= "Message";Header(' content-type:text/html; Charset=utf-8 ');Header("Pragma:public");Header("expires:0");Header("Cache-control:must-revalidate, post-check=0, pre-check=0");Header("Content-type:application/force-download");Header("Content-type:application/octet-stream");Header("Content-type:application/download");Header("Content-disposition:attachment;filename=$mktime. xls ");Header("Content-transfer-encoding:binary");//XLS Data CellXlsbof (); Xlswritelabel (0, 0, format (' name ')); Xlswritelabel (0, 1, Format (' Mailbox ')); Xlswritelabel (0, 2, format (' phone ')); Xlswritelabel (0, 3, format (' Address ')); Xlswritelabel (0, 4, format (' message content ')); Xlswritelabel (0, 5, format (' Message time ')); $conn[Email protected]mysql_connect("localhost", "root", "") or die(Mysql_error()); @mysql_select_db(' Demo ',$conn) or die(Mysql_error()); mysql_query("SET names UTF8"); $sql= "SELECT * from Message"; $result=mysql_query($sql); $i= 0; while($row=Mysql_fetch_array($result)) { $i++; Write_excel_line ($i, 0,$row[' Name ']); Write_excel_line ($i, 1,$row[' Email ']); Write_excel_line ($i, 2,$row[' Phone ']); Write_excel_line ($i, 3,$row[' Address ']); Write_excel_line ($i, 4,$row[' Content ']); Write_excel_line ($i, 5,$row[' Time ']); } xlseof ();Exit();?>
PHP to import database data into Excel