一个导出excel的类(收集)

  网上看到了一个挺好的php的excel导出类,现在跟大家分享一下:

 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 class Excel_Xml
13 {
14  
15         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\">";
16  
17         private $footer = "</Workbook>";
18  
19         private $lines = array();
20  
21         private $sEncoding;
22  
23         private $bConvertTypes;
24  
25         private $sWorksheetTitle;
26  
27  
28         public function __construct($sEncoding = 'UTF-8', $bConvertTypes = false, $sWorksheetTitle = 'Table1')
29         {
30             $this->bConvertTypes = $bConvertTypes;
31             $this->setEncoding($sEncoding);
32             $this->setWorksheetTitle($sWorksheetTitle);
33         }
34  
35         public function setEncoding($sEncoding)
36         {
37             $this->sEncoding = $sEncoding;
38         }
39  
40         public function setWorksheetTitle ($title)
41         {
42                 $title = preg_replace ("/[\\\|:|\/|\?|\*|\[|\]]/", "", $title);
43                 $title = substr ($title, 0, 31);
44                 $this->sWorksheetTitle = $title;
45         }
46  
47         private function addRow ($array)
48         {
49             $cells = "";
50                 foreach ($array as $k => $v):
51                         $type = 'String';
52                         if ($this->bConvertTypes === true && is_numeric($v)):
53                                 $type = 'Number';
54                         endif;
55                         $v = htmlentities($v, ENT_COMPAT, $this->sEncoding);
56                         $cells .= "<Cell><Data ss:Type=\"$type\">" . $v . "</Data></Cell>\n";
57                 endforeach;
58                 $this->lines[] = "<Row>\n" . $cells . "</Row>\n";
59         }
60  
61  
62         public function addArray ($array)
63         {
64                 foreach ($array as $k => $v)
65                         $this->addRow ($v);
66         }
67  
68  
69         public function generateXML ($filename = 'excel-export')
70         {
71                 $filename = preg_replace('/[^aA-zZ0-9\_\-]/', '', $filename);
72                 header("Content-Type: application/vnd.ms-excel; charset=" . $this->sEncoding);
73                 header("Content-Disposition: inline; filename=\"" . $filename . ".xls\"");
74                 echo stripslashes (sprintf($this->header, $this->sEncoding));
75                 echo "\n<Worksheet ss:Name=\"" . $this->sWorksheetTitle . "\">\n<Table>\n";
76                 foreach ($this->lines as $line)
77                         echo $line;
78  
79                 echo "</Table>\n</Worksheet>\n";
80                 echo $this->footer;
81         }
82  
83 }

  用法示意 demo.php

 1 <?php
 2 
 3 require_once 'simpleexcel.class.php';
 4 $xls = new Excel_Xml('UTF-8',false,'测试');
 5 $data = array(
 6     1 => array('名称','地址'),
 7     2 => array('php','www.php.net'),
 8     3 => array('百度','www.baidu.com')
 9 );
10 $xls->addArray($data);
11 $xls->generateXML('name4test');

 

posted @ 2016-09-05 10:08  NickBai  阅读(320)  评论(0编辑  收藏  举报