PHP Excel file export phpexcel Extension Library

Source: Internet
Author: User
Tags php excel

PHP Excel File Export

Phpexcel official website http://phpexcel.codeplex.com/


/** * Export Specific files * Depending on the details */public Function download () {//1. Gets the corresponding two-dimensional array from the database $alist = Array (...);        $list = $alist; $data = Array ();//2.        Set the table header name of xls $HEADARR = Array ("Rank", "name", "Phone", "award", "Participation Time");            if (false = = = Empty ($list)) {$i = 0; foreach ($list as $key = + $val) {//assemble the corresponding cell a. B. C,d.

$data [$i] = Array (($i + 1),//a $val [' name '],//b $val [' Tel '],//c $val [' Award '],//d ... ); $i + +; }}else{$data [0] = array (' No related record. '); } $fileName = "Test-excel_"; $this->explodexls ($data, $HEADARR, $fileName); The/** * Tool function handles the XLS file export * @param $data Array () corresponding tbody data * @param $headArr Array () corresponding THEAD data * @param $fileNam E string Specifies the name of the export file * @return xls format file self-export */Public function Explodexls ($data, $HEADARR, $fileName) {//Note this is replaced by the corresponding root text Folder to find the absolute path $dir = __dir__. ' /.. /.. /.. /.. /public '; Require_once $dir. '/excel/phpexcel.php '; Require_once $dir. '/excel/phpexcel/writer/excel2007.php '; Require_once $dir. '/excel/phpexcel/writer/excel5.php '; Include_once $dir. '/excel/phpexcel/iofactory.php '; if (Empty ($data) | | |!is_aRray ($data)) {die ("data must is a array"); } if (empty ($fileName)) {exit; } $date = Date ("Y_m_d", Time ()); $fileName. = "_{$date}.xls"; Create a new Phpexcel object $objPHPExcel = new Phpexcel (); $objProps = $objPHPExcel->getproperties (); Set table Header $key = Ord ("A"); foreach ($headArr as $v) {$colum = Chr ($key); $objPHPExcel->setactivesheetindex (0)->setcellvalue ($colum. ' 1 ', $v); $key + = 1; } $column = 2; $objActSheet = $objPHPExcel->getactivesheet (); foreach ($data as $key = + $rows) {//Line write $span = Ord ("A"); foreach ($rows as $keyName = + $value) {//column write $j = Chr ($span); $objActSheet->setcellvalue ($j. $column, $value); $span + +; } $column + +; }//$fileName = Iconv ("Utf-8", "gb2312", $fileName); Rename Table $objPHPExcel->getaCtivesheet ()->settitle (' simple '); Set the activity Order index to the first table, so Excel opens this is the first table $objPHPExcel->setactivesheetindex (0); REDIRECT output to a Clientweb browser (Excel2007) header (' content-type:application/ Vnd.openxmlformats-officedocument.spreadsheetml.sheet '); Header ("content-disposition:attachment; Filename=\ "$fileName \" "); Header (' cache-control:max-age=0 '); $objWriter = Phpexcel_iofactory::createwriter ($objPHPExcel, ' Excel2007 '); $objWriter->save (' php://output '); File download exit via browser; }



PHP Excel file export phpexcel Extension 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.