先上效果圖php
代碼有點蠢 大神勿噴數組
這樣的數據結構拿到後來計算合成單元格的跨度 好比: 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');