zoukankan      html  css  js  c++  java
  • 用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 = "	";
     24                     }
     25                     else
     26                     {
     27                         $value = str_replace('"', '""', $value);
     28                         $value = '"' . $value . '"' . "	";
     29                     }
     30                     $line .= $value;
     31                 }
     32                 $data .= trim($line) . "
    ";
     33             }
     34             $data = str_replace("
    ", "", $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 = "	";
     61                         }
     62                         else
     63                         {
     64                             $value = str_replace('"', '""', $value);
     65                             $value = '"' . $value . '"' . "	";
     66                         }
     67                         $line .= $value;
     68                     }
     69                     $data .= trim($line) . "
    ";
     70                 }
     71                 $data = str_replace("
    ", "", $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("	", $headers);
    112         }
    113     }
    114 
    115     private function generate($headers, $data)
    116     {
    117         $this->set_headers();
    118         echo "$headers
    $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);
    
    
  • 相关阅读:
    DBAccess
    业务耗时及数组
    QB资料学习.01
    格式化参数
    日志记录.02_线程处理
    nginx 开启gzip
    js 变量提升和函数提升
    js 深拷贝 vs 浅拷贝
    js 伪数组 转 数组
    js 对象原型和原型链
  • 原文地址:https://www.cnblogs.com/songlen/p/4468549.html
Copyright © 2011-2022 走看看