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.   * Header (of document) 
  31.   * @var string 
  32.   */ 
  33.         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">"; 
  34.  
  35.         /** 
  36.          * Footer (of document) 
  37.          * @var string 
  38.          */ 
  39.         private $footer = ""
  40.  
  41.         /** 
  42.          * Lines to output in the excel document 
  43.          * @var array 
  44.          */ 
  45.         private $lines = array(); 
  46.  
  47.         /** 
  48.          * Used encoding 
  49.          * @var string 
  50.          */ 
  51.         private $sEncoding
  52.          
  53.         /** 
  54.          * Convert variable types 
  55.          * @var boolean 
  56.          */ 
  57.         private $bConvertTypes
  58.          
  59.         /** 
  60.          * Worksheet title 
  61.          * @var string 
  62.          */ 
  63.         private $sWorksheetTitle
  64.  
  65.         /** 
  66.          * Constructor 
  67.          *  
  68.          * The constructor allows the setting of some additional 
  69.          * parameters so that the library may be configured to 
  70.          * one's needs. 
  71.          *  
  72.          * On converting types: 
  73.          * When set to true, the library tries to identify the type of 
  74.          * the variable value and set the field specification for Excel 
  75.          * accordingly. Be careful with article numbers or postcodes 
  76.          * starting with a '0' (zero)! 
  77.          *  
  78.          * @param string $sEncoding Encoding to be used (defaults to UTF-8) 
  79.          * @param boolean $bConvertTypes Convert variables to field specification 
  80.          * @param string $sWorksheetTitle Title for the worksheet 
  81.          */ 
  82.         public function __construct($sEncoding = 'UTF-8'$bConvertTypes = false, $sWorksheetTitle = 'Table1'
  83.         { 
  84.                 $this->bConvertTypes = $bConvertTypes
  85.          $this->setEncoding($sEncoding); 
  86.          $this->setWorksheetTitle($sWorksheetTitle); 
  87.         } 
  88.          
  89.         /** 
  90.          * Set encoding 
  91.          * @param string Encoding type to set 
  92.          */ 
  93.         public function setEncoding($sEncoding
  94.         { 
  95.          $this->sEncoding = $sEncoding
  96.         } 
  97.  
  98.         /** 
  99.          * Set worksheet title 
  100.          *  
  101.          * Strips out not allowed characters and trims the 
  102.          * title to a maximum length of 31. 
  103.          *  
  104.          * @param string $title Title for worksheet 
  105.          */ 
  106.         public function setWorksheetTitle ($title
  107.         { 
  108.                 $title = preg_replace ("/[|:|/|?|*|[|]]/"""$title); 
  109.                 $title = substr ($title, 0, 31); 
  110.                 $this->sWorksheetTitle = $title
  111.         } 
  112.  
  113.         /** 
  114.          * Add row 
  115.          *  
  116.          * Adds a single row to the document. If set to true, self::bConvertTypes 
  117.          * checks the type of variable and returns the specific field settings 
  118.          * for the cell. 
  119.          *  
  120.          * @param array $array One-dimensional array with row content 
  121.          */ 
  122.         private function addRow ($array
  123.         { 
  124.          $cells = ""
  125.                 foreach ($array as $k => $v): 
  126.                         $type = 'String'
  127.                         if ($this->bConvertTypes === true && is_numeric($v)): 
  128.                                 $type = 'Number'
  129.                         endif
  130.                         $v = htmlentities($v, ENT_COMPAT, $this->sEncoding); 
  131.                         $cells .= "$type">" . $v . "n";  
  132.                 endforeach
  133.                 $this->lines[] = "n" . $cells . "n"
  134.         } 
  135.  
  136.         /** 
  137.          * Add an array to the document 
  138.          * @param array 2-dimensional array 
  139.          */ 
  140.         public function addArray ($array
  141.         { 
  142.                 foreach ($array as $k => $v
  143.                         $this->addRow ($v); 
  144.         } 
  145.  
  146.  
  147.         /** 
  148.          * Generate the excel file 
  149.          * @param string $filename Name of excel file to generate (...xls) 
  150.          */ 
  151.         public function generateXML ($filename = 'excel-export'
  152.         { 
  153.                 // correct/validate filename 
  154.                 $filename = preg_replace('/[^aA-zZ0-9_-]/'''$filename); 
  155.       
  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 "n" . $this->sWorksheetTitle . "">nn"
  163.                 foreach ($this->lines as $line
  164.                         echo $line
  165.  
  166.                 echo "
  167. nn"
  168.                 echo $this->footer; 
  169.         } 
  170.  
  171. ?> 

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. $filename=$this->filename; 
  26. $xls = new Excel_XML('UTF-8', false, 'My Test Sheet'); 
  27. $xls->addArray($newdata); 
  28. $xls->generateXML($filename); 
  29.  
  30. /* 
  31. **用法示例(注意导出的文件名只能用英文) 
  32.  
  33.   $asdasd=new myexcel(array('姓名','电话'),array(array('贾新明','13521530320')),'abc'); 
  34.   $asdasd->putout(); 
  35. */ 
  36. ?> 

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

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