php excel类 phpExcel使用方法介绍


Posted in PHP onAugust 21, 2010

下载地址:http://www.codeplex.com/PHPExcel

下面是总结的几个使用方法

include ‘PHPExcel.php';

include ‘PHPExcel/Writer/Excel2007.php';

//或者include ‘PHPExcel/Writer/Excel5.php'; 用于输出.xls的

创建一个excel

$objPHPExcel = new PHPExcel();

保存excel—2007格式

$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);

//或者$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 非2007格式

$objWriter->save("xxx.xlsx");

直接输出到浏览器

$objWriter = new PHPExcel_Writer_Excel5($objPHPExcel);

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="resume.xls"‘);

header("Content-Transfer-Encoding:binary");

$objWriter->save('php://output');

——————————————————————————————————————?

设置excel的属性:

创建人

$objPHPExcel->getProperties()->setCreator("Maarten Balliauw");

最后修改人

$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");

标题

$objPHPExcel->getProperties()->setTitle("Office 2007 XLSX Test Document");

题目

$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX Test Document");

描述

$objPHPExcel->getProperties()->setDescription("Test document for Office 2007 XLSX, generated using PHP classes.");

关键字

$objPHPExcel->getProperties()->setKeywords("office 2007 openxml php");

种类

$objPHPExcel->getProperties()->setCategory("Test result file");

——————————————————————————————————————?

设置当前的sheet

$objPHPExcel->setActiveSheetIndex(0);

设置sheet的name

$objPHPExcel->getActiveSheet()->setTitle('Simple');

设置单元格的值

$objPHPExcel->getActiveSheet()->setCellValue('A1′, ‘String');

$objPHPExcel->getActiveSheet()->setCellValue('A2′, 12);

$objPHPExcel->getActiveSheet()->setCellValue('A3′, true);

$objPHPExcel->getActiveSheet()->setCellValue('C5′, ‘=SUM(C2:C4)');

$objPHPExcel->getActiveSheet()->setCellValue('B8′, ‘=MIN(B2:C5)');

合并单元格

$objPHPExcel->getActiveSheet()->mergeCells('A18:E22′);

分离单元格

$objPHPExcel->getActiveSheet()->unmergeCells('A28:B28′);
保护cell

$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true); // Needs to be set to true in order to enable any worksheet protection!

$objPHPExcel->getActiveSheet()->protectCells('A3:E13′, ‘PHPExcel');

设置格式

// Set cell number formats

echo date('H:i:s') . " Set cell number formatsn";

$objPHPExcel->getActiveSheet()->getStyle('E4′)->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);

$objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4′), ‘E5:E13′ );

设置宽width

// Set column widths

$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setAutoSize(true);

$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);

设置font

$objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->setName('Candara');

$objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->setSize(20);

$objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->setBold(true);

$objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);

$objPHPExcel->getActiveSheet()->getStyle('B1′)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);

$objPHPExcel->getActiveSheet()->getStyle('E1′)->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);

$objPHPExcel->getActiveSheet()->getStyle('D13′)->getFont()->setBold(true);

$objPHPExcel->getActiveSheet()->getStyle('E13′)->getFont()->setBold(true);

设置align

$objPHPExcel->getActiveSheet()->getStyle('D11′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

$objPHPExcel->getActiveSheet()->getStyle('D12′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

$objPHPExcel->getActiveSheet()->getStyle('D13′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);

$objPHPExcel->getActiveSheet()->getStyle('A18′)->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);

//垂直居中

$objPHPExcel->getActiveSheet()->getStyle('A18′)->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);

设置column的border

$objPHPExcel->getActiveSheet()->getStyle('A4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

$objPHPExcel->getActiveSheet()->getStyle('B4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

$objPHPExcel->getActiveSheet()->getStyle('C4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

$objPHPExcel->getActiveSheet()->getStyle('D4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

$objPHPExcel->getActiveSheet()->getStyle('E4′)->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);

设置border的color

$objPHPExcel->getActiveSheet()->getStyle('D13′)->getBorders()->getLeft()->getColor()->setARGB('FF993300′);

$objPHPExcel->getActiveSheet()->getStyle('D13′)->getBorders()->getTop()->getColor()->setARGB('FF993300′);

$objPHPExcel->getActiveSheet()->getStyle('D13′)->getBorders()->getBottom()->getColor()->setARGB('FF993300′);

$objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getTop()->getColor()->setARGB('FF993300′);

$objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getBottom()->getColor()->setARGB('FF993300′);

$objPHPExcel->getActiveSheet()->getStyle('E13′)->getBorders()->getRight()->getColor()->setARGB('FF993300′);

设置填充颜色

$objPHPExcel->getActiveSheet()->getStyle('A1′)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);

$objPHPExcel->getActiveSheet()->getStyle('A1′)->getFill()->getStartColor()->setARGB('FF808080′);

$objPHPExcel->getActiveSheet()->getStyle('B1′)->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);

$objPHPExcel->getActiveSheet()->getStyle('B1′)->getFill()->getStartColor()->setARGB('FF808080′);

加图片

$objDrawing = new PHPExcel_Worksheet_Drawing();

$objDrawing->setName('Logo');

$objDrawing->setDescription('Logo');

$objDrawing->setPath('./images/officelogo.jpg');

$objDrawing->setHeight(36);

$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

$objDrawing = new PHPExcel_Worksheet_Drawing();

$objDrawing->setName('Paid');

$objDrawing->setDescription('Paid');

$objDrawing->setPath('./images/paid.png');

$objDrawing->setCoordinates('B15′);

$objDrawing->setOffsetX(110);

$objDrawing->setRotation(25);

$objDrawing->getShadow()->setVisible(true);

$objDrawing->getShadow()->setDirection(45);

$objDrawing->setWorksheet($objPHPExcel->getActiveSheet());

在默认sheet后,创建一个worksheet

echo date('H:i:s') . " Create new Worksheet objectn";

$objPHPExcel->createSheet();

$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');

$objWriter-save('php://output');

PHP 相关文章推荐
杏林同学录(八)
Oct 09 PHP
第九节--绑定
Nov 16 PHP
PHP通过COM使用ADODB的简单例子
Dec 31 PHP
ThinkPHP实现将本地文件打包成zip下载
Jun 26 PHP
php使用curl简单抓取远程url的方法
Mar 13 PHP
php通过strpos查找字符串出现位置的方法
Mar 17 PHP
thinkphp命名空间用法实例详解
Dec 30 PHP
PHP删除二维数组中相同元素及数组重复值的方法示例
May 05 PHP
php 算法之实现相对路径的实例
Oct 17 PHP
PHP数据对象映射模式实例分析
Mar 29 PHP
Laravel向公共模板赋值方法总结
Jun 25 PHP
php实现对文件压缩简单的方法
Sep 29 PHP
php下正则来匹配dede模板标签的代码
Aug 21 #PHP
php中使用ExcelFileParser处理excel获得数据(可作批量导入到数据库使用)
Aug 21 #PHP
使用VisualStudio开发php的图文设置方法
Aug 21 #PHP
利用Memcached在php下实现session机制 替换PHP的原生session支持
Aug 21 #PHP
ThinkPHP中实例Model方法的区别说明
Aug 21 #PHP
PHP 数组基础知识小结
Aug 20 #PHP
一个简单php扩展介绍与开发教程
Aug 19 #PHP
You might like
PHP的博客ping服务代码
2012/02/04 PHP
Zend Framework连接Mysql数据库实例分析
2016/03/19 PHP
JavaScript 替换Html标签实现代码
2009/10/14 Javascript
jquery下异步提交表单 异步跨域提交表单
2010/11/17 Javascript
return false,对阻止事件默认动作的一些测试代码
2010/11/17 Javascript
JS如何将UTC格式时间转本地格式
2013/09/04 Javascript
JavaScript实现Java中StringBuffer的方法
2015/02/09 Javascript
javascript中加var和不加var的区别 你真的懂吗
2016/01/06 Javascript
小心!AngularJS结合RequireJS做文件合并压缩的那些坑
2016/01/09 Javascript
简单实现js倒计时功能
2017/02/13 Javascript
详解vue服务端渲染(SSR)初探
2017/06/19 Javascript
canvas基础绘制-绚丽倒计时的实例
2017/09/17 Javascript
谈谈vue中mixin的一点理解
2017/12/12 Javascript
JavaScript实现获取select下拉框中第一个值的方法
2018/02/06 Javascript
Vue实现简易翻页效果源码分享
2018/11/08 Javascript
React Native 混合开发多入口加载方式详解
2019/09/23 Javascript
微信小程序工具函数封装
2019/10/28 Javascript
vue实现编辑器键盘抬起时内容跟随光标距顶位置向上滚动效果
2020/05/28 Javascript
微信小程序实现购物车小功能
2020/12/30 Javascript
[02:24]DOTA2亚洲邀请赛 NAVI战队出场宣传片
2015/02/07 DOTA
对python使用http、https代理的实例讲解
2018/05/07 Python
python生成九宫格图片
2018/11/19 Python
python格式化输出保留2位小数的实现方法
2019/07/02 Python
Python函数的默认参数设计示例详解
2019/12/01 Python
在python中求分布函数相关的包实例
2020/04/15 Python
python3.7中安装paddleocr及paddlepaddle包的多种方法
2020/11/27 Python
日本著名化妆品零售网站:Cosme Land
2019/03/01 全球购物
什么是Remote Module
2016/06/10 面试题
《郑和远航》教学反思
2014/04/16 职场文书
学习雷锋演讲稿
2014/05/10 职场文书
交通志愿者活动总结
2014/06/27 职场文书
2014年检验科工作总结
2014/11/22 职场文书
秋季运动会加油词
2015/07/18 职场文书
新闻通讯稿模板
2015/07/22 职场文书
如何把新闻人物写得立体、鲜活?
2019/08/14 职场文书
python自动化测试通过日志3分钟定位bug
2021/11/20 Python