PHP Export data to Excel garbled solution

Source: Internet
Author: User
  1. /**

  2. * Export data to Excel to solve garbled problems
  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, $Row, $Col, 0x0);
  15. Echo Pack ("D", $Value);
  16. Return
  17. }

  18. function Xlswritelabel ($Row, $Col, $Value) {

  19. $Value = Iconv ("UTF-8", "gb2312", $Value); Add this statement to resolve the export Excel file garbled problem 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. File header

  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= Police Office Auxiliary Police statistics. xls");
  37. Header ("Content-disposition:inline;filename=\". $filename. ". Xls\" ");
  38. Iconv ("Utf-8", "gb2312", $filename);//Troubleshoot file-caused garbled ". Xls\" ");
  39. Header ("Content-transfer-encoding:binary");

  40. Adding data to a table

  41. Xlsbof ();
  42. Xlswritelabel (1, 0, "column name");
  43. Xlswritelabel (1, 1, "column name");
  44. Xlswritelabel (1, 2, "column name");
  45. Xlswritelabel (1, 3, "column name");
  46. Xlswritelabel (1, 4, "column name");
  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. Test can be used, the column name is not displayed, to be resolved
  61. ?>

Copy Code
  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    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.