php导出数据到excel文件 php导出excel乱码问题

WBOY
发布: 2016-07-25 08:56:12
原创
949 人浏览过
  1. /**
  2. * 导出excel文件
  3. * by bbs.it-home.org
  4. */
  5. function xlsBOF() {
  6. echo pack("ssssss", 0x809, 0x8, 0x0, 0x10, 0x0, 0x0);
  7. return;
  8. }
  9. function xlsEOF() {
  10. echo pack("ss", 0x0A, 0x00);
  11. return;
  12. }
  13. function xlsWriteNumber($Row, $Col, $Value) {
  14. echo pack("sssss", 0x203, 14, $Row, $Col, 0x0);
  15. echo pack("d", $Value);
  16. return;
  17. }
  18. function xlsWriteLabel($Row, $Col, $Value ) {
  19. $Value = iconv("UTF-8", "gb2312", $Value); //加上本语句,解决导出excel文件乱码问题20110629
  20. $L = strlen($Value);
  21. echo pack("ssssss", 0x204, 8 + $L, $Row, $Col, 0x0, $L);
  22. echo $Value;
  23. return;
  24. }
  25. include "connection.php";
  26. $sql = "select ledger_name,ledger_sex ,ledger_age ,ledger_add from ps_ledger_11";
  27. $query = mysql_query($sql);
  28. // 文件头
  29. header("Pragma: public");
  30. header("Expires: 0");
  31. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  32. header("Content-Type: application/force-download");
  33. header("Content-Type: application/octet-stream");
  34. header("Content-Type: application/download");
  35. header("Content-Type: application/vnd.ms-excel; charset=UTF-8");
  36. header("Content-Disposition: attachment;filename=警务室辅助警力统计表.xls ");
  37. //header("Content-Disposition: inline; filename=\"" . $filename . ".xls\"");
  38. //iconv("utf-8", "gb2312", $filename);//解决文件引起的乱码".xls\"");
  39. header("Content-Transfer-Encoding: binary ");
  40. // 向表中添加数据
  41. xlsBOF();
  42. xlsWriteLabel(1,0,"列名");
  43. xlsWriteLabel(1,1,"列名");
  44. xlsWriteLabel(1,2,"列名");
  45. xlsWriteLabel(1,3,"列名");
  46. xlsWriteLabel(1,4,"列名");
  47. $xlsRow = 1;
  48. while($array = mysql_fetch_array($query)) {
  49. ++$i;
  50. xlsWriteNumber($xlsRow,0,"$i");
  51. xlsWriteNumber($xlsRow,0,"$array[0]");
  52. xlsWriteLabel($xlsRow,1,"$array[1]");
  53. xlsWriteLabel($xlsRow,2,"$array[2]");
  54. xlsWriteLabel($xlsRow,3,"$array[3]");
  55. xlsWriteLabel($xlsRow,4,"$array[4]");
  56. $xlsRow++;
  57. }
  58. xlsEOF();
  59. exit();
  60. //20110629晚上测试通过。列名显示不出来?其它可以。
  61. ?>
复制代码


来源:php.cn
本站声明
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板