PHPExcel 类
//引入工具类
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 类的更多相关文章
- 用PHPExcel类读取excel文件的内容
这里对PHPExcel类不做介绍,有兴趣的朋友可以自己查阅资料 在classes文件夹下有个PHPExcel.php文件,这个文件是这个类库的主要入口文件,在用之前,要引入这个类 其他的类,在此类中会 ...
- ThinkPHP5调用PHPExcel类实现导入导出
注意:extend是放置第三方类的地方,不要乱配置命名空间那些,引起不必要的类错误 代码如下 <?php namespace app\index\controller; use think\Co ...
- TP3.2加载外部PHPexcel类,实现导入和导出
TP3.2加载外部PHPexcel类,实现导入和导出 导入: 1.将下载好的PHPexcel文件放到libray/Org/Uti/文件夹下,将PHPEXCEL.PHP改为PHPEXCEL.class. ...
- php利用PHPExcel类导出导入Excel用法
PHPExcel类是php一个excel表格处理插件了,下面我来给大家介绍利用PHPExcel类来导入与导出excel表格的应用方法,有需要了解的朋友不防参考参考(PHPExcel自己百度下载这里不介 ...
- ThinkPHP3.2.3使用PHPExcel类操作excel导出excel
如何导入excel请看:ThinkPHP3.2.3使用PHPExcel类操作excel导入读取excel // 引入PHPExcel类 import("Org.Util.PHPExccel& ...
- thinkphp引入PHPExcel类---thinkPHP类库扩展-----引入没有采用命名空间的类库
最近项目中遇到引入PHPExcel第三方类库 但是下载的phpExcel类没有命名空间,而且所有接口文件的命名都是以.php结尾 而不是tp中的.class.php 解决办法很简单:在引入没有采用命 ...
- 关于PHPExcel类占用内存问题
最近在帮一家公司做后台excel导出功能,使用的工具类是phpexcel,因为这个类功能比较强大.全面. 但是遇到下面一个问题: 当导出数据量达到一定数量级的时候,比如说1000条,服务器出现卡顿.白 ...
- php使用PHPexcel类读取excel文件(循环读取每个单元格的数据)
error_reporting(E_ALL); date_default_timezone_set('Asia/ShangHai'); include_once('Classes/PHPExcel/I ...
- ThinkPHP3.2.3使用PHPExcel类操作excel导入读取excel
方法一: 1. 下载PHPExcel并保存在如下位置: 2. 在控制器中引用 vendor("PHPExcel.PHPExcel"); $objReader = \PHPExcel ...
随机推荐
- mybatis中的resultMap
resultMap <?xml version="1.0" encoding="UTF-8" ?> <!DOCTYPE mapper PUBL ...
- 在ubuntu上面安装phpmyadmin后,报404错误
安装完mysql后,我想装一个phpmyadmin方便mysql管理. 在终端执行命令:sudo apt-get install libapache2-mod-auth-mysql phpmyadmi ...
- POJ 1191 棋盘分割
棋盘分割 Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 11213 Accepted: 3951 Description 将一个 ...
- 机器学习公开课笔记(8):k-means聚类和PCA降维
K-Means算法 非监督式学习对一组无标签的数据试图发现其内在的结构,主要用途包括: 市场划分(Market Segmentation) 社交网络分析(Social Network Analysis ...
- HDU 4911 http://acm.hdu.edu.cn/showproblem.php?pid=4911(线段树求逆序对)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4911 解题报告: 给出一个长度为n的序列,然后给出一个k,要你求最多做k次相邻的数字交换后,逆序数最少 ...
- HDU 2577 How to Type(dp题)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=2577 解题报告:有一个长度在100以内的字符串,并且这个字符串只有大写和小写字母组成,现在要把这些字符 ...
- 序列化.to_sym
<%= link_to t('links.list'), showable_paths[@subchannel_item.showable_type.to_sym], target: '_bla ...
- 关于mac安装rails报错clang: error: unknown argument
文章都是从我的个人博客上转载过来的,大家可以点击我的个人博客. www.iwangzheng.com mac上安装rails的时候报错, 安装rails的在终端执行了一句命令: $sudo gem i ...
- SQL注入自学[第二学:注入环境的简单突破]
/* 原文出处:珍惜少年时 留给原创一个ZBD机会. 加号即空格 */ 00x1 判断是否含有注入 http://127.0.0.1/1.php?id=3 and 1=1-- 返回正确的页面. htt ...
- Trailing Zeros
Write an algorithm which computes the number of trailing zeros in n factorial. Have you met this que ...