8 พฤษภาคม 2556

Simple export php to excel
  1. $result=mysql_query("select * from tbl_name");
  2. function xlsBOF()
  3. {
  4. echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
  5. return;
  6. }
  7. function xlsEOF()
  8. {
  9. echo pack("ss", 0x0A, 0x00);
  10. return;
  11. }
  12. function xlsWriteNumber($Row, $Col, $Value)
  13. {
  14. echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
  15. echo pack("d", $Value);
  16. return;
  17. }
  18. function xlsWriteLabel($Row, $Col, $Value )
  19. {
  20. $L = strlen($Value);
  21. echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
  22. echo $Value;
  23. return;
  24. }
  25. header("Pragma: public");
  26. header("Expires: 0");
  27. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  28. header("Content-Type: application/force-download");
  29. header("Content-Type: application/octet-stream");
  30. header("Content-Type: application/download");;
  31. header("Content-Disposition: attachment;filename=list.xls");
  32. header("Content-Transfer-Encoding: binary ");
  33. xlsBOF();
  34. xlsWriteLabel(0,0,"Heading1");
  35. xlsWriteLabel(0,1,"Heading2");
  36. xlsWriteLabel(0,2,"Heading3");
  37. $xlsRow = 1;
  38. while($row=mysql_fetch_array($result))
  39. {
  40. xlsWriteNumber($xlsRow,0,$row['field1']);
  41. xlsWriteLabel($xlsRow,1,$row['field2']);
  42. xlsWriteLabel($xlsRow,2,$row['field3']);
  43. $xlsRow++;
  44. }
  45. xlsEOF();