PHPExcel导出动态合并单元格-CI

先上效果图

代码有点蠢 大神勿喷

这样的数据结构拿到后来计算合成单元格的跨度 比如: A1:A30 B1:E1 这种

$arr = [
['year'=>'2018','month'=>'08','day'=>'10'],
['year'=>'2018','month'=>'08','day'=>'11'],
['year'=>'2018','month'=>'09','day'=>'12'],
['year'=>'2018','month'=>'09','day'=>'13']
];

//通过上面的代码拿到
$a = ['A2:A5']; $b = ['B2:B3','B4:B5'];

//数组排序一定是以上这样有序的,不是下面这种无须的
$arr = [
['year'=>'2018','month'=>'08','day'=>'10'], //08
['year'=>'2018','month'=>'09','day'=>'11'], //09
['year'=>'2018','month'=>'08','day'=>'12'], //08
['year'=>'2018','month'=>'09','day'=>'13'] //09
];

附上全部代码 CI框架写的

 1         $arr = [
 2             ['year'=>'2018','month'=>'08','day'=>'10'],
 3             ['year'=>'2018','month'=>'08','day'=>'11'],
 4             ['year'=>'2018','month'=>'09','day'=>'12'],
 5             ['year'=>'2018','month'=>'09','day'=>'13']
 6         ];
 7         $A = [];
 8         $year= $arr[0]['year'];
 9         $s = 2;
10         $e = 1;
11         $B = [];
12         $month= $arr[0]['month'];
13         $s1 = 2;
14         $e1 = 1;
15         foreach ($arr  as $k=> $v){
16             if($v['year'] !=$year){
17                 $A[]="A".$s.":A".$e."";
18                 $e++;
19                 $year= $v['year'];
20                 $s = $e;
21             }else{
22                 $e++;
23                 if(count($arr)==($k+1)){
24                     $A[]="A".$s.":A".$e."";
25                 }
26             }
27             if($v['month'] !=$month){
28                 $B[]="B".$s1.":B".$e1."";
29                 $e1++;
30                 $month = $v['month'];
31                 $s1 = $e1;
32             }else{
33                 $e1++;
34                 if(count($arr)==($k+1)){
35                     $B[]="B".$s1.":B".$e1."";
36                 }
37             }
38         }
39         $title = ['','',''];
40         $filename =  '年月日';
41         ob_end_clean();
42         $obj =& get_instance();
43         $obj->load->library('phpexcel');
44         $obj->load->library('PHPExcel/IOFactory');
45         //创建PHPExcel实例
46         $cellArr = 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');
47         $cellNames = ['A', 'B', 'C', 'D', 'E', 'F','G'];
48         $cellName = [];
49         foreach ($cellNames as $key => $val) {
50             foreach ($cellArr as $k => $v) {
51                 $cellName[] = $val.$v;
52             }
53         }
54         $cellName = array_merge($cellArr,$cellName);
55         /* @实例化 */
56         $obpe = new PHPExcel();
57         /* 设置宽度 */
58         $obpe->getActiveSheet()->getColumnDimension()->setAutoSize(true);
59         $obpe->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);
60         $obpe->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
61         //设置SHEET
62         $obpe->setactivesheetindex(0);
63         $obpe->getActiveSheet()->setTitle('sheet1');
64         $_row = 1;   //设置纵向单元格标识
65         foreach ($title as $k => $v) {
66             $obpe->getactivesheet()->setCellValue($cellName[$k].$_row, $v);
67         }
68         $i = 1;
69         foreach($arr AS $_v){
70             $j = 0;
71             foreach($_v AS $_cell){
72                 $obpe->getActiveSheet()->setCellValue($cellName[$j] . ($i+$_row), $_cell);
73                 $j++;
74             }
75             $i++;
76         }
77         foreach ($A as $aa){
78             $obpe->getActiveSheet()->mergeCells($aa);
79         }
80         foreach ($B as $bb){
81             $obpe->getActiveSheet()->mergeCells($bb);
82         }
83         //输出到浏览器
84 
85         $write = new PHPExcel_Writer_Excel2007($obpe);
86         header("Pragma: public");
87         header("Expires: 0");
88         header("Cache-Control:must-revalidate, post-check=0, pre-check=0");
89         header("Content-Type:application/force-download");
90         header("Content-Type:application/vnd.ms-execl");
91         header("Content-Type:application/octet-stream");
92         header("Content-Type:application/download");
93         header('Content-Disposition:attachment;filename="'.$filename.'.xlsx"');
94         header("Content-Transfer-Encoding:binary");
95         $write->save('php://output');

猜你喜欢

转载自www.cnblogs.com/phper8/p/9398658.html
今日推荐