How to customize Excel's export and merge cells with PHP

零到壹度
Release: 2023-03-21 20:08:01
Original
4722 people have browsed it

This time I will talk to you about how to use PHP to customize Excel's export and merge cells. The following is a practical case, let's take a look.

First of all, customize the export. I use a plug-in for a drop-down multi-select box, which can be found on Baidu. In order to make the style look good. As shown in the figure

value corresponds to the field value found in your database, and text corresponds to your header information. Ok, then I pass these two values ​​​​to our controller through GET.

Introducing exported classes, I won’t go into details about this.

Then we query the database, process the data into a two-dimensional array, loop through it and output it in the table

My data format is a 1-to-many relationship, one class teacher corresponds to multiple classes , then I want to merge this class teacher in the table. $count is the statistics of the class. When the number of classes corresponding to the class teacher

is >1, it will be merged.

            $str=$_GET['str'];//勾选
            $str2=$_GET['str2'];//表头
            $td_field=explode(',', $str2);//表头
            $field=explode(',', $str);//勾选
            $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')合并单元格;->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER) 垂直居中
            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('日报');
            $objPHPExcel->setActiveSheetIndex(0);
            //$filename=urlencode('数据表').'_'.date('Y-m-dHis');
            $filename='日报'.'_'.date('Y-m-dHis');

       

            //生成xls文件
            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;
Copy after login

The above is the detailed content of How to customize Excel's export and merge cells with PHP. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!