Rumah >pembangunan bahagian belakang >tutorial php >php生成excel
生成excel有很多方法 这里记录一个相对最简单 在php 5.3.8里能使用的
1 function xlsBOF() { 2 echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0); 3 return; 4 } 5 function xlsEOF() { 6 echo pack("ss", 0x0A, 0x00); 7 return; 8 } 9 function format( $STR ){10 $STR = str_replace( "\"", "", $STR );11 if ( strpos( $STR, "," ) ){12 $STR = "\"".$STR."\"";13 }14 $STR = iconv( "utf-8", "gb2312", $STR );15 return $STR;16 }17 function xlsWriteNumber($Row, $Col, $Value) {18 echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);19 echo pack("d", $Value);20 return;21 }22 function xlsWriteLabel($Row, $Col, $Value ) {23 $L = strlen($Value);24 echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);25 echo $Value;26 return;27 }28 function write_excel_line($hang,$lie,$val){29 if(is_numeric($val)){30 xlsWriteNumber($hang,$lie,$val);31 }else{32 xlsWriteLabel($hang,$lie,$val);33 }34 }35 $mktime = mktime();36 header('Content-Type: text/html; charset=utf-8');37 header("Pragma: public");38 header("Expires: 0");39 header("Cache-Control: must-revalidate, post-check=0, pre-check=0");40 header("Content-Type: application/force-download");41 header("Content-Type: application/octet-stream");42 header("Content-Type: application/download");43 header("Content-Disposition: attachment;filename=$mktime.xls ");44 header("Content-Transfer-Encoding: binary ");45 // XLS Data Cell46 xlsBOF();47 xlsWriteLabel(0, 0, format('单元格A1'));48 xlsWriteLabel(0, 1, format('单元格B1'));49 xlsWriteLabel(0, 2, format('单元格C1'));50 write_excel_line(1, 0, 'A2');51 write_excel_line(1, 1, 'B2');52 write_excel_line(1, 2, 'C2');53 write_excel_line(2, 0, 'A3');54 write_excel_line(2, 1, 'B3');55 write_excel_line(2, 2, 'C3');56 write_excel_line(3 , 0, '40');57 write_excel_line(3 , 1, '41');58 write_excel_line(3 , 2, '42');59 xlsEOF();