├── README.md └── application └── libraries └── Excel.php /README.md: -------------------------------------------------------------------------------- 1 | Codeigniter-Excel-Export 2 | ======================== 3 | 4 | An Codeigniter library for exporting the data from database to Excel file. 5 | 6 | 一个用于导出数据库数据到Excel文件的PHP库,用于Codeigniter框架中。(支持Excel中包含中文文字) 7 | 8 | 使用方法: 9 | ======================== 10 | 11 | 1. 复制Excel.php到相应目录(application/libraries目录下) 12 | ------------------------ 13 | 14 | 2. 创建数据结果集 15 | ------------------------ 16 | 17 | load->library('excel'); 20 | 21 | $sql = $this->db->get('dbtable'); 22 | 23 | $this->excel->filename = 'abc123'; 24 | $this->excel->make_from_db($sql); 25 | } 26 | ?> 27 | 28 | 3. 导出到Excel文 29 | -------------------------- 30 | 31 | excel->make_from_array($titles, $array); 41 | } 42 | ?> 43 | 44 | 参考项目 45 | ========================= 46 | 47 | https://github.com/EllisLab/CodeIgniter/wiki/Excel-Plugin 48 | 49 | https://github.com/JOakley77/CI-Excel-Generation-Library 50 | 51 | 52 | 53 | 54 | -------------------------------------------------------------------------------- /application/libraries/Excel.php: -------------------------------------------------------------------------------- 1 | field_data(); 11 | 12 | if ($db_results->num_rows() == 0) { 13 | show_error('The table appears to have no data'); 14 | } 15 | else { 16 | $headers = $this->titles($fields); 17 | 18 | foreach ($db_results->result() AS $row) { 19 | $line = ''; 20 | foreach ($row AS $value) { 21 | if (!isset($value) OR $value == '') { 22 | $value = "\t"; 23 | } 24 | else { 25 | $value = str_replace('"', '""', $value); 26 | $value = '"' . $value . '"' . "\t"; 27 | } 28 | $line .= $value; 29 | } 30 | $data .= trim($line) . "\n"; 31 | } 32 | $data = str_replace("\r", "", $data); 33 | 34 | $this->generate($headers, $data); 35 | } 36 | } 37 | 38 | public function make_from_array($titles, $array) { 39 | $data = NULL; 40 | 41 | if (!is_array($array)) { 42 | show_error('The data supplied is not a valid array'); 43 | } 44 | else { 45 | $headers = $this->titles($titles); 46 | 47 | if (is_array($array)) { 48 | foreach ($array AS $row) { 49 | $line = ''; 50 | foreach ($row AS $value) { 51 | if (!isset($value) OR $value == '') { 52 | $value = "\t"; 53 | } 54 | else { 55 | $value = str_replace('"', '""', $value); 56 | $value = '"' . $value . '"' . "\t"; 57 | } 58 | $line .= $value; 59 | } 60 | $data .= iconv( "UTF-8", "GB18030//IGNORE", trim($line)) . "\n"; 61 | } 62 | $data = str_replace("\r", "", $data); 63 | 64 | $this->generate($headers, $data); 65 | } 66 | } 67 | } 68 | 69 | private function generate($headers, $data) { 70 | $this->set_headers(); 71 | 72 | echo "$headers\n$data"; 73 | } 74 | 75 | public function titles($titles) { 76 | if (is_array($titles)) { 77 | $headers = array(); 78 | 79 | if (is_null($this->custom_titles)) { 80 | if (is_array($titles)) { 81 | foreach ($titles AS $title) { 82 | $headers[] = iconv( "UTF-8", "GB18030//IGNORE", $title); 83 | } 84 | } 85 | else { 86 | foreach ($titles AS $title) { 87 | $headers[] = iconv( "UTF-8", "GB18030//IGNORE", $title->name); 88 | } 89 | } 90 | } 91 | else { 92 | $keys = array(); 93 | foreach ($titles AS $title) { 94 | $keys[] = iconv( "UTF-8", "GB18030//IGNORE", $title->name); 95 | } 96 | foreach ($keys AS $key) { 97 | $headers[] = iconv( "UTF-8", "GB18030//IGNORE", $this->custom_titles[array_search($key, $keys)]); 98 | } 99 | } 100 | 101 | return implode("\t", $headers); 102 | } 103 | } 104 | 105 | private function set_headers() { 106 | $ua = $_SERVER["HTTP_USER_AGENT"]; 107 | $filename = $this->filename . ".xls"; 108 | $encoded_filename = urlencode($filename); 109 | $encoded_filename = str_replace("+", "%20", $encoded_filename); 110 | //ob_end_clean(); 111 | header("Pragma: public"); 112 | header("Expires: 0"); 113 | header("Cache-Control: must-revalidate, post-check=0, pre-check=0"); 114 | header("Content-Type: application/vnd.ms-excel; charset=UTF-8"); 115 | header("Content-Type: application/force-download"); 116 | header("Content-Type: application/octet-stream"); 117 | //header("Content-Type: application/vnd.ms-excel;charset=UTF-8"); 118 | header("Content-Type: application/download");; 119 | if (preg_match("/MSIE/", $ua)) { 120 | header('Content-Disposition: attachment; filename="' . $encoded_filename . '"'); 121 | } else if (preg_match("/Firefox/", $ua)) { 122 | header('Content-Disposition: attachment; filename*="utf8\'\'' . $filename . '"'); 123 | } else { 124 | header('Content-Disposition: attachment; filename="' . $filename . '"'); 125 | } 126 | header("Content-Transfer-Encoding: binary "); 127 | } 128 | } --------------------------------------------------------------------------------