Programming/PHP

[PHP] 엑셀 출력을 위한 헤더(Header) 선언

DOTI 2022. 10. 24. 16:45
[PHP] 엑셀 출력을 위한 헤더(Header) 선언
반응형
[PHP] 엑셀 출력을 위한 헤더(Header) 선언

 

엑셀 출력을 위해서는 테이블 구조의 HTML 페이지를 구성 후 상단에 헤더 선언을 추가하면 엑셀 파일로 출력이 가능함

 

 

header 선언

$filename = "파일이름.xls";

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Description: PHP4 Generated Data");

 

 

예제

<?php
$filename = "회원목록.xls";

header("Content-type: application/vnd.ms-excel");
header("Content-Disposition: attachment; filename=".$filename);
header("Content-Description: PHP4 Generated Data");
?>

<!DOCTYPE html>
<html lang="ko">
<head>
	<meta charset="UTF-8">
	<meta http-equiv="X-UA-Compatible" content="IE=edge">
	<meta name="viewport" content="width=device-width, initial-scale=1.0">
	<title>Document</title>
</head>
<body>
	<table border="1">
		<tr>
			<th>이름</th>
			<th>성별</th>
			<th>나이</th>
			<th>전화번호</th>
		</tr>
		<tr>
			<td>홍길동</td>
			<td>남</td>
			<td>30</td>
			<td>010-1234-5678</td>
		</tr>
	</table>
</body>
</html>
반응형