다음은 여러 사용 방법을 요약한 것입니다
include 'PHPExcel.php';
include 'PHPExcel/Writer/Excel2007.php';
//or include 'PHPExcel/Writer/Excel5.php' ;
.xls 출력용 Excel 만들기
$objPHPExcel = new PHPExcel();
Excel 저장—2007 형식
$objWriter = new PHPExcel_Writer_Excel2007($objPHPExcel);
/ /or $ objWriter = new PHPExcel_Writer_Excel5($objPHPExcel); 2007이 아닌 형식
$objWriter->save("xxx.xlsx");
브라우저에 직접 출력
$objWriter = new PHPExcel_Writer_Excel5($ objPHPExcel) ;
header("Pragma: public");
header("Expires: 0");
header("Cache-Control:must-revalidate, post-check=0, pre-check =0 ″);
header("Content-Type:application/force-download");
header("Content-Type:application/vnd.ms-execl");
header("Content -Type :application/octet-stream");
header("Content-Type:application/download");;
header('Content-Disposition:attachment;filename="resume.xls"');
header("Content-Transfer-Encoding:binary");
$objWriter->save('php://output');
———————————— —— —————————————————————————
Excel 속성 설정:
작성자:
$objPHPExcel->getProperties( )- >setCreator("Maarten Balliauw");
마지막 수정자
$objPHPExcel->getProperties()->setLastModifiedBy("Maarten Balliauw");
제목
$objPHPExcel- > getProperties()->setTitle("Office 2007 XLSX 테스트 문서");
제목
$objPHPExcel->getProperties()->setSubject("Office 2007 XLSX 테스트 문서");
설명
$objPHPExcel->getProperties()->setDescription("PHP 클래스를 사용하여 생성된 Office 2007 XLSX용 테스트 문서.");
키워드
$objPHPExcel->getProperties() -> setKeywords("office 2007 openxml php");
카테고리
$objPHPExcel->getProperties()->setCategory("테스트 결과 파일");
——————— ——— ——————————————————————————————
현재 시트 설정
$objPHPExcel->setActiveSheetIndex(0 ) ;
시트 이름 설정
$objPHPExcel->getActiveSheet()->setTitle('Simple');
셀 값 설정
$objPHPExcel->getActiveSheet( )- >setCellValue('A1', 'String');
$objPHPExcel->getActiveSheet()->setCellValue('A2', 12);
$objPHPExcel->getActiveSheet()- > setCellValue('A3', true);
$objPHPExcel->getActiveSheet()->setCellValue('C5', '=SUM(C2:C4)');
$objPHPExcel-> getActiveSheet( )->setCellValue('B8', '=MIN(B2:C5)');
셀 병합
$objPHPExcel->getActiveSheet()->mergeCells('A18:E22') ;
셀 분리
$objPHPExcel->getActiveSheet()->unmergeCells('A28:B28');
保护셀
$objPHPExcel->getActiveSheet()->getProtection()->setSheet(true);
// 워크시트 보호를 활성화하려면 true로 설정해야 합니다!
$objPHPExcel->getActiveSheet()->protectCells('A3:E13', 'PHPExcel');
设置格式
// 셀 번호 형식 설정
echo date('H:i:s') . " 셀 번호 형식 설정n";
$objPHPExcel->getActiveSheet()->getStyle('E4')->getNumberFormat()->setFormatCode(PHPExcel_Style_NumberFormat::FORMAT_CURRENCY_EUR_SIMPLE);
$objPHPExcel->getActiveSheet()->duplicateStyle( $objPHPExcel->getActiveSheet()->getStyle('E4'), 'E5:E13' );
设置宽width
// 열 너비 설정
$objPHPExcel->getActiveSheet() ->getColumnDimension('B')->setAutoSize(true);
$objPHPExcel->getActiveSheet()->getColumnDimension('D')->setWidth(12);
设置font
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setName('Candara');
$objPHPExcel->getActiveSheet()-> getStyle('B1')->getFont()->setSize(20);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setBold (사실);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->setUnderline(PHPExcel_Style_Font::UNDERLINE_SINGLE);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
$objPHPExcel->getActiveSheet()->getStyle('E1')->getFont()->getColor()->setARGB(PHPExcel_Style_Color::COLOR_WHITE);
$objPHPExcel->getActiveSheet( )->getStyle('D13')->getFont()->setBold(true);
$objPHPExcel->getActiveSheet()->getStyle('E13')->getFont() ->setBold(true);
设置align
$objPHPExcel->getActiveSheet()->getStyle('D11')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('D12')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('D13')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_RIGHT);
$objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setHorizontal(PHPExcel_Style_Alignment::HORIZONTAL_JUSTIFY);
//垂直居中
$objPHPExcel->getActiveSheet()->getStyle('A18')->getAlignment()->setVertical(PHPExcel_Style_Alignment::VERTICAL_CENTER);
열의 테두리
$objPHPExcel->getActiveSheet()->getStyle('A4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle('B4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle('C4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle('D4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
$objPHPExcel->getActiveSheet()->getStyle('E4')->getBorders()->getTop()->setBorderStyle(PHPExcel_Style_Border::BORDER_THIN);
테두리 색상
$objPHPExcel->getActiveSheet()->getStyle('D13')->getBorders()->getLeft()->getColor()->setARGB('FF993300');
$objPHPExcel ->getActiveSheet()->getStyle('D13')->getBorders()->getTop()->getColor()->setARGB('FF993300');
$objPHPExcel-> ;getActiveSheet()->getStyle('D13')->getBorders()->getBottom()->getColor()->setARGB('FF993300');
$objPHPExcel->getActiveSheet ()->getStyle('E13')->getBorders()->getTop()->getColor()->setARGB('FF993300');
$objPHPExcel->getActiveSheet() ->getStyle('E13')->getBorders()->getBottom()->getColor()->setARGB('FF993300');
$objPHPExcel->getActiveSheet()-> ;getStyle('E13')->getBorders()->getRight()->getColor()->setARGB('FF993300');
设置填充颜color
$objPHPExcel->getActiveSheet ()->getStyle('A1')->getFill()->setFillType(PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle('A1')-> ;getFill()->getStartColor()->setARGB('FF808080');
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->setFillType (PHPExcel_Style_Fill::FILL_SOLID);
$objPHPExcel->getActiveSheet()->getStyle('B1')->getFill()->getStartColor()->setARGB('FF808080');
그림 추가
$obj드로잉 = new PHPExcel_Worksheet_드로잉();
$obj드로잉->setName('로고');
$obj드로잉->setDescription('로고');
$obj드로잉->setPath('./images/ officelogo.jpg');
$obj드로잉->setHeight(36);
$obj드로잉->setWorksheet($objPHPExcel->getActiveSheet());
$obj드로잉 = new PHPExcel_Worksheet_드로잉();
$obj드로잉->setName('유료');
$obj드로잉->setDescription('유료');
$obj드로잉->setPath('./images/paid.png') ;
$obj드로잉->setCoordinates('B15');
$obj드로잉->setOffsetX(110);
$obj드로잉->setRotation(25);
$obj드로잉-> getShadow()->setVisible(true);
$obj드로잉->getShadow()->setDirection(45);
$obj드로잉->setWorksheet($objPHPExcel->getActiveSheet());
//중국어 출력 문제 처리
일반 출력을 위해서는 문자열을 UTF-8 인코딩으로 변환해야 합니다. 그렇지 않으면 중국어 문자가 공백으로 출력됩니다.
$str = iconv(' gb2312', 'utf-8', $str);
또는 중국어 문자열을 처리하기 위해 특별히 함수를 작성할 수 있습니다:
function ConvertUTF8($str)
{
if(empty($str )) return '';
return iconv('gb2312', 'utf-8', $str);
}
//데이터베이스에서 데이터 처리 방법 출력
다음과 같은 데이터베이스에서 데이터 읽기 :
$db = new Mysql($dbconfig);
$sql = "SELECT * FROM 테이블 이름";
$row = $db->GetAll($sql); 2차원 배열
$count = count($row);
for ($i = 2; $i <= $count+1; $i++) {
$objPHPExcel->getActiveSheet ()-> setCellValue('A' . $i, ConvertUTF8($row[$i-2][1]));
$objPHPExcel->getActiveSheet()->setCellValue('B' . $i, ConvertUTF8( $row[$i-2][2]));
$objPHPExcel->getActiveSheet()->setCellValue('C' . $i, ConvertUTF8($row[$i- 2][3] ));
$objPHPExcel->getActiveSheet()->setCellValue('D' . $i, ConvertUTF8($row[$i-2][4]));
$objPHPExcel-> getActiveSheet()->setCellValue('E' . $i, ConvertUTF8(date("Y-m-d", $row[$i-2][5])));
$objPHPExcel-> ;getActiveSheet()- >setCellValue('F' . $i, ConvertUTF8($row[$i-2][6]));
$objPHPExcel->getActiveSheet()->setCellValue('G ' . $i, ConvertUTF8($row[$i-2][7]));
$objPHPExcel->getActiveSheet()->setCellValue('H' . $i, ConvertUTF8($row[$ i-2][ 8]));
}
기본 시트 이후에 워크시트를 생성합니다
echo date('H:i:s') " 새 워크시트 개체 생성";
$objPHPExcel ->createSheet();
$objWriter = PHPExcel_IOFactory::createWriter($objExcel, 'Excel5');
$objWriter-save('php://output');