phpexcel批量匯出資料,主要參考程式碼段:

<?php
	ob_clean();//ob_clean() - 清空(擦掉)輸出緩衝區
	require_once("PHPExcel/Classes/PHPExcel.php");
	$objExcel = new PHPExcel();
	//設定檔案屬性(否則,中文會出現亂碼)
	$objProperty = $objExcel->getProperties();
	//設定當前工作表
	$objExcel->setActiveSheetIndex(0);
	//獲取當前工作表
	$objActSheet = $objExcel->getActiveSheet();
	//標題欄位置
	$columns = 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"
	);
	
	//標題欄名稱
	$titles = array(
		1 => "姓名", 
		2 => "學號", 
		3 => "性別", 
		4 => "院系", 
		5 => "專業", 
		6 => "年級", 
		7 => "狀態", 
		8 => "工作組", 
		9 => "綜合評分",
		10 => "任務總數",
		11 => "完成數量",
		12 => "完成比例"
	);	
	//寫入標題欄
	for($i=1;$i<=count($titles);$i++){
		$objActSheet->setCellValue($columns[$i] .'1', $titles[$i]);
	}
	$j = 1;
	foreach($result as $k=>$rs){
		$j++;
		$groupuser = array(
			1 => $rs["username"],
			2 => $rs["idnumber"],
			3 => $rs["year"],
			4 => $rs["year"],
			5 => $rs["year"],
			6 => $rs["year"],
			7 => $rs["year"],
			8 => $rs['title'],
			9 => $rs["year"],
			10 => $rs["doccount"],
			11 => $rs["number"],
			12 => $rs["prect"]
		);
		for($i=1;$i<=count($titles);$i++){
			if($i==2 || $i==12){
				$objActSheet->setCellValueExplicit($columns[$i] . $j, $groupuser[$i], PHPExcel_Cell_DataType::TYPE_STRING);
			}else{
				$objActSheet->setCellValue($columns[$i] . $j, $groupuser[$i]);
			}
			//設定字型
			$objStyle = $objActSheet->getStyle($columns[$i] . $j);
			$objFont = $objStyle->getFont();
		}
	}
	$objExcel->setActiveSheetIndex(0);
	$excelname = date("YmdHis");
	header('Content-Type: application/vnd.ms-excel; charset=utf-8');
	header('Content-Disposition: attachment;filename="'. $excelname .'.xls"');
	header('Cache-Control: max-age=0');
	$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
	$objWriter->save('php://output');//下載