Export data from php to excel

Source: Internet
Author: User
Export data from php to excel
Export data to excel.

  1. /**
  2. * Export to an excel file (generally Chinese characters are exported with garbled characters and need to be encoded and converted)
  3. * The usage is as follows:
  4. * $ Excel = new Excel ();
  5. * $ Excel-> addHeader (array ('column 1', 'column 2', 'column 3', 'column 4 '));
  6. * $ Excel-> addBody (
  7. Array (
  8. Array ('data 1', 'data 2', 'data 3', 'data 4 '),
  9. Array ('data 1', 'data 2', 'data 3', 'data 4 '),
  10. Array ('data 1', 'data 2', 'data 3', 'data 4 '),
  11. Array ('data 1', 'data 2', 'data 3', 'data 4 ')
  12. )
  13. );
  14. * $ Excel-> downLoad ();
  15. */
  16. Class Excel {
  17. Private $ head;
  18. Private $ body;
  19. /**
  20. *
  21. * @ Param type $ arr one-dimensional array
  22. */
  23. Public function addHeader ($ arr ){
  24. Foreach ($ arr as $ headVal ){
  25. $ HeadVal = $ this-> charset ($ headVal );
  26. $ This-> head. = "{$ headVal} \ t ";
  27. }
  28. $ This-> head. = "\ n ";
  29. }
  30. /**
  31. *
  32. * @ Param type $ arr two-dimensional array
  33. */
  34. Public function addBody ($ arr ){
  35. Foreach ($ arr as $ arrBody ){
  36. Foreach ($ arrBody as $ bodyVal ){
  37. $ BodyVal = $ this-> charset ($ bodyVal );
  38. $ This-> body. = "{$ bodyVal} \ t ";
  39. }
  40. $ This-> body. = "\ n ";
  41. }
  42. }
  43. /**
  44. * Download an excel file
  45. */
  46. Public function downLoad ($ filename = ''){
  47. If (! $ Filename)
  48. $ Filename = date('ymdhis', time({}.'.xls ';
  49. Header ("Content-type: application/vnd. ms-excel ");
  50. Header ("Content-Disposition: attachment; filename = $ filename ");
  51. Header ("Content-Type: charset = gb2312 ");
  52. If ($ this-> head)
  53. Echo $ this-> head;
  54. Echo $ this-> body;
  55. }
  56. /**
  57. * Encoding conversion
  58. * @ Param type $ string
  59. * @ Return string
  60. */
  61. Public function charset ($ string ){
  62. Return iconv ("UTF-8", "gb2312", $ string );
  63. }
  64. }
  65. ?>

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.