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(),改用循环单元格来插入数据
希望能有所帮助。
