当前位置:Gxlcms > PHP教程 > php导出excel表格综合示例

php导出excel表格综合示例

时间:2021-07-01 10:21:17 帮助过:16人阅读

  1. include("config.php");

  2. // load library
  3. require 'php-excel.class.php';
  4. // create a simple 2-dimensional 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', "书名"),//标题使用中文则到表格里面值为空,待解决
  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 to
  77. * one's needs.
  78. *
  79. * On converting types:
  80. * When set to true, the library tries to identify the type of
  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 the
  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-dimensional 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-dimensional 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 "\nsWorksheetTitle . "\">\n\n";
  163. foreach ($this->lines as $line)
  164. echo $line;

  165. echo "

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

人气教程排行