PHP Export Excel Instance explained

Source: Internet
Author: User
This implementation of PHP exported Excel file using PHP Open source program phpexcel, some key code to share to everyone, the specific content is as follows

<?phperror_reporting (E_all);d ate_default_timezone_set (' Asia/shanghai '); require_once './Classes/PHPExcel.php ' ; $data =array (0=>array ' id ' =>1001, ' username ' and ' Zhang Fei ', ' password ' = ' 123456 ', ' address ' = ' and ' The Three Kingdoms ' Gao 250 Lane, Room 101 '), 1=>array (' id ' =>1002, ' username ' = ' Guan Yu ', ' password ' = ' 123456 ', ' address ' = ' + ' When the Three Kingdoms Huaguoshan  '), 2=>array (' id ' =>1003, ' username ' = ' cao ', ' password ' = ' 123456 ', ' address ' = ' 2055 Lane 3rd Yanan West Road ' ), 3=>array (' id ' =>1004, ' username ' and ' Liu Bei ', ' password ' = ' 654321 ', ' Address ', ' No. 188th, ' Yu Yuan Road, Room 3309 ') ; $objPHPExcel =new Phpexcel (); $objPHPExcel->getproperties ()->setcreator (' http://www.jb51.net ')->set Lastmodifiedby (' http://www.jb51.net ')->settitle (' Office of XLSX Document ')->setsubje CT (' Office of XLSX document ')->setdescription (' Document for Office ' XLSX, generated using PHP clas               Ses. ') ->setkeyworDS (' Office openxml php ')->setcategory (' Result file '); $objPHPExcel->setactivesheetindex (0)- >setcellvalue (' A1 ', ' ID ')->setcellvalue (' B1 ', ' username ')->setcellvalue (' C1 ', ' Password ')->setcellvalue (' D     1 ', ' address '); $i = 2; foreach ($data as $k = + $v) {$objPHPExcel->setactivesheetindex (0)->setcellvalue (' A '. $i, $v [' id '])->se Tcellvalue (' B '. $i, $v [' username '])->setcellvalue (' C ' $i, $v [' Password '])->setcellvalue (' D '. $i, $v [' Address '  ]); $i + +;} $objPHPExcel->getactivesheet ()->settitle (' Third grade 2 '); $objPHPExcel->setactivesheetindex (0); $filename = UrlEncode (' Student information tables '). ' _ '. Date (' Y-m-dhis '); Generate xlsx file/*header (' Content-type:application/vnd.openxmlformats-officedocument.spreadsheetml.sheet '); Header (' Content-disposition:attachment;filename= "'. $filename. '. Xlsx "'); header (' cache-control:max-age=0 '); $objWriter =phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 ') ); *///generates the XLS file 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;

Note: If garbled when exporting Chinese, you can try to convert the string to gb2312 code similar to the following:

View Code Printing

$str =mb_convert_encoding ("gb2312", "UTF-8", $str);

The above is the whole content of this article, I hope that everyone's study has helped.

The above describes the PHP export Excel instance to explain, including the aspects of the content, I hope that 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.