教程集 www.jiaochengji.com
教程集 >  脚本编程  >  php  >  正文 phpExcel实现的excel操作类

phpExcel实现的excel操作类

发布时间:2017-05-12   编辑:jiaochengji.com
分享一例基于phpExcel实现的excel文件操作类,学习下phpExcel类库的用法,感兴趣的朋友不妨参考学习下。

本节内容:
一例基于phpexcel实现的excel类

代码:
 

复制代码 代码示例:

<?php
/*
 * 传入二位数组导出excel
 * 传入excel 导出二位数组
 * @author mrwu
 * @site www.jbxue.com
 */
require('PHPExcel.php');
require_once 'PHPExcel/Reader/Excel5.php'; 
include 'PHPExcel/IOFactory.php'; 

class plugin_phpexcel
{
    private $export_excel_title;//导出excel标题
    private $export_sheet_title;//导出sheet标题
    private $letters;
    private $php_excel;//php_excel操作类
    private $active_sheet;
   
    function __construct($export_excel_title='excel',$export_sheet_title='sheet1')
    {
    $this->letters=range('A','Z',1);
    $this->php_excel=new PHPExcel();
    $this->php_excel->setActiveSheetIndex(0);
    $this->active_sheet=$this->php_excel->getActiveSheet();
    $this->export_excel_title=$export_excel_title;
    $this->export_sheet_title=$export_sheet_title;
    }
   
    /*
     * $title='标题' array
     * $import_arr 插入excel的数组 要求二位数组
     */
    function export($title=array(),$import_arr)
    {
    //有设置excel第一行的标题
        if($title)
        {
       $count=count($title);
        for($i=0;$i<=$count;$i++)
             {
                  $this->active_sheet->setCellValue($this->letters[$i].'1',$title[$i]);
             }
        }
     //循环插入二维数组
        $count=count($import_arr[0]);
        $row=1;
        foreach($import_arr as $value)
        {
       
            $row++;
            $j=0;
            foreach($value as $key=>$v)
            {
            $this->active_sheet->setCellValue($this->letters[$j].$row,$v);
            echo $value[$j];
            $j++;
            }
        }
        $phpWriter=PHPExcel_IOFactory::createWriter($this->php_excel,'Excel5');
        //设置一些标题等
        $file=$this->export_excel_title;
        $this->active_sheet->setTitle($this->export_sheet_title);
       
        //设置header
        header("Pragma: public"); 
        header("Expires: 0"); 
        header("Cache-Control:must-revalidate, post-check=0, pre-check=0"); 
        header("Content-Type:application/force-download"); 
        header("Content-Type:application/vnd.ms-execl"); 
        header("Content-Type:application/octet-stream"); 
        header("Content-Type:application/download"); 
        header('Content-Disposition:attachment;filename="excel.xls"'); 
        header("Content-Transfer-Encoding:binary"); 
        
        $phpWriter->save('php://output');
       
   
    }
}
//调用示例
$php_excel=new plugin_phpexcel('excel','sheet1');
$php_excel->export(array('title','content'),array(array('title'=>'haha','content'=>'shit'),array('title'=>'hehe','content'=>'fuck')));

您可能感兴趣的文章:
PHP导出EXCEL的简单范例
phpexcel导出excel的经典实例
PHPExcel读取excel文件的例子
phpexcel导出数据的实例代码
phpexcel快速开发指南(不错)
phpExcel中文帮助手册(知识点)

您可能感兴趣的文章:
phpexcel导出数据的实例代码
解决php下载excel无法打开的问题
phpexcel导入excel到数据库的代码
PHP导出EXCEL的简单范例 使用phpexcel类库导出excel
PHP导出excel php使用phpexcel导出excel文件
PHPExcel实例代码 phpexcel类库示例
PHP通过PHPExcel类导入导出excel
phpExcel实现的excel操作类
phpexcel快速开发指南(不错)
php读取excel的实例代码

关键词: excel  PHPExcel   
[关闭]
~ ~