php专区

 首页 > php专区 > PHP应用 > php类库 > PHP导出excel类完整实例程序 - php类库

PHP导出excel类完整实例程序 - php类库

分享到:
【字体:大 中 小】
导读:
          本文章来给各位同学详细介绍关于PHP导出excel类完整实例程序代码,这里我们使用了phpExcel插件,大家使用前先去下载一个phpExcel插件 php exeel class php文件,代码如下:?php ***Simpleexcelgenera...

PHP导出excel类完整实例程序

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

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

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

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

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

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

分享到:
php身份证验证相关类 - php类库
php身份证验证相关类  
php excel操作类phpExcel用法介绍 - php...
php excel操作类phpExcel用法介绍 phpExcel是php中一个excel插件操作类,可以很好的解决在excel各种操作,包括如,读,写,删除,插入等excel操作,下面笔者来给各位同学介绍介绍phpExcel用法吧. 下面是总结的几个使用方法,需要先将类进行包含进来,如下代码: include ‘PHPExcel.php’;  ...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……