php 封装 dll,PHP封装Excel表方法使用流程

今天总结了一下Excel表的封装和导出使用,原理

经常使用与一些日常报表, 数据报表, 实现方法比较简单, 一次封装, 简单的方法调用,简单~ 废话不多说,直接入正题, 先说下重要的参数要记住的东西

impUser() 导入方法

exportExcel($xlsName,$xlsCell,$xlsData);导出方法

PHPExcel.php PHP主文件, 放置各种class结合核心文件

其实能实现PHP_EXCEL的方法有很多, 我简单的介绍一种简单粗暴我用过的调用插件方法.

下面介绍的是使用TP3.2框架+PHP_EXCEL 来导出数据

1: 在php.ini配置文件中开启扩展, php_zip.dll

2:PHP_EXCEL使用的是一个外部插件,下载网站http://phpexcel.codeplex.com/

下载之后打开能看到这样

64cc37f4c298

64cc37f4c298

打开CLASSES文件夹

把里面的文件拉到 框架里面中, ThinkPHP/Library/Vendor/

64cc37f4c298

3: 因为涉及到调用此类的方法,所以我们要在框架的 公共控制器函数中比如我的这个是所有控制器都继承的这个主方法

64cc37f4c298

我们就打开这个主方法里面

64cc37f4c298

把这段代码方在里面, 方便我们调用

/**

+----------------------------------------------------------

* Export Excel | 2013.08.23

* Author:HongPing

+----------------------------------------------------------

* @param $expTitle string File name

+----------------------------------------------------------

* @param $expCellName array Column name

+----------------------------------------------------------

* @param $expTableData array Table data

+----------------------------------------------------------

*/

public function exportExcel($expTitle,$expCellName,$expTableData){

$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称

$fileName = $_SESSION['loginAccount'].date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定

$cellNum = count($expCellName);

$dataNum = count($expTableData);

vendor("PHPExcel.PHPExcel");

$objPHPExcel = new PHPExcel();

$cellName = 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','AA','AB','AC','AD','AE','AF','AG','AH','AI','AJ','AK','AL','AM','AN','AO','AP','AQ','AR','AS','AT','AU','AV','AW','AX','AY','AZ');

$objPHPExcel->getActiveSheet(0)->mergeCells('A1:'.$cellName[$cellNum-1].'1');//合并单元格

$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $expTitle.' Export time:'.date('Y-m-d H:i:s'));

for($i=0;$i

$objPHPExcel->setActiveSheetIndex(0)->setCellValue($cellName[$i].'2', $expCellName[$i][1]);

}

// Miscellaneous glyphs, UTF-8

for($i=0;$i

for($j=0;$j

$objPHPExcel->getActiveSheet(0)->setCellValue($cellName[$j].($i+3), $expTableData[$i][$expCellName[$j][0]]);

}

}

header('pragma:public');

header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');

header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印

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

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

exit;

}

/**

+----------------------------------------------------------

* Import Excel | 2013.08.23

* Author:HongPing

+----------------------------------------------------------

* @param $file upload file $_FILES

+----------------------------------------------------------

* @return array array("error","message")

+----------------------------------------------------------

*/

public function importExecl($file){

if(!file_exists($file)){

return array("error"=>0,'message'=>'file not found!');

}

Vendor("PHPExcel.PHPExcel.IOFactory");

$objReader = PHPExcel_IOFactory::createReader('Excel5');

try{

$PHPReader = $objReader->load($file);

}catch(Exception $e){}

if(!isset($PHPReader)) return array("error"=>0,'message'=>'read error!');

$allWorksheets = $PHPReader->getAllSheets();

$i = 0;

foreach($allWorksheets as $objWorksheet){

$sheetname=$objWorksheet->getTitle();

$allRow = $objWorksheet->getHighestRow();//how many rows

$highestColumn = $objWorksheet->getHighestColumn();//how many columns

$allColumn = PHPExcel_Cell::columnIndexFromString($highestColumn);

$array[$i]["Title"] = $sheetname;

$array[$i]["Cols"] = $allColumn;

$array[$i]["Rows"] = $allRow;

$arr = array();

$isMergeCell = array();

foreach ($objWorksheet->getMergeCells() as $cells) {//merge cells

foreach (PHPExcel_Cell::extractAllCellReferencesInRange($cells) as $cellReference) {

$isMergeCell[$cellReference] = true;

}

}

for($currentRow = 1 ;$currentRow<=$allRow;$currentRow++){

$row = array();

for($currentColumn=0;$currentColumn

$cell =$objWorksheet->getCellByColumnAndRow($currentColumn, $currentRow);

$afCol = PHPExcel_Cell::stringFromColumnIndex($currentColumn+1);

$bfCol = PHPExcel_Cell::stringFromColumnIndex($currentColumn-1);

$col = PHPExcel_Cell::stringFromColumnIndex($currentColumn);

$address = $col.$currentRow;

$value = $objWorksheet->getCell($address)->getValue();

if(substr($value,0,1)=='='){

return array("error"=>0,'message'=>'can not use the formula!');

exit;

}

if($cell->getDataType()==PHPExcel_Cell_DataType::TYPE_NUMERIC){

$cellstyleformat=$cell->getParent()->getStyle( $cell->getCoordinate() )->getNumberFormat();

$formatcode=$cellstyleformat->getFormatCode();

if (preg_match('/^([$[A-Z]*-[0-9A-F]*])*[hmsdy]/i', $formatcode)) {

$value=gmdate("Y-m-d", PHPExcel_Shared_Date::ExcelToPHP($value));

}else{

$value=PHPExcel_Style_NumberFormat::toFormattedString($value,$formatcode);

}

}

if($isMergeCell[$col.$currentRow]&&$isMergeCell[$afCol.$currentRow]&&!empty($value)){

$temp = $value;

}elseif($isMergeCell[$col.$currentRow]&&$isMergeCell[$col.($currentRow-1)]&&empty($value)){

$value=$arr[$currentRow-1][$currentColumn];

}elseif($isMergeCell[$col.$currentRow]&&$isMergeCell[$bfCol.$currentRow]&&empty($value)){

$value=$temp;

}

$row[$currentColumn] = $value;

}

$arr[$currentRow] = $row;

}

$array[$i]["Content"] = $arr;

$i++;

}

spl_autoload_register(array('Think','autoload'));//must, resolve ThinkPHP and PHPExcel conflicts

unset($objWorksheet);

unset($PHPReader);

unset($PHPExcel);

unlink($file);

return array("error"=>1,"data"=>$array);

}

OK.放进去之后,不是就可以用了, 我们还要通过我们的实际路径更改一下.

4: 现在我们可以开始来调用了

在我们使用的控制器里面,调用此方法

现在我要做一个数据导出的操作

64cc37f4c298

64cc37f4c298

注意此调用方法

现在我们来使用一下

64cc37f4c298

64cc37f4c298

看到Array的数据跟这个顺序是正比的

PHP封装Excel表方法使用流程

OK. 完成了数据导出功能~

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值