首頁 php教程 php手册 Codeigniter利用PHPExcel导出Excel文件

Codeigniter利用PHPExcel导出Excel文件

May 25, 2016 pm 04:43 PM
codeigniter excel文件 phpexcel

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),代码如下:

<?php
class Table_export extends CI_Controller {
    function __construct() {
        parent::__construct();
        // Here you should add some sort of user validation
        // to prevent strangers from pulling your table data
        
    }
    function index($table_name) {
        $query = $this->db->get($table_name);
        if (!$query) return false;
        // Starting the PHPExcel library
        $this->load->library(&#39;PHPExcel&#39;);
        $this->load->library(&#39;PHPExcel/IOFactory&#39;);
        $objPHPExcel = new PHPExcel();
        $objPHPExcel->getProperties()->setTitle("export")->setDescription("none");
        $objPHPExcel->setActiveSheetIndex(0);
        // Field names in the first row
        $fields = $query->list_fields();
        $col = 0;
        foreach ($fields as $field) {
            $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, 1, $field);
            $col++;
        }
        // Fetching the table data
        $row = 2;
        foreach ($query->result() as $data) {
            $col = 0;
            foreach ($fields as $field) {
                $objPHPExcel->getActiveSheet()->setCellValueByColumnAndRow($col, $row, $data->$field);
                $col++;
            }
            $row++;
        }
        $objPHPExcel->setActiveSheetIndex(0);
        $objWriter = IOFactory::createWriter($objPHPExcel, &#39;Excel5&#39;);
        // Sending headers to force the user to download the file
        header(&#39;Content-Type: application/vnd.ms-excel&#39;);
        header(&#39;Content-Disposition: attachment;filename="Products_&#39; . date(&#39;dMy&#39;) . &#39;.xls"&#39;);
        header(&#39;Cache-Control: max-age=0&#39;);
        $objWriter->save(&#39;php://output&#39;);
    }
}
?>
登入後複製

方法二,代码如下:

Excel Plugin  
	The following plugin will generate a tab-delimited file, and feed it to the client as an Excel file. 
	 
	$this->load->plugin(&#39;to_excel&#39;); 
	$this->db->use_table(&#39;tablename&#39;); 
	$this->db->select(&#39;field1&#39;, &#39;field2&#39;); 
	// run joins, order by, where, or anything else here 
	$query = $this->db->get(); 
	to_excel($query, [&#39;filename&#39;]); // filename is optional, without it, the plugin will default to &#39;exceloutput&#39; 
	 
	So you could run: 
	 
	to_excel($query, &#39;myfile&#39;); // outputs myfile.xls 
	to_excel($query); // outputs exceloutput.xls 
	// you could also use a model here 
	to_excel($this->model_name->functioncall()); 
	 
	/system/plugins/to_excel_pi.php 

	
登入後複製
<?php
if (!defined(&#39;BASEPATH&#39;)) exit(&#39;No direct script access allowed&#39;);
/*
 * Excel library for Code Igniter applications
 * Author: Derek Allard, Dark Horse Consulting, www.darkhorse.to, April 2006
*/
function to_excel($query, $filename = &#39;exceloutput&#39;) {
    $headers = &#39;&#39;; // just creating the var for field headers to append to below
    $data = &#39;&#39;; // just creating the var for field data to append to below
    $obj = & get_instance();
    $fields = $query->field_data();
    if ($query->num_rows() == 0) {
        echo &#39;<p>The table appears to have no data.</p>&#39;;
    } else {
        foreach ($fields as $field) {
            $headers.= $field->name . "t";
        }
        foreach ($query->result() as $row) {
            $line = &#39;&#39;;
            foreach ($row as $value) {
                if ((!isset($value)) OR ($value == "")) {
                    $value = "t";
                } else {
                    $value = str_replace(&#39;"&#39;, &#39;""&#39;, $value);
                    $value = &#39;"&#39; . $value . &#39;"&#39; . "t";
                }
                $line.= $value;
            }
            $data.= trim($line) . "n";
        }
        $data = str_replace("r", "", $data);
        header("Content-type: application/x-msdownload");
        header("Content-Disposition: attachment; filename=$filename.xls");
        echo "$headersn$data";
    }
}
?>
登入後複製

                        

本文地址:

转载随意,但请附上文章地址:-)

本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn

熱AI工具

Undresser.AI Undress

Undresser.AI Undress

人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover

AI Clothes Remover

用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool

Undress AI Tool

免費脫衣圖片

Clothoff.io

Clothoff.io

AI脫衣器

AI Hentai Generator

AI Hentai Generator

免費產生 AI 無盡。

熱門文章

R.E.P.O.能量晶體解釋及其做什麼(黃色晶體)
2 週前 By 尊渡假赌尊渡假赌尊渡假赌
倉庫:如何復興隊友
4 週前 By 尊渡假赌尊渡假赌尊渡假赌
Hello Kitty Island冒險:如何獲得巨型種子
3 週前 By 尊渡假赌尊渡假赌尊渡假赌

熱工具

記事本++7.3.1

記事本++7.3.1

好用且免費的程式碼編輯器

SublimeText3漢化版

SublimeText3漢化版

中文版,非常好用

禪工作室 13.0.1

禪工作室 13.0.1

強大的PHP整合開發環境

Dreamweaver CS6

Dreamweaver CS6

視覺化網頁開發工具

SublimeText3 Mac版

SublimeText3 Mac版

神級程式碼編輯軟體(SublimeText3)

如何在CodeIgniter中實作自訂中介軟體 如何在CodeIgniter中實作自訂中介軟體 Jul 29, 2023 am 10:53 AM

如何在CodeIgniter中實作自訂中介軟體

CodeIgniter中間件:加速應用程式的反應速度和頁面渲染 CodeIgniter中間件:加速應用程式的反應速度和頁面渲染 Jul 28, 2023 pm 06:51 PM

CodeIgniter中間件:加速應用程式的反應速度和頁面渲染

PHP開發:使用 PHPExcel 處理 Excel 文件 PHP開發:使用 PHPExcel 處理 Excel 文件 Jun 15, 2023 pm 03:45 PM

PHP開發:使用 PHPExcel 處理 Excel 文件

完全指南:如何使用php擴充PHPExcel處理Excel文件 完全指南:如何使用php擴充PHPExcel處理Excel文件 Jul 28, 2023 pm 10:01 PM

完全指南:如何使用php擴充PHPExcel處理Excel文件

PHP開發:使用 CodeIgniter 實作 MVC 模式和 RESTful API PHP開發:使用 CodeIgniter 實作 MVC 模式和 RESTful API Jun 16, 2023 am 08:09 AM

PHP開發:使用 CodeIgniter 實作 MVC 模式和 RESTful API

在CodeIgniter框架中使用資料庫查詢建構器(Query Builder)的方法 在CodeIgniter框架中使用資料庫查詢建構器(Query Builder)的方法 Jul 28, 2023 pm 11:13 PM

在CodeIgniter框架中使用資料庫查詢建構器(Query Builder)的方法

php如何使用CodeIgniter5框架? php如何使用CodeIgniter5框架? Jun 01, 2023 am 11:21 AM

php如何使用CodeIgniter5框架?

CodeIgniter中間件:提供安全的檔案上傳和下載功能 CodeIgniter中間件:提供安全的檔案上傳和下載功能 Aug 01, 2023 pm 03:01 PM

CodeIgniter中間件:提供安全的檔案上傳和下載功能

See all articles