phpexcel导出excel的经典实例

WBOY
Freigeben: 2016-07-25 08:56:07
Original
963 Leute haben es durchsucht
  1. /**
  2. * phpexcel类库导出excel文件
  3. * edit: bbs.it-home.org
  4. *
  5. */
  6. require 'php-excel.class.php';//引用谷歌的phpexcel 类
  7. $result = mysql_query("SELECT * FROM dingdan"); //查询一个数据表,并返回记录集
  8. $data = array( 1 => array ('订单编号', '线路名称'),);
  9. while($row = mysql_fetch_array($result)){//把查询的结果循环输出到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, '财务报表');
  14. $xls->addArray($data);
  15. $xls->generateXML('2011010320');
  16. ?>
复制代码

附,phpexcel 类 php-excel.class.php 文件代码。

  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. /**
  15. * Lines to output in the excel document
  16. * @var array
  17. */
  18. private $lines = array();
  19. /**
  20. * Used encoding
  21. * @var string
  22. */
  23. private $sEncoding;
  24. /**
  25. * Convert variable types
  26. * @var boolean
  27. */
  28. private $bConvertTypes;
  29. /**
  30. * Worksheet title
  31. * @var string
  32. */
  33. private $sWorksheetTitle;
  34. /**
  35. * Constructor
  36. *
  37. * The constructor allows the setting of some additional
  38. * parameters so that the library may be configured to
  39. * one's needs.
  40. *
  41. * On converting types:
  42. * When set to true, the library tries to identify the type of
  43. * the variable value and set the field specification for Excel
  44. * accordingly. Be careful with article numbers or postcodes
  45. * starting with a '0' (zero)!
  46. *
  47. * @param string $sEncoding Encoding to be used (defaults to GBK)
  48. * @param boolean $bConvertTypes Convert variables to field specification
  49. * @param string $sWorksheetTitle Title for the worksheet
  50. */
  51. public function __construct($sEncoding = 'UTF-8', $bConvertTypes = false, $sWorksheetTitle = 'Table1')
  52. {
  53. $this->bConvertTypes = $bConvertTypes;
  54. $this->setEncoding($sEncoding);
  55. $this->setWorksheetTitle($sWorksheetTitle);
  56. }
  57. /**
  58. * Set encoding
  59. * @param string Encoding type to set
  60. */
  61. public function setEncoding($sEncoding)
  62. {
  63. $this->sEncoding = $sEncoding;
  64. }
  65. /**
  66. * Set worksheet title
  67. *
  68. * Strips out not allowed characters and trims the
  69. * title to a maximum length of 31.
  70. *
  71. * @param string $title Title for worksheet
  72. */
  73. public function setWorksheetTitle ($title)
  74. {
  75. $title = preg_replace ("/[///|:|//|/?|/*|/[|/]]/", "", $title);
  76. $title = substr ($title, 0, 31);
  77. $this->sWorksheetTitle = $title;
  78. }
  79. /**
  80. * Add row
  81. *
  82. * Adds a single row to the document. If set to true, self::bConvertTypes
  83. * checks the type of variable and returns the specific field settings
  84. * for the cell.
  85. *
  86. * @param array $array One-dimensional array with row content
  87. */
  88. private function addRow ($array)
  89. {
  90. $cells = "";
  91. foreach ($array as $k => $v):
  92. $type = 'String';
  93. if ($this->bConvertTypes === true && is_numeric($v)):
  94. $type = 'Number';
  95. endif;
  96. $v = htmlentities($v, ENT_COMPAT, $this->sEncoding);
  97. $cells .= "" . $v . "/n";
  98. endforeach;
  99. $this->lines[] = "/n" . $cells . "/n";
  100. }
  101. /**
  102. * Add an array to the document
  103. * @param array 2-dimensional array
  104. */
  105. public function addArray ($array)
  106. {
  107. foreach ($array as $k => $v)
  108. $this->addRow ($v);
  109. }
  110. /**

  111. * Generate the excel file
  112. * @param string $filename Name of excel file to generate (...xls)
  113. */
  114. public function generateXML ($filename = 'excel-export')
  115. {
  116. // correct/validate filename
  117. $filename = preg_replace('/[^aA-zZ0-9/_/-]/', '', $filename);
  118. // deliver header (as recommended in php manual)
  119. header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding);
  120. header("Content-Disposition: inline; filename=/"" . $filename . ".xls/"");
  121. // print out document to the browser
  122. // need to use stripslashes for the damn ">"
  123. echo stripslashes (sprintf($this->header, $this->sEncoding));
  124. echo "/nsWorksheetTitle . "/">/n/n";
  125. foreach ($this->lines as $line)
  126. echo $line;
  127. echo "
  128. /n
    /n";
  129. echo $this->footer;
  130. }
  131. }
  132. ?>
复制代码


Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Aktuelle Ausgaben
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage
Über uns Haftungsausschluss Sitemap
Chinesische PHP-Website:Online-PHP-Schulung für das Gemeinwohl,Helfen Sie PHP-Lernenden, sich schnell weiterzuentwickeln!