用excel.php类库导出excel文件

excel.php是个小型的php类库,可以满足基本的从数据库中取出数据然后导出xls格式的excel文件,代码如下:

  1 class Excel {
  2     public $filename = ‘excel‘;
  3     public $custom_titles;
  4 
  5     public function make_from_db($db_results)
  6     {
  7         $data         = NULL;
  8         $fields     = $db_results->field_data();
  9         if ($db_results->num_rows() == 0)
 10         {
 11             show_error(‘The table appears to have no data‘);
 12         }
 13         else
 14         {
 15             $headers = $this->titles($fields);
 16             foreach ($db_results->result() AS $row)
 17             {
 18                 $line = ‘‘;
 19                 foreach ($row AS $value)
 20                 {
 21                     if (!isset($value) OR $value == ‘‘)
 22                     {
 23                         $value = "\t";
 24                     }
 25                     else
 26                     {
 27                         $value = str_replace(‘"‘, ‘""‘, $value);
 28                         $value = ‘"‘ . $value . ‘"‘ . "\t";
 29                     }
 30                     $line .= $value;
 31                 }
 32                 $data .= trim($line) . "\n";
 33             }
 34             $data = str_replace("\r", "", $data);
 35             $this->generate($headers, $data);
 36         }
 37     }
 38 
 39     public function make_from_array($titles, $array, $filename = ‘excel‘)
 40     {
 41         $data = NULL;
 42         $this->filename = $filename;
 43 
 44         if ( ! is_array($array))
 45         {
 46             show_error(‘The data supplied is not a valid array‘);
 47         }
 48         else
 49         {
 50             $headers = $this->titles($titles);
 51             if (is_array($array))
 52             {
 53                 foreach ($array AS $row)
 54                 {
 55                     $line = ‘‘;
 56                     foreach ($row AS $value)
 57                     {
 58                         if (!isset($value) OR $value == ‘‘)
 59                         {
 60                             $value = "\t";
 61                         }
 62                         else
 63                         {
 64                             $value = str_replace(‘"‘, ‘""‘, $value);
 65                             $value = ‘"‘ . $value . ‘"‘ . "\t";
 66                         }
 67                         $line .= $value;
 68                     }
 69                     $data .= trim($line) . "\n";
 70                 }
 71                 $data = str_replace("\r", "", $data);
 72                 $this->generate($headers, $data);
 73             }
 74         }
 75     }
 76 
 77     public function titles($titles)
 78     {
 79         if (is_array($titles))
 80         {
 81             $headers = array();
 82             if (is_null($this->custom_titles))
 83             {
 84                 if (is_array($titles))
 85                 {
 86                     foreach ($titles AS $title)
 87                     {
 88                         $headers[] = $title;
 89                     }
 90                 }
 91                 else
 92                 {
 93                     foreach ($titles AS $title)
 94                     {
 95                         $headers[] = $title->name;
 96                     }
 97                 }
 98             }
 99             else
100             {
101                 $keys = array();
102                 foreach ($titles AS $title)
103                 {
104                     $keys[] = $title->name;
105                 }
106                 foreach ($keys AS $key)
107                 {
108                     $headers[] = $this->custom_titles[array_search($key, $keys)];
109                 }
110             }
111             return implode("\t", $headers);
112         }
113     }
114 
115     private function generate($headers, $data)
116     {
117         $this->set_headers();
118         echo "$headers\n$data";
119     }
120 
121     private function set_headers()
122     {
123         header("Pragma: public");
124         header("Expires: 0");
125         header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
126         header("Content-Type: application/force-download");
127         header("Content-Type: application/octet-stream");
128         header("Content-Type: application/download");;
129         header("Content-Disposition: attachment; filename=$this->filename.xls");
130         header("Content-Transfer-Encoding: binary ");
131     }
132 }

这里用其中的一种方法导出xls文件:

$titles = array(‘姓名‘, ‘年龄‘, ‘性别‘, ‘民族‘); //设置表格的头部名称
$array = array(
    array(‘张三‘, ‘18‘, ‘男‘, ‘汉族‘),
    array(‘李四‘, ‘19‘, ‘男‘, ‘汉族‘),
    array(‘王五‘, ‘18‘, ‘男‘, ‘汉族‘),
);
//下面把执行这个方法就行了
make_from_array($titles, $array, $filename);

 

郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。