php专区

 首页 > php专区 > PHP应用 > 常用功能 > Codeigniter利用PHPExcel导出Excel文件 - php高级应用

Codeigniter利用PHPExcel导出Excel文件 - php高级应用

分享到:
【字体:
导读:
          1 准备工作下载PHPExcel:http: phpexcel codeplex com这是个强大的Excel库,这里只演示导出Excel文件的功能,其中的大部分功能可能都用不着 2 安装PHPExcel到Codeigniter1) 解压压缩包里的Clas...

Codeigniter利用PHPExcel导出Excel文件

1.准备工作

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

这是个强大的Excel库,这里只演示导出Excel文件的功能,其中的大部分功能可能都用不着.

2. 安装PHPExcel到Codeigniter

1) 解压压缩包里的Classes文件夹中的内容到applicationlibraries目录下,目录结构如下:

-- applicationlibrariesPHPExcel.php

-- applicationlibrariesPHPExcel (文件夹)

2)修改applicationlibrariesPHPExcelIOFactory.php 文件

-- 将其类名从PHPExcel_IOFactory改为IOFactory,遵从CI类命名规则.

-- 将其构造函数改为public

3. 安装完毕,写一个导出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. 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 '

    The table appears to have no data.

    '
  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. ?> 
分享到:
QQ登录集成到自己网站php代码 - php高级...
QQ登录集成到自己网站php代码 我们现在在各大网站论坛都可以看到点击一个QQ图标就可以利用自己的QQ号在网站进行登录了,下面我来告诉你一段QQ登录集成到自己网站php代码,有需要的朋友可参考. 1.打开open.qq.com 添加创建应用:-》输入常规的数据,你会看到对应的APP ID和KEY值,这是对你身份证...
PHP导入和导出CSV文件实现程序 - php高级...
PHP导入和导出CSV文件实现程序 我们先准备mysql数据表,假设项目中有一张记录学生信息的表student,并有id,name,sex,age分别记录学生的姓名、性别、年龄等信息,代码如下: CREATE TABLE `student` (     `id` int(11) NOT NULL auto_increment,     `name` varchar(50...
  •         php迷,一个php技术的分享社区,专属您自己的技术摘抄本、收藏夹。
  • 在这里……