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也可以?成Shell Script
Oct 09 PHP
一些被忽视的PHP函数(简单整理)
Apr 30 PHP
PHP排序算法的复习和总结
Feb 15 PHP
PHP数字字符串左侧补0、字符串填充和自动补齐的几种方法
May 10 PHP
PHP统计nginx访问日志中的搜索引擎抓取404链接页面路径
Jun 30 PHP
php快速排序原理与实现方法分析
May 26 PHP
PHP入门教程之会话控制技巧(cookie与session)
Sep 11 PHP
php抽象方法和抽象类实例分析
Dec 07 PHP
PHP基于pdo的数据库操作类【可支持mysql、sqlserver及oracle】
May 21 PHP
PHP+mysql实现的三级联动菜单功能示例
Feb 15 PHP
PHP递归的三种常用方式
Feb 28 PHP
PHP7匿名类的用法示例
Apr 05 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
PHP5/ZendEngine2的改进
2006/10/09 PHP
Laravel 5 框架入门(四)完结篇
2015/04/09 PHP
根据IP的地址,区分不同的地区,查看不同的网站页面的js代码
2013/02/26 Javascript
举例讲解AngularJS中的模块
2015/06/17 Javascript
JS简单模拟触发按钮点击功能的方法
2015/11/30 Javascript
Node.js静态文件服务器改进版
2016/01/10 Javascript
jQuery实现多级联动下拉列表查询框
2016/01/18 Javascript
vue2.0+webpack环境的构造过程
2016/11/08 Javascript
解决layui动态添加的元素click等事件触发不了的问题
2019/09/20 Javascript
Vue el-autocomplete远程搜索下拉框并实现自动填充功能(推荐)
2019/10/25 Javascript
JavaScript创建表格的方法
2020/04/13 Javascript
原生jQuery实现只显示年份下拉框
2020/12/24 jQuery
在漏洞利用Python代码真的很爽
2007/08/26 Python
基于Python的文件类型和字符串详解
2017/12/21 Python
python 2.7.14安装图文教程
2018/04/08 Python
Python3处理HTTP请求的实例
2018/05/10 Python
Python3.6简单反射操作示例
2018/06/14 Python
Python实现最常见加密方式详解
2019/07/13 Python
Python实现把多维数组展开成DataFrame
2019/11/30 Python
Python描述符descriptor使用原理解析
2020/03/21 Python
Django windows使用Apache实现部署流程解析
2020/10/12 Python
香港交友网站:be2香港
2018/07/22 全球购物
将一个文本文件的内容按倒序打印出来
2015/01/05 面试题
为什么需要版本控制
2016/10/28 面试题
成人大专自我鉴定范文
2013/10/19 职场文书
大课间活动实施方案
2014/03/06 职场文书
建筑节能汇报材料
2014/08/22 职场文书
支部书记四风问题自我剖析材料
2014/09/29 职场文书
分居协议书范本(律师见证版)
2014/11/26 职场文书
五一劳动节慰问信
2015/02/14 职场文书
如何写辞职书
2015/02/26 职场文书
党员转正大会主持词
2015/07/02 职场文书
Python OpenCV快速入门教程
2021/04/17 Python
Redis IP地址的绑定的实现
2021/05/08 Redis
业余无线电通联Q语
2022/02/18 无线电
mysql的单列多值存储实例详解
2022/04/05 MySQL