php匯出資料到excel出現亂碼的解決辦法

來源:互聯網
上載者:User
  1. /**

  2. * 匯出資料到excel 解決亂碼問題
  3. * Edit 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_addfrom 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. //測試能用,列名顯示不出來,有待解決
  61. ?>

複製代碼
  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.