PHP导出excel类完整实例程序

本文章来给各位同学详细介绍关于PHP导出excel类完整实例程序代码,这里我们使用了phpExcel插件,大家使用前先去下载一个phpExcel插件.

php exeel.class.php文件,代码如下:

  1. <?php
  2. /**
  3. * Simple excel generating from PHP5
  4. *
  5. * @package Utilities
  6. * @license http://www.opensource.org/licenses/mit-license.php
  7. * @author Oliver Schwarz <oliver.schwarz@gmail.com>
  8. * @version 1.0
  9. */
  10. /**
  11. * Generating excel documents on-the-fly from PHP5
  12. *
  13. * Uses the excel XML-specification to generate a native
  14. * XML document, readable/processable by excel.
  15. *
  16. * @package Utilities
  17. * @subpackage Excel
  18. * @author Oliver Schwarz <oliver.schwarz@vaicon.de>
  19. * @version 1.1
  20. *
  21. * @todo Issue #4: Internet Explorer 7 does not work well with the given header
  22. * @todo Add option to give out first line as header (bold text)
  23. * @todo Add option to give out last line as footer (bold text)
  24. * @todo Add option to write to file
  25. */
  26. class Excel_XML
  27. {
  28. /**
  29. * Header (of document)
  30. * @var string
  31. */
  32. private $header = "<?xml version="1.0" encoding="%s"?>n<Workbook xmlns="urn:schemas-microsoft-com:office:spreadsheet" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns:ss="urn:schemas-microsoft-com:office:spreadsheet" xmlns:html="http://www.w3.org/TR/REC-html40">";
  33. /**
  34. * Footer (of document)
  35. * @var string
  36. */
  37. private $footer = "</Workbook>";
  38. /**
  39. * Lines to output in the excel document
  40. * @var array
  41. */
  42. private $lines = array();
  43. /**
  44. * Used encoding
  45. * @var string
  46. */
  47. private $sEncoding;
  48. /**
  49. * Convert variable types
  50. * @var boolean
  51. */
  52. private $bConvertTypes;
  53. /**
  54. * Worksheet title
  55. * @var string
  56. */
  57. private $sWorksheetTitle;
  58. /**
  59. * Constructor
  60. *
  61. * The constructor allows the setting of some additional
  62. * parameters so that the library may be configured to
  63. * one's needs.
  64. *
  65. * On converting types:
  66. * When set to true, the library tries to identify the type of
  67. * the variable value and set the field specification for Excel
  68. * accordingly. Be careful with article numbers or postcodes
  69. * starting with a '0' (zero)!
  70. *
  71. * @param string $sEncoding Encoding to be used (defaults to UTF-8)
  72. * @param boolean $bConvertTypes Convert variables to field specification
  73. * @param string $sWorksheetTitle Title for the worksheet
  74. */
  75. public function __construct($sEncoding = 'UTF-8', $bConvertTypes = false, $sWorksheetTitle = 'Table1')
  76. {
  77. $this->bConvertTypes = $bConvertTypes;
  78. $this->setEncoding($sEncoding);
  79. $this->setWorksheetTitle($sWorksheetTitle);
  80. }
  81. /**
  82. * Set encoding
  83. * @param string Encoding type to set
  84. */
  85. public function setEncoding($sEncoding)
  86. {
  87. $this->sEncoding = $sEncoding;
  88. }
  89. /**
  90. * Set worksheet title
  91. *
  92. * Strips out not allowed characters and trims the
  93. * title to a maximum length of 31.
  94. *
  95. * @param string $title Title for worksheet
  96. */
  97. public function setWorksheetTitle ($title)
  98. {
  99. $title = preg_replace ("/[\|:|/|?|*|[|]]/", "", $title);
  100. $title = substr ($title, 0, 31);
  101. $this->sWorksheetTitle = $title;
  102. }
  103. /**
  104. * Add row
  105. *
  106. * Adds a single row to the document. If set to true, self::bConvertTypes
  107. * checks the type of variable and returns the specific field settings
  108. * for the cell.
  109. *
  110. * @param array $array One-dimensional array with row content
  111. */
  112. private function addRow ($array)
  113. {
  114. $cells = "";
  115. foreach ($array as $k => $v):
  116. $type = 'String';
  117. if ($this->bConvertTypes === true && is_numeric($v)):
  118. $type = 'Number';
  119. endif;
  120. $v = htmlentities($v, ENT_COMPAT, $this->sEncoding);
  121. $cells .= "<Cell><Data ss:Type="$type">" . $v . "</Data></Cell>n";
  122. endforeach;
  123. $this->lines[] = "<Row>n" . $cells . "</Row>n";
  124. }
  125. /**
  126. * Add an array to the document
  127. * @param array 2-dimensional array
  128. */
  129. public function addArray ($array)
  130. {
  131. foreach ($array as $k => $v)
  132. $this->addRow ($v);
  133. }
  134. /**
  135. * Generate the excel file
  136. * @param string $filename Name of excel file to generate (...xls)
  137. */
  138. public function generateXML ($filename = 'excel-export')
  139. {
  140. // correct/validate filename
  141. $filename = preg_replace('/[^aA-zZ0-9_-]/', '', $filename);
  142. // deliver header (as recommended in php manual)
  143. header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding);
  144. header("Content-Disposition: inline; filename="" . $filename . ".xls"");
  145. // print out document to the browser
  146. // need to use stripslashes for the damn ">"
  147. echo stripslashes (sprintf($this->header, $this->sEncoding));
  148. echo "n<Worksheet ss:Name="" . $this->sWorksheetTitle . "">n<Table>n";
  149. foreach ($this->lines as $line)
  150. echo $line;
  151. echo "</Table>n</Worksheet>n";
  152. echo $this->footer;
  153. }
  154. }
  155. ?>

excel.class.php文件,代码如下:

  1. <?php
  2. /*功能:导出excel类
  3. *时间:2012年8月16日
  4. *作者:565990136@qq.com
  5. */
  6. class myexcel{
  7. private $filelds_arr1=array();
  8. private $filelds_arr2=array();
  9. private $filename;
  10. // load library
  11. function __construct($fields_arr1,$fields_arr2,$filename='test'){
  12. $this->filelds_arr1=$fields_arr1;
  13. $this->filelds_arr2=$fields_arr2;
  14. $this->filename=$filename;
  15. }
  16. function putout(){
  17. require 'php-excel.class.php';
  18. $datavalue=$this->filelds_arr2;
  19. $newdata[0]=$this->filelds_arr1;
  20. $arrcount=count($datavalue);
  21. for($i=1;$i<=$arrcount;$i++){
  22. $newdata[$i]=array_values($datavalue[$i-1]);
  23. }
  24. $filename=$this->filename;
  25. $xls = new Excel_XML('UTF-8', false, 'My Test Sheet');
  26. $xls->addArray($newdata);
  27. $xls->generateXML($filename);
  28. }
  29. }
  30. /*
  31. **用法示例(注意导出的文件名只能用英文)
  32. $asdasd=new myexcel(array('姓名','电话'),array(array('贾新明','13521530320')),'abc');
  33. $asdasd->putout();
  34. */
  35. ?>

注意,我们把上面的代码分别保存成两个文件再进行操作.