导出excel

public function exportExcel($expTitle,$expTableData,$lis){

$xlsTitle = iconv('utf-8', 'gb2312', $expTitle);//文件名称

$fileName = $_SESSION['account'].date('_YmdHis');//or $xlsTitle 文件名称可根据自己情况设定

vendor("PHPExcel.Classes.PHPExcel"); 或 require_once '../../extend/PHPExcel-1.8/PHPExcel-1.8/Classes/PHPExcel.php';

$objPHPExcel = new \PHPExcel();

// Miscellaneous glyphs, UTF-8

 

//2、设置表头

$objPHPExcel->getActiveSheet()->setCellValue('A1', '序号');

$objPHPExcel->getActiveSheet()->setCellValue('B1', '姓名');

$objPHPExcel->getActiveSheet()->setCellValue('C1', '学校');

$objPHPExcel->getActiveSheet()->setCellValue('D1', '成绩');

$objPHPExcel->getActiveSheet()->setCellValue('E1', '排名');

 

//3、填入数据

$count = count($expTableData);//$driver 为数据库表取出的数据

$count1 = count($lis);//$lis为数据库表取出的数据

for ($i = 2; $i <= $count + 1; $i++) {

$objPHPExcel->getActiveSheet()->setCellValue('A' . $i, $i - 1);

$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, $expTableData[$i - 2]['zname']);

$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, $expTableData[$i - 2]['school']);

}

for ($i = 2; $i <= $count1 + 1; $i++) {

$objPHPExcel->getActiveSheet()->setCellValue('D' . $i, $lis[$i - 2]['score']);

$objPHPExcel->getActiveSheet()->setCellValue('E' . $i, $lis[$i - 2]['rank']);

}

 

 

 

header('pragma:public');

header('Content-type:application/vnd.ms-excel;charset=utf-8;name="'.$xlsTitle.'.xls"');

header("Content-Disposition:attachment;filename=$fileName.xls");//attachment新窗口打印inline本窗口打印

$objWriter = \PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');

 

$objWriter->save('php://output');

exit;

}

 

posted @ 2020-07-07 14:37  Smile☆  阅读(134)  评论(0编辑  收藏  举报