//引入工具类
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. SQL Server 2005 发布 订阅 (配置实例[图])(转载)

    2.1          发布&订阅 1.       测 试环境: Item 发布机 A 订阅机 B OS Windows 2003 Server Windows 2003 Server S ...

  2. 使用ymPrompt弹框

    使用弹框 <%@ page language="java" import="java.util.*" pageEncoding="UTF-8&q ...

  3. ASP.NET MVC4 log4net

    LogManager.GetLogger(MethodBase.GetCurrentMethod().DeclaringType);

  4. Windows下文件的所有和权限

    跟linux不同, 在linux下 ,文件的所有者,就拥有对文件的所有读写执行的权限, 而windows, 文件的所有者不一定对文件拥有所有的权限, 场景: 要对系统文件(windows\system ...

  5. hibernate中几个接口作用

    1.Configuration 类 Configuration 类负责管理 Hibernate 的配置信息,包括数据库的URL.用户名.密码.JDBC驱动类,数据库Dialect,数据库连接池等,其加 ...

  6. magic-encoding

    (文章都是从我的个人主页上粘贴过来的,大家也可以访问我的主页 www.iwangzheng.com) 今天页面跳转都出问题了,各种方法都试过了, log里说语法错误,问了pp,他说是汉字的原因...果 ...

  7. HTML快速入门4

    七.表单 1. 概述 建立交互式的站点,需要使用 HTML 表单,它可以让用户提供信息,并对此作出处理.可以建立类似 复选框.单选按钮及文本框的控件. 掌握表单的使用对 Active Server P ...

  8. BZOJ 1455

    STL的基本用法 (居然能空间卡过去= =!!!) #include <cstdio> #include <ext/pb_ds/priority_queue.hpp> #inc ...

  9. linux 下如何查看和踢除正在登陆的其它用户 ==>Linux下用于查看系统当前登录用户信息的4种方法

    在linux系统中用pkill命令踢出在线登录用户 由于linux服务器允许多用户登录,公司很多人知道密码,工作造成一定的障碍 所以需要有时踢出指定的用户 1/#who   查出当前有那些终端登录(用 ...

  10. MySql的like语句中的通配符:百分号、下划线和escape

      MySql的like语句中的通配符:百分号.下划线和escape   %:表示任意个或多个字符.可匹配任意类型和长度的字符. Sql代码 select * from user where user ...