Let's talk about this one. How to customize Excel's export and merge cells with PHP, here is the actual case, take a look.
First of all custom export, I use a Lado box of a plug-in, Baidu can be found a bit, in order to look good.
The value values correspond to the field values found in your database, and text corresponds to your table header information. OK, and then I pass these two values to our controller through get.
Introduce the export class, this is not much to say.
Then it is querying the database, data processing into a two-dimensional array, loop through the output in the table
My data format is 1-to-many relationship, a class teacher corresponding to a number of classes, then I want to merge the form in the class teacher, $count is the statistics of the class, when the class teacher
The corresponding class number is >1 when it is merged.
$str=$_GET['str'];//check
$str2=$_GET['str2'];//Header
$td_field=explode(',', $str2);//Header
$field=explode(',', $str);//check
$objPHPExcel=new \PHPExcel();
$objPHPExcel->getProperties()->setCreator('http://www.jb51.NET')
->setLastModifiedBy('http://www.jb51.Net')
->setTitle('Office 2007 XLSX Document')
->setSubject('Office 2007 XLSX Document')
->setDescription('Document for Office 2007 XLSX, generated using PHP classes.')
->setKeywords('office 2007 openxml php')
->setCategory('Result file');
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A1',$td_field[0])
->setCellValue('B1',$td_field[1])
->setCellValue('C1',$td_field[2])
->setCellValue('D1',$td_field[3])
->setCellValue('E1',$td_field[4])
->setCellValue('F1',$td_field[5])
->setCellValue('G1',$td_field[6])
->setCellValue('H1',$td_field[7])
->setCellValue('I1',$td_field[8])
->setCellValue('J1',$td_field[9])
->setCellValue('K1',$td_field[10])
->setCellValue('L1',$td_field[11])
->setCellValue('M1',$td_field[12]);
$i=2;
//->mergeCells('A18:E22') merge cells; ->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER) center vertically
foreach($new_array as $k=>$v){
if($v["count"]> 1){
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i,$v["$field[0]"])
->setCellValue('B'.$i,$v["$field[1]"])
->setCellValue('C'.$i,$v["$field[2]"])
->setCellValue('D'.$i,$v["$field[3]"])
->setCellValue('E'.$i,$v["$field[4]"])
->setCellValue('F'.$i,$v["$field[5]"])
->setCellValue('G'.$i,$v["$field[6]"])
->setCellValue('H'.$i,$v["$field[7]"])
->setCellValue('I'.$i,$v["$field[8]"])
->setCellValue('J'.$i,$v["$field[9]"])
->setCellValue('K'.$i,$v["$field[10]"])
->setCellValue('L'.$i,$v["$field[11]"])
->setCellValue('M'.$i,$v["$field[12]"])
->mergeCells('A'.$i.':A'.($i+$v["count"]-1))
->mergeCells('B'.$i.':B'.($i+$v["count"]-1))
->mergeCells('C'.$i.':C'.($i+$v["count"]-1))
->mergeCells('D'.$i.':D'.($i+$v["count"]-1))
->mergeCells('E'.$i.':E'.($i+$v["count"]-1))
->mergeCells('F'.$i.':F'.($i+$v["count"]-1))
->mergeCells('G'.$i.':G'.($i+$v["count"]-1))
->mergeCells('H'.$i.':H'.($i+$v["count"]-1))
->mergeCells('I'.$i.':I'.($i+$v["count"]-1));
$objPHPExcel->setActiveSheetIndex(0)->getStyle('A'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('B'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('C'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('D'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('E'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('F'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('G'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('H'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
$objPHPExcel->setActiveSheetIndex(0)->getStyle('I'.$i)->getAlignment()->setVertical(\PHPExcel_Style_Alignment::VERTICAL_CENTER);
}else{
$objPHPExcel->setActiveSheetIndex(0)
->setCellValue('A'.$i,$v["$field[0]"])
->setCellValue('B'.$i,$v["$field[1]"])
->setCellValue('C'.$i,$v["$field[2]"])
->setCellValue('D'.$i,$v["$field[3]"])
->setCellValue('E'.$i,$v["$field[4]"])
->setCellValue('F'.$i,$v["$field[5]"])
->setCellValue('G'.$i,$v["$field[6]"])
->setCellValue('H'.$i,$v["$field[7]"])
->setCellValue('I'.$i,$v["$field[8]"])
->setCellValue('J'.$i,$v["$field[9]"])
->setCellValue('K'.$i,$v["$field[10]"])
->setCellValue('L'.$i,$v["$field[11]"])
->setCellValue('M'.$i,$v["$field[12]"]);
}
$i++;
}
$objPHPExcel->getActiveSheet()->setTitle('daily');
$objPHPExcel->setActiveSheetIndex(0);
//$filename=urlencode('Data Table').'_'.date('Y-m-dHis');
$filename='daily newspaper'.'_'.date('Y-m-dHis');
//Generate xls file
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$filename.'.xls"');
header('Cache-Control: max-age=0');
$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');
$objWriter->save('php://output');
exit;