php execl表格导出

PHP#数据Excel导出的一些策略

导出其实在任何类型的后端系统都比较常见,正规情况下excel导出的数据应该更多的用来做备份、底稿,理想情况下针对业务数据的任何业务操作都不应该依靠从后端业务系统导出数据然后人工进行干涉处理,但现实很骨感。。。

数据导出这类的功能在有条件的情况下是须要封装起来的,只有一个数据出口,在后续的业务规则调整变更时会真正的体现出它的价值。当一个典型的应用系统出现了五花八门的数据导出需求,说明这个应用系统的业务分析做的不够好或者很差,当用户只想用应用系统导出数据来辅助他们的业务流程时,最恐怖的情况就会是用户和数据库的距离只是隔着一个导出按钮。。。

环境

1.PHP5.5.14 (cli) (built: Sep  9 201419:09:25)

2.PHP Excel 1.7.8 (http://www.codeplex.com/PHPExcel)

处理逻辑

其实这类问题的关键点就是如何定义导出规则,定义的这套规则是否能适应业务流程,最基本的办法就是将数据导出的过程抽象为3个基础阶段,而后每个阶段可以再进行逐步的细化:

1.导出规则的定义

2.业务数据与导出规则的适配

3.导出规则解析构造Excel

示例

该示例实现了基本的导出功能,没有进行任何封装,没有关注性能或其它扩展性问题。

  1. <?php  
  2. namespace org\ybygjy\comp\excel;  
  3.   
  4. /** 
  5.  * Excel组件封装 
  6.  * <p>1.统一处理工程中针对excel的数据导出</p> 
  7.  * @author WangYanCheng 
  8.  * @version 2015-1-22 
  9.  */  
  10. class ExcelComp {  
  11.     /** 
  12.      * 构造函数 
  13.      */  
  14.     public function __construct() {  
  15.         require_once 'org/ybygjy/library/excel/PHPExcel.php';  
  16.     }  
  17.     /** 
  18.      * 测试入口 
  19.      */  
  20.     public function doTest() {  
  21.         //构造原始数据  
  22.         $dataArray = $this->buildData();  
  23.         //导出  
  24.         $this->doExportData($dataArray);  
  25.     }  
  26.     /** 
  27.      * 解析传递的原始数据并导出 
  28.      * @param $dataArr 
  29.      */  
  30.     public function doExportData($dataArr) {  
  31.         $phpObjExcel = new \PHPExcel();  
  32.         $worksSheet = $phpObjExcel->setActiveSheetIndex(0);  
  33.         //构造表头数据_Begin  
  34.         $tmpColTitles = [];  
  35.         $firstDataEntry = $dataArr[0];  
  36.         //分配列索引  
  37.         $colIndex = 0;  
  38.         foreach($firstDataEntry as $key => $val) {  
  39.             if (preg_match('/^_/'$key)) {  
  40.                 continue;  
  41.             }  
  42.             if (is_array($val)) {  
  43.                 //取array下的列名称  
  44.                 $val = $val[0];  
  45.                 $rowNums = count($val);  
  46.                 foreach ($val as $innerKey => $innerValue) {  
  47.                     $tmpColTitles[] = array(  
  48.                         'parentKey' => $key,  
  49.                         'key' => $innerKey,  
  50.                         'colIndex' => $colIndex  
  51.                     );  
  52.                     $colIndex++;  
  53.                 }  
  54.             } else {  
  55.                 $tmpColTitles[] = array(  
  56.                     'key'=>$key,  
  57.                     'colIndex'=>$colIndex  
  58.                 );  
  59.                 $colIndex++;  
  60.             }  
  61.         }  
  62.         for($i = 0; $i < count($tmpColTitles); $i++) {  
  63.             $tmpObj = $tmpColTitles[$i];  
  64.             $key = $tmpObj['key'];  
  65.             $colIndex = $tmpObj['colIndex'];  
  66.             $worksSheet->setCellValueByColumnAndRow($colIndex,1,$key);     
  67.         }  
  68.         //构造表头数据_End  
  69.         //填充单元格数据  
  70.         $currRow = 2;  
  71.         foreach ($dataArr as $dataEntry) {  
  72.             $mergeRow = $dataEntry['_DIMENSION'];  
  73.             foreach ($tmpColTitles as $colEntry) {  
  74.                 $key = $colEntry['key'];  
  75.                 $colIndex = $colEntry['colIndex'];  
  76.                 $parentKey = (isset($colEntry['parentKey']) ? $colEntry['parentKey'] : null);  
  77.                 if (empty($parentKey)) {  
  78.                     $value = $dataEntry[$key];  
  79.                     if ($mergeRow == 1) {  
  80.                         $worksSheet->setCellValueByColumnAndRow($colIndex$currRow$value);  
  81.                     } else {  
  82.                         $worksSheet->mergeCellsByColumnAndRow($colIndex$currRow$colIndex, ($currRow + $mergeRow - 1))->setCellValueByColumnAndRow($colIndex$currRow$value);  
  83.                     }  
  84.                 } else {  
  85.                     $tmpDataArr = $dataEntry[$parentKey];  
  86.                     $innerRow = $currRow;  
  87.                     for($index = 0; $index < count($tmpDataArr); $index++) {  
  88.                         $innerDataEntry = $tmpDataArr[$index];  
  89.                         $value = $innerDataEntry[$key];  
  90.                         $worksSheet->setCellValueByColumnAndRow($colIndex$innerRow$value);  
  91.                         $innerRow++;  
  92.                     }  
  93.                 }  
  94.             }  
  95.             $currRow += $mergeRow;  
  96.         }  
  97.         header('Content-Type: application/vnd.ms-excel');  
  98.         header('Content-Type: application/force-download');  
  99.         header('Content-Type: application/octet-stream');  
  100.         header('Content-Type: application/download');  
  101.         header('Content-Disposition: attachment;filename="HelloWord.xls"');  
  102.         header('Cache-Control: max-age=0');  
  103.         header('Cache-Control: max-age=1');  
  104.         header('Cache-Control: no-cache, must-revalidate');  
  105.         header('Pragma: public');  
  106.         $objWriter = \PHPExcel_IOFactory::createWriter($phpObjExcel'Excel5');  
  107.         $objWriter->save('php://output');  
  108.     }  
  109.     /** 
  110.      * 构造测试数据 
  111.      * @return multitype:multitype:string number multitype:multitype:string 
  112.      */  
  113.     private function buildData() {  
  114.         $rtnData = array(  
  115.             array(  
  116.                 'name'=>'YanCheng_01',  
  117.                 'age'=>'20',  
  118.                 'addr'=>array(  
  119.                     array(  
  120.                         'country'=>'China',  
  121.                         'province'=>'ShanDong'  
  122.                     ),  
  123.                     array(  
  124.                         'country'=>'China',  
  125.                         'province'=>'BeiJing'  
  126.                     )  
  127.                 ),  
  128.                 '_DIMENSION'=>2  
  129.             ),  
  130.             array(  
  131.                 'name'=>'YanCheng_02',  
  132.                 'age'=>'21',  
  133.                 'addr'=>array(  
  134.                     array(  
  135.                         'country'=>'China',  
  136.                         'province'=>'LanZhou'  
  137.                     ),  
  138.                     array(  
  139.                         'country'=>'China',  
  140.                         'province'=>'NingXia'  
  141.                     )  
  142.                 ),  
  143.                 '_DIMENSION'=>2  
  144.             ),  
  145.             array(  
  146.                 'name'=>'YanCheng_03',  
  147.                 'age'=>'22',  
  148.                 'addr'=>array(  
  149.                     array(  
  150.                         'country'=>'China',  
  151.                         'province'=>'JiaYuGuan'  
  152.                     )  
  153.                 ),  
  154.                 '_DIMENSION'=>1  
  155.             )  
  156.         );  
  157.         return $rtnData;  
  158.     }  

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值