首页 > 语言 > PHP > 正文

PHP导出excel类完整实例程序

2024-09-04 11:44:03
字体:大 中 小
来源:转载
供稿:网友

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

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

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

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.  
  19. $datavalue=$this->filelds_arr2; 
  20. $newdata[0]=$this->filelds_arr1; 
  21. $arrcount=count($datavalue); 
  22. for($i=1;$i<=$arrcount;$i++){ 
  23. $newdata[$i]=array_values($datavalue[$i-1]); 
  24. } 
  25.  
  26.  
  27. $filename=$this->filename; 
  28. $xls = new Excel_XML('UTF-8', false, 'My Test Sheet'); 
  29. $xls->addArray($newdata); 
  30. $xls->generateXML($filename); 
  31. } 
  32. } 
  33.  
  34. /* 
  35. **用法示例(注意导出的文件名只能用英文) 
  36.  
  37.   $asdasd=new myexcel(array('姓名','电话'),array(array('贾新明','13521530320')),'abc'); 
  38.   $asdasd->putout(); 
  39. */ 
  40. ?> 

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

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表