Home  >  Article  >  Backend Development  >  phpexcel 导出数据耗时,求教

phpexcel 导出数据耗时,求教

WBOY
WBOYOriginal
2016-06-23 14:02:16917browse

导出7000条数据耗时20秒。求教,我那里弄的不对了。

function  exportExcel($data_array,$array,$filename){			$letters_arr=array(			'1'=>'A','2'=>'B','3'=>'C','4'=>'D','5'=>'E',			'6'=>'F','7'=>'G','8'=>'H','9'=>'I','10'=>'J',			'11'=>'K','12'=>'L','13'=>'M','14'=>'N','15'=>'O',			'16'=>'P','17'=>'Q','18'=>'R','19'=>'S','20'=>'T',			'21'=>'U','22'=>'V','23'=>'W','24'=>'X','25'=>'Y',			'26'=>'Z','27'=>'AA','28'=>'AB','29'=>'AC','30'=>'AD',			'31'=>'AE','32'=>'AF','33'=>'AG','34'=>'AH','35'=>'AI',			'36'=>'AJ','37'=>'AK','38'=>'AL','39'=>'AM','40'=>'AN',			'41'=>'AO','42'=>'AP','43'=>'AQ','44'=>'AR','45'=>'AS',			'46'=>'AT','47'=>'AU','47'=>'AV','49'=>'AW','50'=>'AX',			'51'=>'AY','52'=>'AZ',			);			foreach($data_array as $data){				$a=array();				foreach($array as $k=>$v){									$a[]=$data[$k];				}				$dataArray[]=$a;			}		Vendor('Zend.Classes.PHPExcel');				$objPHPExcel = new PHPExcel();		$count=count($array);		$count_2=count($dataArray);		$count_2=$count_2+3;		//首行设置		$objPHPExcel->setActiveSheetIndex(0)				   ->setCellValue('A1',$filename);		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(15);		$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);		$objPHPExcel->getActiveSheet()->mergeCells('A1:'.$letters_arr[$count].'1');		$objPHPExcel->getActiveSheet()->getStyle('A1:'.$letters_arr[$count].'1')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);		//载入数据		$objPHPExcel->getActiveSheet()->fromArray($array, NULL, 'A3');		$objPHPExcel->getActiveSheet()->fromArray($dataArray, NULL, 'A4');		//设置边框		$sharedStyle1 = new PHPExcel_Style();		$sharedStyle1->applyFromArray(				array('borders'=>array(						'left'=> array('style' => PHPExcel_Style_Border::BORDER_THIN),						'right'=>array('style' => PHPExcel_Style_Border::BORDER_THIN),						'top'=>array('style' => PHPExcel_Style_Border::BORDER_THIN),						'bottom'=>array('style' => PHPExcel_Style_Border::BORDER_THIN)						)						)						);		$objPHPExcel->getActiveSheet()->setSharedStyle($sharedStyle1,'A3:'.$letters_arr[$count].$count_2);		 //设置表头填充颜色 		$objStyleA3 = $objPHPExcel->getActiveSheet()->getStyle('A3:'.$letters_arr[$count].'3');		$objStyleA3 = $objStyleA3->getFill();  		$objStyleA3->setFillType(PHPExcel_Style_Fill::FILL_SOLID);		$objStyleA3->getStartColor()->setARGB('FFFFBB');		header('Content-Type: application/vnd.ms-excel');		header('Content-Disposition: attachment;filename="'.$filename.'"');		header('Cache-Control: max-age=0');		$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel,'Excel5');		$objWriter->save('php://output');		exit;


回复讨论(解决方案)

1、试试不要用getActiveSheet(),改成:
$objSheet = $objPHPExcel->getSheet(0);
$objSheet->……
2、如果还是比较慢,测试一下不要用fromArray(),改用循环单元格来插入数据
希望能有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn