Home >Backend Development >PHP Tutorial >Example of php exporting excel format file

Example of php exporting excel format file

WBOY
WBOYOriginal
2016-07-25 08:56:10887browse
  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 xlsWriteNumber($Row, $Col, $Value) {
  10. echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
  11. echo pack("d", $Value);
  12. return;
  13. }
  14. function xlsWriteLabel($Row, $Col, $Value ) {
  15. $L = strlen($Value);
  16. echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
  17. echo $Value;
  18. return;
  19. }
复制代码

2,向浏览器发送header,下载xls文件

  1. // Query Database
  2. $result=mysql_db_query($dbname,"select id,prename,name,sname,grade from appdata where course='$courseid' and sec='$section'")
  3. // Send Header
  4. header("Pragma: public");
  5. header("Expires: 0");
  6. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  7. header("Content-Type: application/force-download");
  8. header("Content-Type: application/octet-stream");
  9. header("Content-Type: application/download");;
  10. header("Content-Disposition: attachment;filename=$courseid-$sec.xls ");
  11. header("Content-Transfer-Encoding: binary ");
  12. // XLS Data Cell
  13. xlsBOF();
  14. xlsWriteLabel(1,0,"Student Register $semester/$year");
  15. xlsWriteLabel(2,0,"COURSENO : ");
  16. xlsWriteLabel(2,1,"$courseid");
  17. xlsWriteLabel(3,0,"TITLE : ");
  18. xlsWriteLabel(3,1,"$title");
  19. xlsWriteLabel(4,0,"SETION : ");
  20. xlsWriteLabel(4,1,"$sec");
  21. xlsWriteLabel(6,0,"NO");
  22. xlsWriteLabel(6,1,"ID");
  23. xlsWriteLabel(6,2,"Gender");
  24. xlsWriteLabel(6,3,"Name");
  25. xlsWriteLabel(6,4,"Lastname");
  26. $xlsRow = 7;
  27. while(list($id,$prename,$name,$sname,$grade)=mysql_fetch_row($result)) {
  28. ++$i;
  29. xlsWriteNumber($xlsRow,0,"$i");
  30. xlsWriteNumber($xlsRow,1,"$id");
  31. xlsWriteLabel($xlsRow,2,"$prename");
  32. xlsWriteLabel($xlsRow,3,"$name");
  33. xlsWriteLabel($xlsRow,4,"$sname");
  34. $xlsRow++;
  35. }
  36. xlsEOF();
  37. exit();
复制代码


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn