orange056

[PHP] 엑셀다운로드 기능 만들기 본문

카테고리 없음

[PHP] 엑셀다운로드 기능 만들기

오렝지방구 2021. 8. 13. 11:24
728x90
반응형
<div >
	<button id="exel_down">엑셀 다운로드</button>
</div>
<script>
$('#exel_down').click(function(){
	location.href = "url";
})
</script>
<?php

$file = $_GET['file'];
$mysqli = new mysqli('127.0.0.1', 'root', '비번', '테이블명');

header( "Content-type: application/vnd.ms-excel; charset=utf-8");
header( "Content-Disposition: attachment; filename = excel_test.xls" );     //filename = 저장되는 파일명을 설정합니다.
header( "Content-Description: PHP4 Generated Data" );

//엑셀 파일로 만들고자 하는 데이터의 테이블을 만듭니다.
$EXCEL_FILE = "
<table border='1'>
    <tr>
       <td>이름</td>
       <td>성별</td>
       <td>나이</td>
       <td>전화번호</td>
       <td>사진여부</td>
    </tr>
";

$qry = "SELECT * FROM '테이블명'";


$res = $mysqli->query($qry);

// DB 에 저장된 데이터를 테이블 형태로 저장합니다.
while ($row = $res->fetch_object()) {
$EXCEL_FILE .= "
    <tr>
       <td>".$row->name."</td>
       <td>".$row->gender."</td>
       <td>".$row->age."</td>
       <td>".$row->phone."</td>
       <td>".$row->file."</td>
    </tr>
";
}

$EXCEL_FILE .= "</table>";

// 만든 테이블을 출력해줘야 만들어진 엑셀파일에 데이터가 나타납니다.
echo "<meta http-equiv='Content-Type' content='text/html; charset=utf-8'>";
echo $EXCEL_FILE;
?>
728x90
반응형
Comments