/** * 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