Php export data to excel file php export excel garbled problem

Source: Internet
Author: User
Php export data to excel file php export excel garbled problem

  1. /**
  2. * Export an excel file
  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); // add this statement to solve the problem of exporting garbled excel files 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. // 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; charsets = UTF-8 ");
  36. Header ("Content-Disposition: attachment?filename=auxiliary police force statistical table .xls ");
  37. // Header ("Content-Disposition: inline; filename = \" ". $ filename.". xls \"");
  38. // Iconv ("UTF-8", "gb2312", $ filename); // Fix the garbled characters caused by Files ". xls \"");
  39. Header ("Content-Transfer-Encoding: binary ");
  40. // Add data to the 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. // 20110629 the test passes in the evening. The column name cannot be displayed? Others.
  61. ?>

Related Article

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.