CI框架中使用PHPExcel导出Excel文件

本文转自:http://blog.csdn.net/u012884402/article/details/47339903


准备工作: 
1.首先,下载一个PHPExcel类包,下方是上传的类包的下载路径

http://pan.baidu.com/s/1pJA8TVP

2.安装PHPExcel到CI中

a). 解压压缩包中的Classes文件夹中的内容到application\libraries\目录下,目录结构如下: 
—application\libraries\PHPExcel.PHP 
— application\libraries\PHPExcel (文件夹)

b).修改application\libraries\PHPExcel\IOFactory.php 文件 
— 将文件中其定义的类名从PHPExcel_IOFactory改为IOFactory,遵从CI类命名规则。 
—将其构造函数改为public

3.编写代码

a).导出Excel表格
header('Content-Type: application/vnd.ms-excel;');          
header('Content-Disposition: attachment;filename="text.xls"');
header('Cache-Control: max-age=0'); 

include 'PHPExcel/Writer/Excel2007.php';
$this->load->library('PHPExcel');
$this->load->library('PHPExcel/IOFactory');

$objPHPExcel = new PHPExcel();
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10

注解:header中是必填写的头文件信息 
header(‘Content-Type: application/vnd.ms-excel;’); ​将查询结果导出到Excel中 
header(‘Content-Disposition: attachment;filename=”text.xls”’);​ 文件名 
​include,和$this->load->library都是用来载入文件的

4.写入头文件

echo "<table width='500' border='1' cellspacing='2' cellpadding='2'>
        <tr align='center'  style='background-color:#E5E8ED'>
            <td rowspan='3'> </td>

            <td rowspan='3'> </td>

            <td >&nbsp;</td>
            <td >&nbsp;</td>
            <td >&nbsp;</td>
            <td >&nbsp;</td>

            <td rowspan='3' > </td>
            <td rowspan='3' ></td>
            <td rowspan='3' >  </td>

            <td colspan='7'> </td>
            <td colspan='9'> </td>
            <td colspan='4'> </td>
        </tr>

        <tr align='center'  style='background-color:#E5E8ED'>

            <td colspan='4' > </td>

            <td colspan='3' > </td>
            <td> </td>
            <td colspan='3'> </td>

            <td colspan='2'> </td>
            <td colspan='2'> </td>
            <td colspan='5'> </td>

            <td colspan='2'> </td>
            <td colspan='2'> </td>

        </tr>

        <tr align='center'  style='background-color:#E5E8ED'>

            <td > </td>
            <td> </td>
            <td > </td>
            <td > </td>

            <td> </td>
            <td> </td>
            <td> </td>
            <td> </td>
            <td> </td>
            <td> </td>
            <td> </td>
            <td> </td>
            <td> </td>
            <td> </td>
            <td> </td>
            <td> </td>
            <td> </td>
            <td> </td>
            <td>  </td>
            <td> </td>          
            <td>  </td>
            <td> </td>          
            <td> </td>
            <td> </td>

        </tr>";
 
 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66

这样头文件就写好了

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值