php 엑셀 다운로드 구현 > php

본문 바로가기
사이트 내 전체검색

php

php 엑셀 다운로드 구현

페이지 정보

작성자 서방님 댓글 0건 조회 133회 작성일 13-01-31 14:57

본문

<?

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");

?>

 

댓글목록

등록된 댓글이 없습니다.

Total 612건 30 페이지
게시물 검색

회원로그인

접속자집계

오늘
255
어제
225
최대
1,347
전체
155,014
Latest Crypto Fear & Greed Index

그누보드5
Copyright © 서방님.kr All rights reserved.