메뉴 건너뛰기

프로그램언어

조회 수 29782 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
<?

header( "Content-type: application/vnd.ms-excel" );
header( "Content-type: application/vnd.ms-excel; charset=utf-8");
header( "Content-Disposition: attachment; filename = invoice.xls" );
header( "Content-Description: PHP4 Generated Data" );

$sql = "select * from tblName order by reg_date desc";
$result = mysql_query($sql);

// 테이블 상단 만들기
$EXCEL_STR = "
<table border='1'>
<tr>
   <td>번호</td>
   <td>코드</td>
   <td>내용</td>
</tr>";

while($row = mysql_fetch_array($result)) {
   $EXCEL_STR .= "
   <tr>
       <td>".$row['idx']."</td>
       <td>".$row['code']."</td>
       <td>".$row['contents']."</td>
   </tr>
   ";
}

$EXCEL_STR .= "</table>";

echo "<meta content=\"application/vnd.ms-excel; charset=UTF-8\" name=\"Content-type\"> ";
echo $EXCEL_STR;
?>

 


================================

PHP 엑셀 저장 excel 저장


브라우저화면에 출력되는 내용을 엑셀로 출력해야하는 경우가 많이 있습니다.

DB의 내용을 엑셀파일의 형태로 저장하여 백업을 하거나, 홈페이지 방문자가 화면의 내용을 엑셀형태로 받아가기를 원하는 경우 해당 기능을 구현해야 하는 경우가 있습니다.


엑셀로 저장

엑셀의 형태로 다운받을 수 있는 방법은 상당히 간단합니다.

해당 출력을 담당하는 소스 상단에 아래의 코드를 덧붙여주면 끝입니다.


단, 주의해야할 점이 있다면 header()함수 앞에서는 아무것도 출력을 해서는 안된다는 겁니다.

아래 샘플코드를 프로그램 파일 제일 상단에 두면 큰 문제를 만나시지는 않을 겁니다.


샘플코드


<?

$output_file_name = "다운로드.xls";

header( "Content-type: application/vnd.ms-excel" );

header( "Content-Disposition: attachment; filename={$output_file_name}.xls" );

header( "Content-Description: PHP4 Generated Data");

?>



하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved