Phpexcel Exporting data to Excel

Source: Internet
Author: User
Public function Excel () {$data = $this->getexaminfo ();      Taught subject id $subject = D (' Exam ')->getteacherinfo ($this->authinfo);      $list = M (' score ')->where (Array (' acp_id ' = intval ($_get[' id ')))->order (' Sc_rank ASC ')->select ();        $auth = Getdatabyarray (' auth ', $list, ' a_id ', ' a_id,a_nickname ');      foreach ($list as & $value) {$value [' a_nickname '] = $auth [$value [' a_id ']][' a_nickname '];        } import (' @.org.phpexcel.phpexcel ');      Create New Phpexcel Object $objPHPExcel = new Phpexcel (); Set Properties $objPHPExcel->getproperties ()->setcreator ("CTOs")->setlastmodifiedby ("CTOs ")->settitle (" Office "xlsx Test Document")->setsubject ("Office" xlsx test Docume              NT ")->setdescription (" Test document for Office "XLSX, generated using PHP classes.") ->setkeywords ("Office openxml PHP")->sEtcategory ("Test result file");      Set width $objPHPExcel->getactivesheet ()->getcolumndimension (' A ')->setwidth (20);      $objPHPExcel->getactivesheet ()->getcolumndimension (' B ')->setwidth (20);      $objPHPExcel->getactivesheet ()->getcolumndimension (' C ')->setwidth (20);        $objPHPExcel->getactivesheet ()->getcolumndimension (' D ')->setwidth (20);        Set the row height $objPHPExcel->getactivesheet ()->getrowdimension (' 1 ')->setrowheight (22);        $objPHPExcel->getactivesheet ()->getrowdimension (' 2 ')->setrowheight (20);      Fonts and Styles $objPHPExcel->getactivesheet ()->getdefaultstyle ()->getfont ()->setsize (10);      $objPHPExcel->getactivesheet ()->getstyle (' A2:d2 ')->getfont ()->setbold (true);        $objPHPExcel->getactivesheet ()->getstyle (' A1 ')->getfont ()->setbold (true); $objPHPExcel->getactivesheet ()->getstyle (' A2:d2 ')->getalignment ()->sethorizontal (phpexcel_style_ AlignmEnt::horizontal_center); $objPHPExcel->getactivesheet ()->getstyle (' A2:d2 ')->getborders ()->getallborders ()        Setborderstyle (Phpexcel_style_border::border_thin); Set the horizontal center $objPHPExcel->getactivesheet ()->getstyle (' A1 ')->getalignment ()->sethorizontal (phpexcel_      Style_alignment::horizontal_center); $objPHPExcel->getactivesheet ()->getstyle (' A ')->getalignment ()->sethorizontal (phpexcel_style_      Alignment::horizontal_center); $objPHPExcel->getactivesheet ()->getstyle (' B ')->getalignment ()->sethorizontal (phpexcel_style_      Alignment::horizontal_center); $objPHPExcel->getactivesheet ()->getstyle (' C ')->getalignment ()->sethorizontal (phpexcel_style_      Alignment::horizontal_center); $objPHPExcel->getactivesheet ()->getstyle (' D ')->getalignment ()->sethorizontal (phpexcel_style_        Alignment::horizontal_center);        Merge $objPHPExcel->getactivesheet ()->mergecells (' a1:d1 '); Table Header $objPHpexcel->setactivesheetindex (0)->setcellvalue (' A1 ', $data [' Examtitle '])->setcellvalue (' A2 ', ' serial number ')->setcellvalue (' B2 ', ' name ')->setcellvalue (' C2 ', ' class ')->setce        Llvalue (' D2 ', ' grades '); Content for ($i = 0, $len = count ($list), $i < $len, $i + +) {$objPHPExcel->getactivesheet (0)->setcell Value (' A '.          ($i + 3), $list [$i] [' Sc_rank ']); $objPHPExcel->getactivesheet (0)->setcellvalue (' B ').          ($i + 3), $list [$i] [' a_nickname ']); $objPHPExcel->getactivesheet (0)->setcellvalue (' C ').          ($i + 3), $data [' title ']; $objPHPExcel->getactivesheet (0)->setcellvalue (' D ').          ($i + 3), $list [$i] [' sc_point ']); $objPHPExcel->getactivesheet ()->getstyle (' A '. ($i + 3). ':D '. ($i + 3))          ->getalignment ()->setvertical (Phpexcel_style_alignment::vertical_center); $objPHPExcel->getactivesheet ()->getstyle (' A '. ($i + 3). ':D '. ($i + 3)) -GetBorders ()->getallborders ()->setborderstyle (Phpexcel_style_border::border_thin);      $objPHPExcel->getactivesheet ()->getrowdimension ($i + 3)->setrowheight (16);        }//Rename sheet $objPHPExcel->getactivesheet ()->settitle ($data [' examtitle ']); Set Active sheet Index to the first sheet, so Excel opens this as the first sheet $objPHPExcel->setactiveshee        Tindex (0);      Output header (' Content-type:application/vnd.ms-excel '); Header (' content-disposition:attachment;filename= '. $data [' Examtitle '].      '. xls ');        Header (' cache-control:max-age=0 ');      $objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel5 ');  $objWriter->save (' php://output '); }

The above describes the Phpexcel export data to Excel, including the aspects of the content, I hope the PHP tutorial interested in a friend helpful.

  • 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.