首頁 > 後端開發 > php教程 > php匯出excel格式檔案的例子

php匯出excel格式檔案的例子

WBOY
發布: 2016-07-25 08:56:10
原創
892 人瀏覽過
  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();
复制代码


來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板