//引入工具类
require_once 'PHPExcel.php';

//HandleExcel.class.php 文件
class HandleExcel extends PHPExcel{ private $file;
private $fileType; private $objProperty; //文档属性对象
private $objReader;
private $objWriter; private $property = array(); public function __construct($property = array()){
parent::__construct();
if($property){
$this->property = $property;
$this->setProperty($this->property);
}
} /*
* 设置文档属性
* $property = array('title'=>'标题', 'creator' => '作者');
*/
public function setProperty($property = array()){
$this->objProperty = $this->getProperties();
if(!empty($property['creator']))$this->objProperty->setCreator($property['creator']);
if(!empty($property['title'])) $this->objProperty->setTitle($property['title']);
if(!empty($property['subject']))$this->objProperty->setSubject($property['subject']);
if(!empty($property['laster']))$this->objProperty->setLastModifiedBy($property['laster']);
if(!empty($property['description']))$this->objProperty->setDescription($property['description']);
if(!empty($property['keyword']))$this->objProperty->setKeywords($property['keyword']);
if(!empty($property['category']))$this->objProperty->setCategory($property['category']);
} /*
* 添加数据
* $data = array( 'name'=>'tom', 'age'=>'13');
* $dataCnf = array('name=>'a1', 'age'=>'b2');
* */
public function addData($data, $field = array(), $index = ''){
$objAdd = ($index)? $this->setActiveSheetIndex($index) : $this->getActiveSheet(); //根据data自动创建位置数据对应位置.
if($field){
$data = array_merge( array(0 => $field), $data);
$excelRowCnf = array('a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z');
$excelLine = 1;
foreach($data as $key => $row){
$excelRow = 0;
foreach($field as $fieldName => $text){
$data['new'][$key][$excelRowCnf[$excelRow].$excelLine] = $row[$fieldName];
$excelRow++;
}
unset($data[$key]);
$excelLine++;
}
$data = $data['new'];
} //添加
foreach($data as $row){
foreach($row as $place => $val){
if(empty($place) || empty($val)) continue;
$objAdd->setCellValue($place, $val);
}
} } //生成文件
public function saveFile($savePath, $output = false, $type = 'Excel5'){
$this->objWriter = PHPExcel_IOFactory::createWriter($this, $type);
if($output){
$savePath = iconv ('utf-8', 'gb2312', $savePath);
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$savePath.'"');
header('Cache-Control: max-age=0');
// If you're serving to IE 9, then the following may be needed
header('Cache-Control: max-age=1');
// If you're serving to IE over SSL, then the following may be needed
header ('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); // Date in the past
header ('Last-Modified: '.gmdate('D, d M Y H:i:s').' GMT'); // always modified
header ('Cache-Control: cache, must-revalidate'); // HTTP/1.1
header ('Pragma: public'); // HTTP/1.0
$this->objWriter->save('php://output');
exit;
}
$this->objWriter->save($savePath);
} /* -------------------------------------- 读取文件 ---------------------------- */ //设置读对象
private function setReader($file, $type = null){
$this->file = $file;
if($type){
$this->fileType = $type;
$this->objReader = PHPExcel_IOFactory::createReader($type)->load($file);
}else{
$this->fileType = PHPExcel_IOFactory::identify($file);
$this->objReader = PHPExcel_IOFactory::load($file);
}
} //加载文件
public function loadFile($file, $type = null){
$this->setReader($file, $type);
$this->sheetData = $this->objReader->getActiveSheet()->toArray(null,true,true,true);
} //返回需要的数据
public function dataFormat($meed, $start = 1, $end = null){
foreach($this->sheetData as $line => $row){
if($start && $line < $start) continue;
if($end && $line > $end) break;
foreach($row as $key => $val){
if(array_key_exists($key, $meed)){
$data[$line][$meed[$key]] = $val;
}
}
}
return array_merge($data);
} //工作表信息
public function sheetInfo($file = null){
($file)? null : $file = $this->file;
$info = $this->objReader->listWorksheetInfo($file);
return $info;
} }

set_time_limit(0);
//使用如下:
require_once 'HandleExcel.class.php';
$myExcel = new HandleExcel();
$fieldCnf = array('title'=>'名称','type'=>'类型', 'content'=>'反馈内容', 'name'=>'反馈人', 'reg_mail'=>'联系邮箱','from_link'=>'相关链接', 'addtime'=>'反馈时间'); /*说明一下, $record 是查询得到的二维数组数据
array(
array('title' => '这是标题1', 'type'=>1, 'content'=>'内容文字' ... ),
array('title' => '这是标题2', 'type'=>3, 'content'=>'内容文字' ... )
...
)
*/
$myExcel->addData($record, $fieldCnf);
// ->saveFile(文件名, 是否输出到浏览器? )
$myExcel->saveFile('用户反馈数据.xls', true);

最后, 得到的excel文件显示如下:

PHPExcel 类的更多相关文章

  1. 用PHPExcel类读取excel文件的内容

    这里对PHPExcel类不做介绍,有兴趣的朋友可以自己查阅资料 在classes文件夹下有个PHPExcel.php文件,这个文件是这个类库的主要入口文件,在用之前,要引入这个类 其他的类,在此类中会 ...

  2. ThinkPHP5调用PHPExcel类实现导入导出

    注意:extend是放置第三方类的地方,不要乱配置命名空间那些,引起不必要的类错误 代码如下 <?php namespace app\index\controller; use think\Co ...

  3. TP3.2加载外部PHPexcel类,实现导入和导出

    TP3.2加载外部PHPexcel类,实现导入和导出 导入: 1.将下载好的PHPexcel文件放到libray/Org/Uti/文件夹下,将PHPEXCEL.PHP改为PHPEXCEL.class. ...

  4. php利用PHPExcel类导出导入Excel用法

    PHPExcel类是php一个excel表格处理插件了,下面我来给大家介绍利用PHPExcel类来导入与导出excel表格的应用方法,有需要了解的朋友不防参考参考(PHPExcel自己百度下载这里不介 ...

  5. ThinkPHP3.2.3使用PHPExcel类操作excel导出excel

    如何导入excel请看:ThinkPHP3.2.3使用PHPExcel类操作excel导入读取excel // 引入PHPExcel类 import("Org.Util.PHPExccel& ...

  6. thinkphp引入PHPExcel类---thinkPHP类库扩展-----引入没有采用命名空间的类库

    最近项目中遇到引入PHPExcel第三方类库 但是下载的phpExcel类没有命名空间,而且所有接口文件的命名都是以.php结尾  而不是tp中的.class.php 解决办法很简单:在引入没有采用命 ...

  7. 关于PHPExcel类占用内存问题

    最近在帮一家公司做后台excel导出功能,使用的工具类是phpexcel,因为这个类功能比较强大.全面. 但是遇到下面一个问题: 当导出数据量达到一定数量级的时候,比如说1000条,服务器出现卡顿.白 ...

  8. php使用PHPexcel类读取excel文件(循环读取每个单元格的数据)

    error_reporting(E_ALL); date_default_timezone_set('Asia/ShangHai'); include_once('Classes/PHPExcel/I ...

  9. ThinkPHP3.2.3使用PHPExcel类操作excel导入读取excel

    方法一: 1. 下载PHPExcel并保存在如下位置: 2. 在控制器中引用 vendor("PHPExcel.PHPExcel"); $objReader = \PHPExcel ...

随机推荐

  1. 关于FlexPaper 2.1.2版本 二次开发 Logo 、打印、搜索、缩略图、添加按钮、js交互、右键菜单、书签等相关问题

    2015-03-02 更新文章,由于需求修改,更改了flexpaper插件,故增加第9.10.11小节,下载代码时请注意. 先废话几句.最近用到文档在线浏览功能,之前用的是print2flash(一个 ...

  2. sql server case when 判断为空

    代码如下 select distinct G.* ,(select BUSINESS_NAME from BusinessInfo where BusinessInfo.BUSINESS_BID=G. ...

  3. HTTP 304 的理解

    304 的标准解释是:Not Modified 客户端有缓冲的文档并发出了一个条件性的请求(一般是提供If-Modified-Since头表示客户只想比指定日期更新的文档).服务器告诉客户,原来缓冲的 ...

  4. BC.5200.Trees(dp)

    Trees  Accepts: 156  Submissions: 533  Time Limit: 2000/1000 MS (Java/Others)  Memory Limit: 65536/6 ...

  5. Ubuntu 开机自启动工具 update-rd.d 使用详解

    常用命令: $ sudo update-rc.d nginx defaults      #增加服务 $ sudo update-rc.d -f nginx remove    #移除服务 Linux ...

  6. [BZOJ3343]教主的魔法

    [BZOJ3343]教主的魔法 试题描述 教主最近学会了一种神奇的魔法,能够使人长高.于是他准备演示给XMYZ信息组每个英雄看.于是N个英雄们又一次聚集在了一起,这次他们排成了一列,被编号为1.2.… ...

  7. PHP生成CSV文件

    CSV文件的定义这里就不多做介绍了,难能可贵的是用Excel可以直接打开CSV文件.用PHP输出CSV文件本身很简单,但是大家如果有业务需求,下面的代码可以作为参考. $tableheader = a ...

  8. CPU供电维修

  9. HDOj 1010 DFS优化

    #include<cstdio> #include<cstring> ]={,,,-}; ]={,,-,}; ][]; int x1,y1,x2,y2; int step; i ...

  10. Redis windows安装配置与Jedis访问数据库

    一 Redis概要 Redis是一个开源的使用ANSI C语言编写.遵守BSD协议.支持网络.可基于内存亦可持久化的日志型.Key-Value数据库,并提供多种语言的API.它通常被称为数据结构服务器 ...