Export a classic excel instance using phpexcel

Source: Internet
Author: User
Tags php excel
Export a classic excel instance using phpexcel

  1. /**
  2. * Export an excel file using the phpexcel class library
  3. * Edit: bbs.it-home.org
  4. *
  5. */
  6. Require 'php-excel. class. php'; // reference Google's phpexcel class
  7. $ Result = mysql_query ("SELECT * FROM dingdan"); // query a data table and return the record set
  8. $ Data = array (1 => array ('Order number', 'line name '),);
  9. While ($ row = mysql_fetch_array ($ result) {// cyclically output the query results to EXCEL
  10. Array_push ($ data, array ($ row ["dd_bh"], $ row ["dd_xianlu_name"]);
  11. }
  12. // Generate file (constructor parameters are optional)
  13. $ Xls = new Excel_XML ('gb2312', false, 'financial report ');
  14. $ Xls-> addArray ($ data );
  15. $ Xls-> generateXML ('20140901 ');
  16. ?>

Appendix, php Excel class php-excel.class.php file code.

  1. Class Excel_XML

  2. {
  3. /**
  4. * Header (of document)
  5. * @ Var string
  6. */
  7. Private $ header =" /N ";
  8. /**
  9. * Footer (of document)
  10. * @ Var string
  11. */
  12. Private $ footer ="";
  13. /**
  14. * Lines to output in the excel document
  15. * @ Var array
  16. */
  17. Private $ lines = array ();
  18. /**
  19. * Used encoding
  20. * @ Var string
  21. */
  22. Private $ sEncoding;
  23. /**
  24. * Convert variable types
  25. * @ Var boolean
  26. */
  27. Private $ bConvertTypes;
  28. /**
  29. * Worksheet title
  30. * @ Var string
  31. */
  32. Private $ sWorksheetTitle;
  33. /**
  34. * Constructor
  35. *
  36. * The constructor allows the setting of some additional
  37. * Parameters so that the library may be configured
  38. * One's needs.
  39. *
  40. * On converting types:
  41. * When set to true, the library tries to identify the type
  42. * The variable value and set the field specification for Excel
  43. * Accordingly. Be careful with article numbers or postcodes
  44. * Starting with a '0' (zero )!
  45. *
  46. * @ Param string $ sEncoding Encoding to be used (defaults to GBK)
  47. * @ Param boolean $ bConvertTypes Convert variables to field specification
  48. * @ Param string $ sWorksheetTitle Title for the worksheet
  49. */
  50. Public function _ construct ($ sEncoding = 'utf-8', $ bConvertTypes = false, $ sWorksheetTitle = 'table1 ')
  51. {
  52. $ This-> bConvertTypes = $ bConvertTypes;
  53. $ This-> setEncoding ($ sEncoding );
  54. $ This-> setWorksheetTitle ($ sWorksheetTitle );
  55. }
  56. /**
  57. * Set encoding
  58. * @ Param string Encoding type to set
  59. */
  60. Public function setEncoding ($ sEncoding)
  61. {
  62. $ This-> sEncoding = $ sEncoding;
  63. }
  64. /**
  65. * Set worksheet title
  66. *
  67. * Strips out not allowed characters and trims
  68. * Title to a maximum length of 31.
  69. *
  70. * @ Param string $ title Title for worksheet
  71. */
  72. Public function setWorksheetTitle ($ title)
  73. {
  74. $ Title = preg_replace ("/[// |: |/// | /? |/* |/[|/]/"," ", $ Title );
  75. $ Title = substr ($ title, 0, 31 );
  76. $ This-> sWorksheetTitle = $ title;
  77. }
  78. /**
  79. * Add row
  80. *
  81. * Adds a single row to the document. If set to true, self: bConvertTypes
  82. * Checks the type of variable and returns the specific field settings
  83. * For the cell.
  84. *
  85. * @ Param array $ array One-dimen1_array with row content
  86. */
  87. Private function addRow ($ array)
  88. {
  89. $ Cells = "";
  90. Foreach ($ array as $ k => $ v ):
  91. $ Type = 'string ';
  92. If ($ this-> bConvertTypes === true & is_numeric ($ v )):
  93. $ Type = 'number ';
  94. Endif;
  95. $ V = htmlentities ($ v, ENT_COMPAT, $ this-> sEncoding );
  96. $ Cells. =" ". $ V ." /N ";
  97. Endforeach;
  98. $ This-> lines [] =" /N ". $ cells ." /N ";
  99. }
  100. /**
  101. * Add an array to the document
  102. * @ Param array 2-dimen1_array
  103. */
  104. Public function addArray ($ array)
  105. {
  106. Foreach ($ array as $ k => $ v)
  107. $ This-> addRow ($ v );
  108. }

  109. /**

  110. * Generate the excel file
  111. * @ Param string $ filename Name of excel file to generate (...xls)
  112. */
  113. Public function generateXML ($ filename = 'Excel-export ')
  114. {
  115. // Correct/validate filename
  116. $ Filename = preg_replace ('/[^ aA-zZ0-9/_/-]/', '', $ filename );
  117. // Deliver header (as recommended in php manual)
  118. Header ("Content-Type: application/vnd. ms-excel; charset =". $ this-> sEncoding );
  119. Header ("Content-Disposition: inline; filename =/" ". $ filename.". xls /"");
  120. // Print out document to the browser
  121. // Need to use stripslashes for the damn ">"
  122. Echo stripslashes (sprintf ($ this-> header, $ this-> sEncoding ));
  123. Echo "/n SWorksheetTitle. "/">/n
  124. Foreach ($ this-> lines as $ line)
  125. Echo $ line;
  126. Echo"
  127. /N ";
    /N /N ";
  128. Echo $ this-> footer;
  129. }
  130. }
  131. ?>

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.