国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁(yè) > 開發(fā) > PHP > 正文

Codeigniter利用PHPExcel導(dǎo)出Excel文件

2024-05-04 21:48:07
字體:
來(lái)源:轉(zhuǎn)載
供稿:網(wǎng)友

1.準(zhǔn)備工作

下載PHPExcel:http://phpexcel.codeplex.com

這是個(gè)強(qiáng)大的Excel庫(kù),這里只演示導(dǎo)出Excel文件的功能,其中的大部分功能可能都用不著.

2. 安裝PHPExcel到Codeigniter

1) 解壓壓縮包里的Classes文件夾中的內(nèi)容到applicationlibraries目錄下,目錄結(jié)構(gòu)如下:

-- applicationlibrariesPHPExcel.php

-- applicationlibrariesPHPExcel (文件夾)

2)修改applicationlibrariesPHPExcelIOFactory.php 文件

-- 將其類名從PHPExcel_IOFactory改為IOFactory,遵從CI類命名規(guī)則.

-- 將其構(gòu)造函數(shù)改為public

3. 安裝完畢,寫一個(gè)導(dǎo)出excel的控制器(Controller),代碼如下:

  1. class Table_export extends CI_Controller { 
  2.   
  3.     function __construct() 
  4.     { 
  5.         parent::__construct(); 
  6.   
  7.         // Here you should add some sort of user validation 
  8.         // to prevent strangers from pulling your table data 
  9.     } 
  10.   
  11.     function index($table_name
  12.     { 
  13.         $query = $this->db->get($table_name); 
  14.   
  15.         if(!$query
  16.             return false; 
  17.   
  18.         // Starting the PHPExcel library 
  19.         $this->load->library('PHPExcel'); 
  20.         $this->load->library('PHPExcel/IOFactory'); 
  21.   
  22.         $objPHPExcel = new PHPExcel(); 
  23.         $objPHPExcel->getProperties()->setTitle("export")->setDescription("none"); 
  24.   
  25.         $objPHPExcel->setActiveSheetIndex(0); 
  26.   
  27.         // Field names in the first row 
  28.         $fields = $query->list_fields(); 
  29.         $col = 0; 
  30.         foreach ($fields as $field
  31.         { 
  32.             $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field); 
  33.             $col++; 
  34.         } 
  35.   
  36.         // Fetching the table data 
  37.         $row = 2; 
  38.         foreach($query->result() as $data
  39.         { 
  40.             $col = 0; 
  41.             foreach ($fields as $field
  42.             { 
  43.                 $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col$row$data->$field); 
  44.                 $col++; 
  45.             } 
  46.   
  47.             $row++; 
  48.         } 
  49.   
  50.         $objPHPExcel->setActiveSheetIndex(0); 
  51.   
  52.         $objWriter = IOFactory::createWriter($objPHPExcel'Excel5'); 
  53.   
  54.         // Sending headers to force the user to download the file 
  55.         header('Content-Type: application/vnd.ms-excel'); 
  56.         header('Content-Disposition: attachment;filename="Products_'.date('dMy').'.xls"'); 
  57.         header('Cache-Control: max-age=0'); 
  58.   
  59.         $objWriter->save('php://output'); 
  60.     } 
  61.   

方法二,代碼如下:

  1. Excel Plugin  
  2. The following plugin will generate a tab-delimited file, and feed it to the client as an Excel file. 
  3.  
  4. $this->load->plugin('to_excel'); 
  5. $this->db->use_table('tablename'); 
  6. $this->db->select('field1''field2'); 
  7. // run joins, order by, where, or anything else here 
  8. $query = $this->db->get(); 
  9. to_excel($query, ['filename']); // filename is optional, without it, the plugin will default to 'exceloutput' 
  10.  
  11. So you could run: 
  12.  
  13. to_excel($query'myfile'); // outputs myfile.xls 
  14. to_excel($query); // outputs exceloutput.xls 
  15. // you could also use a model here 
  16. to_excel($this->model_name->functioncall()); 
  17.  
  18. /system/plugins/to_excel_pi.php 
  19.  
  20. <?php if (!defined('BASEPATH')) exit('No direct script access allowed'); 
  21.  
  22. /* 
  23. * Excel library for Code Igniter applications 
  24. * Author: Derek Allard, Dark Horse Consulting, www.darkhorse.to, April 2006 
  25. */ 
  26.  
  27. function to_excel($query$filename='exceloutput'
  28.      $headers = ''// just creating the var for field headers to append to below 
  29.      $data = ''// just creating the var for field data to append to below 
  30.       
  31.      $obj =& get_instance(); 
  32.       
  33.      $fields = $query->field_data(); 
  34.      if ($query->num_rows() == 0) { 
  35.           echo '<p>The table appears to have no data.</p>'
  36.      } else { 
  37.           foreach ($fields as $field) { 
  38.              $headers .= $field->name . "t"
  39.           } 
  40.       
  41.           foreach ($query->result() as $row) { 
  42.                $line = ''
  43.                foreach($row as $value) {                                             
  44.                     if ((!isset($value)) OR ($value == "")) { 
  45.                          $value = "t"
  46.                     } else { 
  47.                          $value = str_replace('"''""'$value); 
  48.                          $value = '"' . $value . '"' . "t"
  49.                     } 
  50.                     $line .= $value
  51.                } 
  52.                $data .= trim($line)."n"
  53.           } 
  54.            
  55.           $data = str_replace("r","",$data); 
  56.                           
  57.           header("Content-type: application/x-msdownload"); 
  58.           header("Content-Disposition: attachment; filename=$filename.xls"); 
  59.           echo "$headersn$data";   
  60.      } 
  61. ?> 

發(fā)表評(píng)論 共有條評(píng)論
用戶名: 密碼:
驗(yàn)證碼: 匿名發(fā)表
主站蜘蛛池模板: 赤水市| 蓬溪县| 芒康县| 涡阳县| 望谟县| 万全县| 闸北区| 安顺市| 宝兴县| 枣强县| 临武县| 宁武县| 新宁县| 堆龙德庆县| 大兴区| 阜南县| 前郭尔| 鄱阳县| 上饶县| 宁德市| 莒南县| 离岛区| 大化| 楚雄市| 庆阳市| 荃湾区| 高要市| 正蓝旗| 香格里拉县| 太仆寺旗| 安塞县| 本溪市| 楚雄市| 健康| 连平县| 同仁县| 贵定县| 昌宁县| 富平县| 安吉县| 买车|