Example of php excel table export

Source: Internet
Author: User
Tags php excel
Example of php excel table export

  1. Include ("config. php ");

  2. // Load library
  3. Require 'php-excel. class. php ';
  4. // Create a simple 2-dimen1_array
  5. // @ Mysql_query ("set character set gb2312 ");
  6. $ StrSql = "select * from booklist ";
  7. $ Result = mysql_query ($ strSql );

  8. // Print_r ($ result );

  9. $ Data = array (

  10. 1 => array ('id', "name of the book"), // if the title is in Chinese, the nominal value in the table is blank.
  11. );
  12. While ($ row = mysql_fetch_array ($ result ))
  13. {
  14. Array_push ($ data, array ($ row ['bookid'], $ row ['bookname']);
  15. }
  16. // Generate file (constructor parameters are optional)
  17. $ Xls = new Excel_XML ('utf-8', false, 'My Test Sheet ');
  18. $ Xls-> addArray ($ data );
  19. $ Xls-> generateXML ('My-test ');
  20. ?>

  21. /**
  22. * Simple excel generating from PHP5
  23. *
  24. * @ Version 1.0
  25. */

  26. /**

  27. * Generating excel documents on-the-fly from PHP5
  28. *
  29. * Uses the excel XML-specification to generate a native
  30. * XML document, readable/processable by excel.
  31. *
  32. * @ Package Utilities
  33. * @ Version 1.1
  34. *
  35. * @ Todo Issue #4: Internet Explorer 7 does not work well with the given header
  36. * @ Todo Add option to give out first line as header (bold text)
  37. * @ Todo Add option to give out last line as footer (bold text)
  38. * @ Todo Add option to write to file
  39. */
  40. Class Excel_XML
  41. {

  42. /**

  43. * Header (of document)
  44. * @ Var string
  45. */
  46. Private $ header =" \ N ";

  47. /**

  48. * Footer (of document)
  49. * @ Var string
  50. */
  51. Private $ footer ="";

  52. /**

  53. * Lines to output in the excel document
  54. * @ Var array
  55. */
  56. Private $ lines = array ();

  57. /**

  58. * Used encoding
  59. * @ Var string
  60. */
  61. Private $ sEncoding;

  62. /**

  63. * Convert variable types
  64. * @ Var boolean
  65. */
  66. Private $ bConvertTypes;

  67. /**

  68. * Worksheet title
  69. * @ Var string
  70. */
  71. Private $ sWorksheetTitle;

  72. /**

  73. * Constructor
  74. *
  75. * The constructor allows the setting of some additional
  76. * Parameters so that the library may be configured
  77. * One's needs.
  78. *
  79. * On converting types:
  80. * When set to true, the library tries to identify the type
  81. * The variable value and set the field specification for Excel
  82. * Accordingly. Be careful with article numbers or postcodes
  83. * Starting with a '0' (zero )!
  84. *
  85. * @ Param string $ sEncoding Encoding to be used (defaults to UTF-8)
  86. * @ Param boolean $ bConvertTypes Convert variables to field specification
  87. * @ Param string $ sWorksheetTitle Title for the worksheet
  88. */
  89. Public function _ construct ($ sEncoding = 'utf-8', $ bConvertTypes = false, $ sWorksheetTitle = 'table1 ')
  90. {
  91. $ This-> bConvertTypes = $ bConvertTypes;
  92. $ This-> setEncoding ($ sEncoding );
  93. $ This-> setWorksheetTitle ($ sWorksheetTitle );
  94. }

  95. /**

  96. * Set encoding
  97. * @ Param string Encoding type to set
  98. */
  99. Public function setEncoding ($ sEncoding)
  100. {
  101. $ This-> sEncoding = $ sEncoding;
  102. }

  103. /**

  104. * Set worksheet title
  105. *
  106. * Strips out not allowed characters and trims
  107. * Title to a maximum length of 31.
  108. *
  109. * @ Param string $ title Title for worksheet
  110. */
  111. Public function setWorksheetTitle ($ title)
  112. {
  113. $ Title = preg_replace ("/[\\|:\/ | \? | \ * | \ [| \]/"," ", $ Title );
  114. $ Title = substr ($ title, 0, 31 );
  115. $ This-> sWorksheetTitle = $ title;
  116. }

  117. /**

  118. * Add row
  119. *
  120. * Adds a single row to the document. If set to true, self: bConvertTypes
  121. * Checks the type of variable and returns the specific field settings
  122. * For the cell.
  123. *
  124. * @ Param array $ array One-dimen1_array with row content
  125. */
  126. Private function addRow ($ array)
  127. {
  128. $ Cells = "";
  129. Foreach ($ array as $ k => $ v ):
  130. $ Type = 'string ';
  131. If ($ this-> bConvertTypes === true & is_numeric ($ v )):
  132. $ Type = 'number ';
  133. Endif;
  134. $ V = htmlentities ($ v, ENT_COMPAT, $ this-> sEncoding );
  135. $ Cells. =" ". $ V ." \ N ";
  136. Endforeach;
  137. $ This-> lines [] =" \ N ". $ cells ." \ N ";
  138. }

  139. /**

  140. * Add an array to the document
  141. * @ Param array 2-dimen1_array
  142. */
  143. Public function addArray ($ array)
  144. {
  145. Foreach ($ array as $ k => $ v)
  146. $ This-> addRow ($ v );
  147. }

  148. /**
  149. * Generate the excel file
  150. * @ Param string $ filename Name of excel file to generate (...xls)
  151. */
  152. Public function generateXML ($ filename = 'Excel-export ')
  153. {
  154. // Correct/validate filename
  155. $ Filename = preg_replace ('/[^ aA-zZ0-9 \ _ \-]/', '', $ filename );

  156. // Deliver header (as recommended in php manual)

  157. Header ("Content-Type: application/vnd. ms-excel; charset =". $ this-> sEncoding );
  158. Header ("Content-Disposition: inline; filename = \" ". $ filename.". xls \"");

  159. // Print out document to the browser

  160. // Need to use stripslashes for the damn ">"
  161. Echo stripslashes (sprintf ($ this-> header, $ this-> sEncoding ));
  162. Echo "\ n SWorksheetTitle. "\"> \ n
  163. Foreach ($ this-> lines as $ line)
  164. Echo $ line;

  165. Echo"

  166. \ N ";
    \ N \ N ";
  167. Echo $ this-> footer;
  168. }
  169. }
  170. ?>

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.