1. 程式人生 > 其它 >PHP生成excel檔案(包含格式等基本內容)

PHP生成excel檔案(包含格式等基本內容)

需先引入PHP檔案
include_once(ROOT_PATH . '/Public/excel/PHPExcel.php');
$objPHPExcel = new PHPExcel();
$objPHPExcel->getProperties()->setCreator($msprojectinfo['projectname'].'_資料匯入模板')
->setLastModifiedBy("資料匯入模板")
->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("資料匯入模板");
$worksheet = $objPHPExcel->getActiveSheet();
// 預設格式
$objPHPExcel->getActiveSheet()->getDefaultRowDimension()->setRowHeight(24);//預設行高
$objPHPExcel->getActiveSheet()->getDefaultColumnDimension()->setWidth(12);//預設寬度
$objPHPExcel->getDefaultStyle()->getFont()->setSize(10.5);//預設字型大小

$objPHPExcel->getDefaultStyle()->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_CENTER);//水平居中
$objPHPExcel->getDefaultStyle()->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);//垂直居中

$objPHPExcel->getActiveSheet()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);//第一行加粗
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setSize(14);//第一行字型大小
$objPHPExcel->getActiveSheet()->getStyle('A1')->getFont()->setBold(true);//第一行字型加粗

$objPHPExcel->getActiveSheet()->getRowDimension('1')->setRowHeight(32); //第一行高度
$objPHPExcel->getActiveSheet()->getRowDimension('2')->setRowHeight(28); //第二行高度
$objPHPExcel->getActiveSheet()->getRowDimension('3')->setRowHeight(28); //第三行高度
$objPHPExcel->getActiveSheet()->getColumnDimension('A')->setWidth(9);//第一列寬度

$colArr = array("A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z","AA","AB","AC");

//第一行合併
$objPHPExcel->getActiveSheet()->mergeCells('A1:'.$colArr[$lienum-1].'1');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A1', $msprojectinfo['projectname'].'_資料匯入模板');

$objPHPExcel->getActiveSheet()->mergeCells('A2:A3');
$objPHPExcel->getActiveSheet()->mergeCells('B2:B3');
$objPHPExcel->getActiveSheet()->mergeCells('C2:C3');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A2', '姓名');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B2', '考場');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C2', '面試');
$lie2 = count($normandcnorm)+2;//中間的合併
$objPHPExcel->getActiveSheet()->mergeCells('D2:'.$colArr[$lie2].'2');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('D2', '分數');

$lie = 2;
if (is_array($normandcnorm)){
foreach ($normandcnorm as $nk=>$nv){
$lie++;
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colArr[$lie].'3', $nv);
}
}

$lie++;
$objPHPExcel->getActiveSheet()->mergeCells($colArr[$lie].'2:'.$colArr[$lie].'3');
$objPHPExcel->setActiveSheetIndex(0)->setCellValue($colArr[$lie].'2', '評語');

$objPHPExcel->getActiveSheet()->getStyle('A2:'.$colArr[$lie].'3')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle('A2:'.$colArr[$lie].'3')->getFill()->getStartColor()->setARGB('FFe6fffb');
$objPHPExcel->getActiveSheet()->getStyle('A2:'.$colArr[$lie].'3')->getFont()->setBold(true);//加粗
$objPHPExcel->getActiveSheet()->getStyle('A2:'.$colArr[$lie].'3')->getFont()->setSize(10.5);

$num=3;
if (is_array($mspersonlist)){
foreach ($mspersonlist as $k2=>$v2){
if (is_array($mwidandmsg[$v2['mwid']])){
foreach ($mwidandmsg[$v2['mwid']] as $k3=>$v3){
$num++;
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('A'.$num, $v2['name']);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('B'.$num, $msworkandname[$v2['mwid']]);
$objPHPExcel->setActiveSheetIndex(0)->setCellValue('C'.$num, $interviewerlist[$v3]);
}
}
}
}

$objPHPExcel->getActiveSheet()->getStyle('A4:'.$colArr[$lienum-1].$num)->getFont()->setSize(9);

$style_array = array(
'borders' => array(
'allborders' => array( //設定全部邊框
'style' => \PHPExcel_Style_Border::BORDER_THIN //粗的是thick
),

),
);
$objPHPExcel->getActiveSheet()->getStyle('A1:'.$colArr[$lienum-1].$num)->getAlignment()->setWrapText(true);
$objPHPExcel->getActiveSheet()->getStyle('A1:'.$colArr[$lienum-1].$num)->applyFromArray($style_array);

ob_end_clean();
ob_start();

$objPHPExcel->getActiveSheet()->setTitle($msprojectinfo['projectname'].'_資料匯入模板');
$objPHPExcel->setActiveSheetIndex(0);

$objWriter = PHPExcel_IOFactory::createWriter($objPHPExcel, 'Excel5');
header('Content-Type: application/vnd.ms-excel');
header('Content-Disposition: attachment;filename="'.$msprojectinfo['projectname'].'_資料匯入模板.xls"');
header('Cache-Control: max-age=0');
$objWriter->save('php://output');