PHPEXCEL 使用小记

2015-01-24信息快讯网

下面的文章主要是介绍基于PHPEXCEL实现的一些excel的操作实现代码,需要的朋友可以参考下

首先是使用PHP Reader 读取Excle内容:
 
require("http://www.jb51.net/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使用异步调用获取数据时出现(错误c00ce56e导致此项操作无法完成)
解决PHP mysql_query执行超时(Fatal error: Maximum execution time …)
处理(php-cgi.exe - FastCGI 进程超过了配置的请求超时时限)的问题
解析array splice的移除数组中指定键的值,返回一个新的数组
解析PHP跳出循环的方法以及continue、break、exit的区别介绍
浅析Dos下运行php.exe,出现没有找到php_mbstring.dll 错误的解决方法
php selectradio和checkbox默认选择的实现方法详解
php setcookie(name, value, expires, path, domain, secure) 参数详解
浅析memcache启动以及telnet命令详解
解析php中如何直接执行SHELL
领悟php接口中interface存在的意义
比较strtr, str_replace和preg_replace三个函数的效率
基于php导出到Excel或CSV的详解(附utf8、gbk 编码转换)
解析PHPExcel使用的常用说明以及把PHPExcel整合进CI框架的介绍
php 备份数据库代码(生成word,excel,json,xml,sql)
基于PHPExcel的常用方法总结
eAccelerator的安装与使用详解
PDO版本问题 Invalid parameter number: no parameters were bound
关于mysql字符集设置了character_set_client=binary 在gbk情况下会出现表描述是乱码的情况
Could not load type System.ServiceModel.Activation.HttpModule解决办法
phpexcel导出excel的颜色和网页中的颜色显示不一致
用Simple Excel导出xls实现方法
php读取EXCEL文件 php excelreader读取excel文件
php excel reader读取excel内容存入数据库实现代码
php中explode与split的区别介绍
PHPExcel读取Excel文件的实现代码
©2014-2024 dbsqp.com