Develop
2013.04.23 14:37
[php] 데이터를 엑셀,워드 형태로 변환할 때 헤더(ms-office)
조회 수 7508 댓글 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를 활용해서 필드명을 계속 가져오게 하면 쉽게 처리 됩니다.
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
1017 | System/OS | [linux] iptables 초간단 세팅 스크립트 | hooni | 2017.09.26 | 1386 |
1016 | Develop | [ios] 동영상 플레이어 샘플 (for PIP Player) | hooni | 2017.03.15 | 1387 |
1015 | System/OS | [linux] yum 업데이트 시 커널 제외하기 | hooni | 2014.09.11 | 1396 |
1014 | Develop | [android] How can I place app icon on launcher home screen? | hooni | 2016.11.15 | 1399 |
1013 | Develop | [ios] GPS 이용 상태 확인 | hooni | 2015.04.27 | 1405 |
1012 | Develop | [c] Mac OS 에 gmp(gmp.h) 라이브러리 설치 | hooni | 2016.10.03 | 1428 |
1011 | PPT | [ppt] 전자금융보안론 발표/설치 자료 | hooni | 2016.05.03 | 1430 |
1010 | Develop | [js] URL 파싱하기 (jQuery 안쓰고) | hooni | 2017.12.14 | 1431 |
1009 | Develop | macOS에 node, npm 설치하기 (homebrew) | hooni | 2021.11.06 | 1436 |
1008 | Etc | ISMS 인증기준 – 정보보호대책 (시스템개발보안) | hooni | 2016.12.01 | 1439 |
1007 | Develop | [php] 한글 문자열 자르기 (utf-8) | hooni | 2015.11.10 | 1440 |
1006 | System/OS | [mac] Mac OS 패키지 매니저, HomeBrew | hooni | 2015.01.03 | 1444 |