Develop
2013.04.23 14:37
[php] 데이터를 엑셀,워드 형태로 변환할 때 헤더(ms-office)
조회 수 7488 댓글 0
우선 excel이나 ms-word로 변환할려면 아래의 헤더부분이 필요합니다.
<?
// 엑셀의 경우
header( "Content-type: application/vnd.ms-excel" );
header( "Content-Disposition: attachment; filename=파일명.xls" );
header( "Content-Description: PHP4 Generated Data" );
// 워드일 경우
header( "Content-type: application/vnd.ms-word" );
header( "Content-Disposition: attachment; filename=파일명.doc" );
header( "Content-Description: PHP4 Generated Data" );
?>
그 다음엔 엑셀에서 자기가 원하는 표를 구성합니다.
그리고 구성한 엑셀파일을 웹페이지로 저장합니다. (엑셀에서 웹페이지로 저장이 가능합니다.) <- 이 부분이 핵심이라고 할 수 있겠네요.
그런다음 메모장이나 기타 에디터로 여시고 상단에 위의 헤더를 추가합니다.
그리고 해당 셀부분에 DB에서 해당하는 필드값을 넣어줍니다.
워드도 같은 방식으로 구성하면 됩니다.
엑셀 변환시 샘플코드
<?
include "./DBConn.php";
header( "Content-type: application/vnd.ms-excel" );
header( "Content-Disposition: attachment; filename=업체관리.xls" );
header( "Content-Description: PHP4 Generated Data" );
$table_data = mysql_query("SELECT * FROM company order by BC_REGDATE desc");
?>
<?
while( $data = mysql_fetch_array( $table_data ) ) :
$bc_no=$data[BC_NO];
$bc_company=stripslashes($data[BC_COMPANY]);
$bc_name=stripslashes($data[BC_NAME]);
$bc_comno=$data[BC_COMNO];
$bc_address1=stripslashes($data[BC_ADDRESS1]);
$bc_address2=stripslashes($data[BC_ADDRESS2]);
?>
<html>
<table>
<tr>
<td> <?echo "$bc_company"; ?> </td>
</tr>
<?$i++; endwhile;?>
</table>
</html>
또 다른 샘플.. ㅋㅋ
<html>
...
<table>
<?php
$table_data = mysql_query("SELECT * FROM company order by BC_REGDATE desc");
while( $data = mysql_fetch_array( $table_data ) ) :
{
echo "<tr>";
foreach($data as $field)
{
echo "<td>";
echo $field;
echo "</td>";
}
echo "</tr>";
}
?>
</table>
</html>
foreach를 활용해서 필드명을 계속 가져오게 하면 쉽게 처리 됩니다.
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
957 | System/OS | [mac] 맥에서 기본 실행 앱 변경하기 | hooni | 2018.03.02 | 1808 |
956 | Database | SQL JOIN 정리 (Inner Join & Outer Join) | hooni | 2019.11.22 | 1836 |
955 | Develop | [ios] Objective-C 에서 자주 사용하는 수학 함수와 유용한 Define | hooni | 2014.08.08 | 1860 |
954 | Develop | [ios] 카메라 사용 권한 확인해서 분기하는 방법 | hooni | 2015.02.26 | 1869 |
953 | Develop | [php][laravel] 초간단 MacOS에서 Laravel 개발 환경 구축 | hooni | 2017.12.15 | 1873 |
952 | Develop | [android] 가속도 센서를 이용한 흔듦(Shake) 감지 | hooni | 2014.11.04 | 1898 |
951 | Develop | [ios] 웹뷰 history.back() ㅋㄷ | hooni | 2016.06.27 | 1901 |
950 | System/OS | 맥 OS X 에서 스크린 화면 캡쳐 단축키 (Mac Print Screen) | hooni | 2015.07.21 | 1902 |
949 | System/OS | iptime 공유기 해킹 기술문서 4 | hooni | 2015.01.01 | 1941 |
948 | Etc | 영어. 반드시 외워야 할 단어 | hooni | 2016.07.07 | 1968 |
947 | Develop | [matlab] ZigZag-Scanning (2-D Array) | hooni | 2016.10.15 | 1987 |
946 | Develop | [ios] 카테고리 확장 메소드를 찾지 못하는 경우 | hooni | 2014.08.08 | 1995 |