PHPEXCEL 使用小记


Posted in PHP onJanuary 06, 2013

首先是使用PHP Reader 读取Excle内容:

require("https://3water.com/PHPExcel/Classes/PHPExcel.php"); 
$file = "D:\\datas.xlsx"; 
if(!file_exists($file)){ 
die("no file found in {$file}"); 
} 
$datasReader = PHPExcel_IOFactory::load($file); 
$sheets = $datasReader->getAllSheets(); 
//如果有多个工作簿 
$countSheets = count($sheets); 
$sheetsinfo = array(); 
$sheetData = array(); 
if($countSheets==1){ 
$sheet = $sheets[0]; 
$sheetsinfo["rows"] = $sheet->getHighestRow(); 
$sheetsinfo["column"] = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn()); 
for($row=1;$row<=$sheetsinfo["rows"];$row++){ 
for($column=0;$column<$sheetsinfo["column"];$column++){ 
$sheetData[$column][$row] = $sheet->getCellByColumnAndRow($column, $row)->getValue(); 
} 
} 
}else{ 
foreach ($sheets as $key => $sheet) 
{ 
$sheetsinfo[$key]["rows"] = $sheet->getHighestRow(); 
$sheetsinfo[$key]["column"] = PHPExcel_Cell::columnIndexFromString($sheet->getHighestColumn()); 
for($row=1;$row<=$sheetsinfo[$key]["rows"];$row++){ 
for($column=0;$column<$sheetsinfo[$key]["column"];$column++){ 
$sheetData[$key][$column][$row] = $sheet->getCellByColumnAndRow($column, $row)->getValue(); 
} 
} 
} 
} 
echo "<pre>"; 
print_r($sheetData); 
echo "</pre>";

注:使用PHP 读取excel文件内容,一般都是处理整理好格式的csv或者excel,也可以读取xml文件

PHPExcel生成Exceel

$sql = sprintf("select * from table where op_id=%d", intval($this->params['id'])); 
$query = $this->_db->query($sql); 
require_once './PHPExcel_1.7.4/Classes/PHPExcel.php'; 
$objPHPExcel = new PHPExcel(); 
$objPHPExcel->setActiveSheetIndex(0); 
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(10); 
$objPHPExcel->getActiveSheet()->getColumnDimension('B')->setWidth(15); 
$objPHPExcel->getActiveSheet()->getColumnDimension('C')->setWidth(15); 
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(15); 
$objPHPExcel->getActiveSheet()->getColumnDimension('E')->setWidth(15); 
$objPHPExcel->getActiveSheet()->setCellValue('A1', "{$this->_packInfos['o_id']}"); 
$objPHPExcel->getActiveSheet()->setCellValue('B1', "Volume weight (kg)"); 
$objPHPExcel->getActiveSheet()->setCellValue('D1', "Actual weight (kg)"); 
$objPHPExcel->getActiveSheet()->setCellValue('A2', "Box No."); 
$objPHPExcel->getActiveSheet()->setCellValue('B2', "Products"); 
$objPHPExcel->getActiveSheet()->setCellValue('C2', "Shipping Box"); 
$objPHPExcel->getActiveSheet()->setCellValue('D2', "System"); 
$objPHPExcel->getActiveSheet()->setCellValue('E2', "Input"); 
$objActSheet = $objPHPExcel->getActiveSheet(); 
$objActSheet->mergeCells("B1:C1"); 
$objActSheet->mergeCells("D1:E1"); 
$objPHPExcel->getActiveSheet()->getStyle('A1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); 
$objPHPExcel->getActiveSheet()->getStyle('B1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
$objPHPExcel->getActiveSheet()->getStyle('D1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
$objPHPExcel->getActiveSheet()->getStyle('A2'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); 
$objPHPExcel->getActiveSheet()->getStyle('B2'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
$objPHPExcel->getActiveSheet()->getStyle('C2'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
$objPHPExcel->getActiveSheet()->getStyle('D2'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
$objPHPExcel->getActiveSheet()->getStyle('E2'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER); 
if($this->_db->num_rows($query)>0) 
{ 
$i=3; 
while ($row = $this->_db->fetch_assoc($query)) 
{ 
$objPHPExcel->getActiveSheet()->setCellValue('A'.($i),"BOX ".$row['box_num']); 
$objPHPExcel->getActiveSheet()->setCellValue('B'.($i),sprintf("%.2f",$row['volume_weight'])); 
$objPHPExcel->getActiveSheet()->setCellValue('C'.($i),sprintf("%.2f",$row['box_weight'])); 
$objPHPExcel->getActiveSheet()->setCellValue('D'.($i),sprintf("%.2f",$row['system_weight'])); 
$objPHPExcel->getActiveSheet()->setCellValue('E'.($i),sprintf("%.2f",$row['real_weight'])); 
$objPHPExcel->getActiveSheet()->getStyle('A'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_LEFT); 
$objPHPExcel->getActiveSheet()->getStyle('B'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); 
$objPHPExcel->getActiveSheet()->getStyle('C'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); 
$objPHPExcel->getActiveSheet()->getStyle('D'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); 
$objPHPExcel->getActiveSheet()->getStyle('E'.($i))->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT); 
$i++; 
} 
} 
$fileName="exportBox.xls"; 
$filePath = dirname(dirname("__FILE__"))."/template/".$fileName; 
$path = "./template/".$fileName; 
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel); 
if(file_exists($path)){ 
chmod($path, 0777); 
unlink($path); 
$objWriter->save($path); 
header('application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename=weight-'.$this->_packInfos["o_id"].".xlsx"); 
readfile($filePath); 
die(); 
} 
else 
{ 
$objWriter->save($path); 
header('application/vnd.ms-excel'); 
header('Content-Disposition: attachment;filename=weight-'.$this->_packInfos["o_id"].".xlsx"); 
readfile($filePath); 
die(); 
}

注:上面的php生成excel的方式是直接使用A标签形式的,如果使用ajax,可以不使用header,直接echo $path,前台window.location.href=返回来的path就可以了。
PHP 相关文章推荐
PHP脚本的10个技巧(8)
Oct 09 PHP
php连接数据库代码应用分析
May 29 PHP
关于shopex同步ucenter的redirect问题,导致script不运行
Apr 10 PHP
PHP5.3与5.5废弃与过期函数整理汇总
Jul 10 PHP
phpstorm编辑器乱码问题解决
Dec 01 PHP
php结合正则批量抓取网页中邮箱地址
May 19 PHP
php使用Jpgraph绘制饼状图的方法
Jun 10 PHP
php算法实例分享
Jul 14 PHP
浅析ThinkPHP缓存之快速缓存(F方法)和动态缓存(S方法)(日常整理)
Oct 26 PHP
php实现图片上传并进行替换操作
Mar 15 PHP
PHP封装的MSSql操作类完整实例
May 26 PHP
laravel实现批量更新多条记录的方法示例
Oct 22 PHP
关于mysql字符集设置了character_set_client=binary 在gbk情况下会出现表描述是乱码的情况
Jan 06 #PHP
Could not load type System.ServiceModel.Activation.HttpModule解决办法
Dec 29 #PHP
PHP数组无限分级数据的层级化处理代码
Dec 29 #PHP
php学习笔记之面向对象编程
Dec 29 #PHP
php5.3中连接sqlserver2000的两种方法(com与ODBC)
Dec 29 #PHP
PHP中计算字符串相似度的函数代码
Dec 29 #PHP
PHP flock 文件锁详细介绍
Dec 29 #PHP
You might like
解析htaccess伪静态的规则
2013/06/18 PHP
php中通过curl检测页面是否被百度收录
2013/09/27 PHP
php读取富文本的时p标签会出现红线是怎么回事
2014/05/13 PHP
php使用APC实现实时上传进度条功能
2015/10/26 PHP
PHP7常量数组用法分析
2016/09/26 PHP
效率高的Javscript字符串替换函数的benchmark
2008/08/02 Javascript
SharePoint 客户端对象模型 (一) ECMA Script
2011/05/22 Javascript
jquery实现聚光灯效果的方法
2015/02/06 Javascript
微信小程序 页面跳转及数据传递详解
2017/03/14 Javascript
聊聊那些使用前端Javascript实现的机器学习类库
2017/09/18 Javascript
基于javascript 显式转换与隐式转换(详解)
2017/12/15 Javascript
深入浅析Vue.js中 computed和methods不同机制
2018/03/22 Javascript
jQuery序列化form表单数据为JSON对象的实现方法
2018/09/20 jQuery
layui table 表格上添加日期控件的两种方法
2019/09/28 Javascript
nest.js 使用express需要提供多个静态目录的操作方法
2019/10/24 Javascript
[04:52]2015国际邀请赛LGD战队晋级之路
2015/08/14 DOTA
在Python操作时间和日期之asctime()方法的使用
2015/05/22 Python
Python 多进程和数据传递的理解
2017/10/09 Python
Python实现字符串匹配的KMP算法
2019/04/04 Python
python 使用plt画图,去除图片四周的白边方法
2019/07/09 Python
Django 全局的static和templates的使用详解
2019/07/19 Python
Python + Requests + Unittest接口自动化测试实例分析
2019/12/12 Python
解决Opencv+Python cv2.imshow闪退问题
2020/04/24 Python
浅析Python 中的 WSGI 接口和 WSGI 服务的运行
2020/12/09 Python
python中封包建立过程实例
2021/02/18 Python
HTML5 Canvas实现360度全景图的示例代码
2018/01/29 HTML / CSS
HTML5 Blob 实现文件下载功能的示例代码
2019/11/29 HTML / CSS
帕克纽约:PARKER NY
2018/12/09 全球购物
Troy-Bilt官网:草坪割草机、吹雪机、分蘖机等
2019/02/19 全球购物
印度尼西亚最好的小工具在线商店:Erafone.com
2019/03/26 全球购物
篮球赛口号
2014/06/18 职场文书
教师节感谢信
2015/01/22 职场文书
机动车交通事故协议书
2015/01/29 职场文书
2015年艾滋病宣传活动总结
2015/03/27 职场文书
简单的辞职信范文(2016最新版)
2015/05/12 职场文书
债务纠纷起诉书
2015/05/20 职场文书