Phpexcel data export to an Excel file

Source: Internet
Author: User
Tags ord

/** * Data export to excel file * * @param object $data The contents of the data to be exported * @param object $HEADARR Set the header name of Excel * @param stri Ng $fileName the exported file name * @param string $width Set the width of the cell * * @throws phpexcel_exception Exception control * @throws PHPEXCE  L_reader_exception controls Excel Load Exceptions * @throws Phpexcel_writer_exception controls Excel write-in Exceptions * @return BOOL */Public function Outputexcel ($data, $HEADARR, $fileName, $width = ' 30 ') {//If no data returned false if (empty ($data) | |!i        S_array ($data)) {return false;        }//Set today's date to name the Excel file $date = Date ("Y_m_d", Time ());        $fileName. = "_{$date}.xlsx";        Instantiate the Phpexcel object $objPHPExcel = new Phpexcel ();        Set table Header $index = Ord ("A");        $objActSheet = $objPHPExcel->getactivesheet ();            Generate header foreach ($headArr as $value) {$columnHeader = Chr ($index); $objPHPExcel->setactivesheetindex (0)->setcellvalue ($columnHeader.          ' 1 ', $value);  $objActSheet->getcolumndimension ($columnHeader)->setwidth ($width);        $index + = 1;        } $columnData = 2;            foreach ($data as $key = + $rows) {$span = Ord ("A");                foreach ($rows as $keyName = + $value) {//column write $j = Chr ($span);                $objActSheet->setcellvalue ($j. $columnData, $value);            $span + +;        } $columnData + +;        }//Encoding conversion $fileName = Iconv ("Utf-8", "GBK", $fileName);        Set the activity Order index to the first table, so Excel opens this is the first table $objPHPExcel->setactivesheetindex (0); REDIRECT output to a client Web 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 through the browserLoad return true; }

  

Phpexcel data export to an Excel file

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.