Using the Phpexcel class to export the XLS document format code example
For convenience, the XLS document was previously exported using CSV mode. Now that the demand has changed, you want to export the document to be presented in the form of merged cells. This time use Phpexcel to generate and format cells, such as width, center, number format, and so on.
The basic steps are: Load the phpexcel.php file, instantiate the Phpexcel class, write the data, and format the cell.
The effect is as follows:
The code is as follows:
Setactivesheetindex (0);//Set activity Sheet$objactsheet = $phpexcel->getactivesheet ();//Get current active sheet Object $objactsheet-> Setcellvalue (' A1 ', ' number ')->mergecells (' A1:a2 '), $objActSheet->setcellvalue (' B1 ', ' Pedestrian ')->mergecells (' B1:d 1 '); $objActSheet->setcellvalue (' B2 ', ' name '), $objActSheet->setcellvalue (' C2 ', ' age '); $objActSheet Setcellvalue (' D2 ', ' gender '), $objActSheet->setcellvalue (' E1 ', ' time ')->mergecells (' e1:e2 '); $objActSheet Setcellvalue (' A3 ', ' 201512010001 '), $objActSheet->setcellvalue (' B3 ', ' Xiao Zhang '), $objActSheet->setcellvalue (' C3 '), $objActSheet->setcellvalue (' D3 ', ' Male '), $objActSheet->setcellvalue (' E3 ', ' 2015-12-01 '); $objActSheet- >setcellvalue (' A4 ', ' 201512010002 '); $objActSheet->setcellvalue (' B4 ', ' little Sister '); $objActSheet->setcellvalue (' C4 ', ', '), $objActSheet->setcellvalue (' D4 ', ' female '), $objActSheet->setcellvalue (' E4 ', ' 2015-12-02 '); $objStyleA 1 = $objActSheet->getstyle (' A1 ');//Set the number format of the contents of the cell, the original content is all displayed $objstylea1->getnumberformat ()->setformatcode ( Phpexcel_style_numberformat::format_number);//set horizontally centered, vertically centered $objstylea1->getalignment ()->setvertical (phpexcel_style_ Alignment::vertical_center); $objStyleA 1->getalignment ()->sethorizontal (phpexcel_style_alignment:: Horizontal_center);//Copy style information from the specified cell. $objActSheet->duplicatestyle ($objStyleA 1, ' a1:e4 ');//Set the width of the cell $ Objactsheet->getcolumndimension (' a ')->setwidth, $objActSheet->getcolumndimension (' a ')->setwidth $objActSheet->getcolumndimension (' a ')->setwidth (' A '), $objActSheet->getcolumndimension (' a ') SetWidth, $objActSheet->getcolumndimension (' A ')->setwidth (+), header ("content-type:application/ Force-download "); Header (" Content-type:application/octet-stream "); Header (" Content-type:application/download "); Header (' content-disposition:inline;filename= ' Demo.xls '); Header ("Content-transfer-encoding:binary"); Header (" Expires:mon, 1997 05:00:00 GMT "); Header (" Last-modified: ". Gmdate ("D, D M Y h:i:s"). "GMT"); Header ("Cache-control:must-revalidate, post-cHeck=0, pre-check=0 "); Header (" Pragma:no-cache "); $objWriter = Phpexcel_iofactory::createwriter ($phpexcel, ' Excel5 ') ; $objWriter->save (' php://output ');? >