This section mainly sets the background color, border, font size, table width
<?PHPHeader("Content-type:text/html;charset=utf-8");require"./db.php";require"./phpexcel/phpexcel.php";//connecting to a database$db= Db::getinstance ();//instantiating an Excel class$objPHPExcel=NewPhpexcel ();$objSheet=$objPHPExcel->getactivesheet ();//gets the current active cell//Set Horizontal center$objSheet->getdefaultstyle ()->getalignment ()->setvertical (Phpexcel_style_alignment::vertical_center), Sethorizontal (phpexcel_style_alignment::vertical_center);//Set Font$objSheet->getdefaultstyle ()->getfont ()->setname (' Microsoft Ya Black ')->setsize (' 13 ');$objSheet->getstyle (' a2:z2 ')->getfont ()->setsize (' the ')->setbold (True);$objSheet->getstyle (' a3:z3 ')->getfont ()->setsize (' the ')->setbold (True);//Set Background color//query grade$gradeInfo=$db->getresult ("Select ' Grade ' from the user group by grade order by grade ASC");//Query Class$index= 0;foreach($gradeInfo as $k=$val) { $gradeIndex= Getcells ($index* 2); $objSheet->setcellvalue ($gradeIndex.‘ 2 ', ' High '.$val[' Grade ']); $classInfo=$db->getresult ("SELECT DISTINCT (Class) from user where grade =".$val[' Grade ']. " Order BY Class "); foreach($classInfo as $j=$VL) { $nameIndex= Getcells ($index* 2);//Name Column $scoreIndex= Getcells ($index* 2 + 1);//Fractional Columns $info=$db->getresult ("Select Username,score from User where class=".$VL[' class ']. and grade = ".$val[' Grade ']. " ORDER BY score Desc "); $objSheet->mergecells ($nameIndex.‘ 3: '.$scoreIndex.‘ 3 '); //set the class background color $objSheet->getstyle ($nameIndex.‘ 3: '.$scoreIndex.‘ 3 ')->getfill ()->setfilltype (phpexcel_style_fill::fill_solid)->getstartcolor ()->setRGB (' 51e351 '); //set the class border $classBorderStyle= Getborderstyle (' E351ca '); $objSheet->getstyle ($nameIndex.‘ 3: '.$scoreIndex.‘ 3 ')->applyfromarray ($classBorderStyle); $objSheet->setcellvalue ($nameIndex.‘ 3 ',$VL[' class ']. ' Class); //Set Wrap, note that you need to use double quotation marks to break the line $objSheet->getstyle ($nameIndex)->getalignment ()->setwraptext (true); $objSheet->setcellvalue ($nameIndex.‘ 4 ', ' name ' \ line ')->setcellvalue ($scoreIndex.‘ 4 ', ' fractions '); //Extra Long number formatting $objSheet->getstyle ($scoreIndex)->getnumberformat ()->setformatcode (Phpexcel_style_numberformat::format_text); $m= 5; foreach($info as $v) { //$objSheet->setcellvalue ($nameIndex. $m, $v [' username '])->setcellvalue ($scoreIndex. $m, $v [' score ']);// Fill Student Information//->setvalueexplicit (' + ', phpexcel_cell_datatype::type_numeric)//If there is an extra long number $objSheet->setcellvalue ($nameIndex.$m,$v[' username ']) ->setcellvalueexplicit ($scoreIndex.$m,$v[' Score ']. ' 123123123213123 ', Phpexcel_cell_datatype::type_string); $m++; } $index++; } $endGradeIndex= Getcells (($index*2-1));//get end of each grade cell//Combined grade cell $objSheet->mergecells ($gradeIndex.‘ 2: '.$endGradeIndex.‘ 2 ');//(' a2:f2 ') $objSheet->getstyle ($gradeIndex.‘ 2: '.$endGradeIndex.‘ 2 ')->getfill ()->setfilltype (phpexcel_style_fill::fill_solid)->getstartcolor ()->setRGB (' e36951 '); //Set Border $gradeBorderStyle= Getborderstyle (' e3df51 '); $objSheet->getstyle ($gradeIndex.‘ 2: '.$endGradeIndex.‘ 2 ')->applyfromarray ($gradeBorderStyle);}functionGetcells ($index) { $arr=Range(' A ', ' Z '); return $arr[$index];}//get a different borderfunctionGetborderstyle ($color) { $styleArray=Array( ' Borders ' =Array( ' Outline ' =Array( ' Style ' =>phpexcel_style_border::border_thick, ' color ' =Array(' rgb ' = =$color) ) ) ); return $styleArray;}//generate an Excel file in the specified format$objWriter= Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 ');//Generate Excel$objWriter->save (' style.xlsx ');
PHP build Excel (3)