Export data as an Excel document using the Phpexcel class library

Source: Internet
Author: User
Tags getcolor

Download Phpexcel class library, http://phpexcel.codeplex.com/, extract the Phpexcel folder and phpexcel.php file in class, and copy it into your project code, here's how to export it as an Excel file

<?phpfunction export_data ()  {require_once (' phpexcel.php ');//  document name $filename =  ' FileName ';//  create  PHPExcel  Object $objphpexcel = new phpexcel ();//  set the generated document properties $ Objphpexcel->getproperties ()->setcreator ("Http://www.jianbin.info")->setlastmodifiedby ("HTTP/ Www.jianbin.info ")->settitle (" Http://www.jianbin.info ")->setsubject (" Http://www.jianbin.info ") SetDescription ("Http://www.jianbin.info")->setkeywords ("Http://www.jianbin.info")->setcategory ("HTTP/ Www.jianbin.info "), $col _number = 65; $col _letter = chr ($col _number); $row _number =  1;/** $table   Data Format array (' Head '  => array (Field 1, Field 2,..., field N), ' body '  => array (' Row 1 '  =>  (data 1, data 2,..., data N), ' Line 2 '  =>  (data 1, data 2,..., data N),... ' row n '  =>  (data 1, Data 2, (.., data N),)  */foreach  ($table [' head '] as  $key = + $val) {$objPHPExcel Setactivesheetindex (0)->setcellvalue ("{$col _letter}{$row _number} ",  $val), $objPHPExcel->getactivesheet ()->getcolumndimension (" {$col _letter} ") Setautosize (TRUE);//  automatic list setting is not supported for Chinese $objphpexcel->getactivesheet ()->getstyle ("{$col _letter}{$row _ Number} ")->getfont ()->setbold (true), $objPHPExcel->getactivesheet ()->getstyle (" {$col _letter}{$row _ Number} ")->getalignment ()-> sethorizontal (phpexcel_style_alignment::horizontal_center); $ Objphpexcel->getactivesheet ()->getstyle ("{$col _letter}{$row _number}")->getfill ()->setfilltype ( Phpexcel_style_fill::fill_solid); $objPHPExcel->getactivesheet ()->getstyle ("{$col _letter}{$row _number}")- >getfill ()->getstartcolor ()->setargb (' FF58ACFA '); $objPHPExcel->getactivesheet ()->getstyle ("{$ col_letter}{$row _number} ")->getborders ()->gettop ()->setborderstyle (Phpexcel_style_border::border_thin ); $objPHPExcel->getactivesheet ()->getstyle ("{$col _letter}{$row _number}")->getborders ()->getleft ()- >setborderstyle (Phpexcel_style_bordEr::border_thin); $objPHPExcel->getactivesheet ()->getstyle ("{$col _letter}{$row _number}")->getborders () ->getright ()->setborderstyle (Phpexcel_style_border::border_thin), $objPHPExcel->getactivesheet () GetStyle ("{$col _letter}{$row _number}")->getborders ()->getbottom ()->setborderstyle (phpexcel_style_ Border::border_thin), $objPHPExcel->getactivesheet ()->getstyle ("{$col _letter}{$row _number}") GetBorders ()->gettop ()->getcolor ()->setargb (' FFFF0000 '); $col _number++; $col _LETTER&NBSP;=&NBSP;CHR ($ Col_number);} $row _number++;//  Construction bodyforeach  ($table [' body '] as  $row) {$col _number = 65; $col _ LETTER&NBSP;=&NBSP;CHR ($col _number);foreach  ($row  as  $val)  {$objPHPExcel Setactivesheetindex (0)->setcellvalue ("{$col _letter}{$row _number}",  $val); $objPHPExcel Getactivesheet ()->getstyle ("{$col _letter}{$row _number}")->getfont ()->setbold (true); $objPHPExcel Getactivesheet ()->getstyle ("{$col _letter}{$row _number} ")->getalignment ()-> sethorizontal (phpexcel_style_alignment::horizontal_center); $ Objphpexcel->getactivesheet ()->getstyle ("{$col _letter}{$row _number}")->getborders ()->gettop () Setborderstyle (Phpexcel_style_border::border_thin); $objPHPExcel->getactivesheet ()->getstyle ("{$col _ letter}{$row _number} ")->getborders ()->getleft ()->setborderstyle (Phpexcel_style_border::border_thin); $ Objphpexcel->getactivesheet ()->getstyle ("{$col _letter}{$row _number}")->getborders ()->getright ()- >setborderstyle (Phpexcel_style_border::border_thin); $objPHPExcel->getactivesheet ()->getstyle ("{$col _ letter}{$row _number} ")->getborders ()->getbottom ()->setborderstyle (Phpexcel_style_border::border_thin) ; $objPHPExcel->getactivesheet ()->getstyle ("{$col _letter}{$row _number}")->getborders ()->gettop ()- >getcolor ()->setargb (' FFFF0000 '); $col _number++; $col _letter = chr ($col _number);} $row _number++;}   Rename a worksheet $objphpExcel->getactivesheet ()->settitle ($filename);//  open Sheet by default 1$objphpexcel->setactivesheetindex (0);//   Set header output to client header (' Content-type: application/vnd.ms-excel '); header (' content-disposition:  Attachment;filename= "'. $filename. '. XLS "'); header (' cache-control: max-age=0 ');// if you ' Re serving to ie 9,  then the following may be neededheader (' cache-control: max-age=1 ');//  if you ' re serving to ie over ssl, then the following  may be neededheader  (' expires: mon, 26 jul 1997 05:00:00 gmt ');  // Date in the pastheader  (' last-modified:  ' gmdate (' d, d m  y h:i:s '). '  gmt '); // always modifiedheader  (' cache-control: cache, must-revalidate ');  // HTTP/1.1header  (' pragma: public ');  // http/1.0$obJwriter = phpexcel_iofactory::createwriter ($objPHPExcel,  ' Excel5 '); $objWriter->save (' php:// Output '); exit;}

The exported file opens with the following effect:

Export data as an Excel document using the Phpexcel class library

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.