public function excel() { $data = $this->getexaminfo(); // 所教学科id $subject = d('exam')->getteacherinfo($this->authinfo); $list = m('score')->where(array('acp_id' => intval($_get['id'])))->order('sc_rank asc')->select(); $auth = getdatabyarray('auth', $list, 'a_id', 'a_id,a_nickname'); foreach ($list as &$value) { $value['a_nickname'] = $auth[$value['a_id']]['a_nickname']; } import('@.org.phpexcel.phpexcel'); // create new phpexcel object $objphpexcel = new phpexcel(); // set properties $objphpexcel->getproperties()->setcreator(ctos) ->setlastmodifiedby(ctos) ->settitle(office 2007 xlsx test document) ->setsubject(office 2007 xlsx test document) ->setdescription(test document for office 2007 xlsx, generated using php classes.) ->setkeywords(office 2007 openxml php) ->setcategory(test result file); // set width $objphpexcel->getactivesheet()->getcolumndimension('a')->setwidth(20); $objphpexcel->getactivesheet()->getcolumndimension('b')->setwidth(20); $objphpexcel->getactivesheet()->getcolumndimension('c')->setwidth(20); $objphpexcel->getactivesheet()->getcolumndimension('d')->setwidth(20); // 设置行高度 $objphpexcel->getactivesheet()->getrowdimension('1')->setrowheight(22); $objphpexcel->getactivesheet()->getrowdimension('2')->setrowheight(20); // 字体和样式 $objphpexcel->getactivesheet()->getdefaultstyle()->getfont()->setsize(10); $objphpexcel->getactivesheet()->getstyle('a2:d2')->getfont()->setbold(true); $objphpexcel->getactivesheet()->getstyle('a1')->getfont()->setbold(true); $objphpexcel->getactivesheet()->getstyle('a2:d2')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center); $objphpexcel->getactivesheet()->getstyle('a2:d2')->getborders()->getallborders()->setborderstyle(phpexcel_style_border::border_thin); // 设置水平居中 $objphpexcel->getactivesheet()->getstyle('a1')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center); $objphpexcel->getactivesheet()->getstyle('a')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center); $objphpexcel->getactivesheet()->getstyle('b')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center); $objphpexcel->getactivesheet()->getstyle('c')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center); $objphpexcel->getactivesheet()->getstyle('d')->getalignment()->sethorizontal(phpexcel_style_alignment::horizontal_center); // 合并 $objphpexcel->getactivesheet()->mergecells('a1:d1'); // 表头 $objphpexcel->setactivesheetindex(0) ->setcellvalue('a1', $data['examtitle']) ->setcellvalue('a2', '序号') ->setcellvalue('b2', '姓名') ->setcellvalue('c2', '班级') ->setcellvalue('d2', '成绩'); // 内容 for ($i = 0, $len = count($list); $i getactivesheet(0)->setcellvalue('a' . ($i + 3), $list[$i]['sc_rank']); $objphpexcel->getactivesheet(0)->setcellvalue('b' . ($i + 3), $list[$i]['a_nickname']); $objphpexcel->getactivesheet(0)->setcellvalue('c' . ($i + 3), $data['title']); $objphpexcel->getactivesheet(0)->setcellvalue('d' . ($i + 3), $list[$i]['sc_point']); $objphpexcel->getactivesheet()->getstyle('a' . ($i + 3) . ':d' . ($i + 3))->getalignment()->setvertical(phpexcel_style_alignment::vertical_center); $objphpexcel->getactivesheet()->getstyle('a' . ($i + 3) . ':d' . ($i + 3))->getborders()->getallborders()->setborderstyle(phpexcel_style_border::border_thin); $objphpexcel->getactivesheet()->getrowdimension($i + 3)->setrowheight(16); } // rename sheet $objphpexcel->getactivesheet()->settitle($data['examtitle']); // set active sheet index to the first sheet, so excel opens this as the first sheet $objphpexcel->setactivesheetindex(0); // 输出 header('content-type: application/vnd.ms-excel'); header('content-disposition: attachment;filename=' . $data['examtitle'] . '.xls'); header('cache-control: max-age=0'); $objwriter = phpexcel_iofactory::createwriter($objphpexcel, 'excel5'); $objwriter->save('php://output'); }
以上就介绍了phpexcel导出数据到excel,包括了方面的内容,希望对php教程有兴趣的朋友有所帮助。