Develop
2013.04.23 14:37
[php] 데이터를 엑셀,워드 형태로 변환할 때 헤더(ms-office)
조회 수 7509 댓글 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를 활용해서 필드명을 계속 가져오게 하면 쉽게 처리 됩니다.
번호 | 분류 | 제목 | 글쓴이 | 날짜 | 조회 수 |
---|---|---|---|---|---|
577 | Develop | [c] 프로세스간의 통신(파이프) | hooni | 2003.04.23 | 6908 |
576 | Develop |
[c] pcapdump 파일 분석 하는 프로그램.. ㅋㅋ
![]() |
hooni | 2013.04.23 | 6910 |
575 | Develop |
[c] 맵서치인 듯(옛날 컴에서 찾은 자료)
![]() |
hooni | 2013.04.23 | 6912 |
574 | Develop |
[c] 단기과정[01/10] 과제.. swap(any data, ..)
![]() |
hooni | 2003.04.23 | 6931 |
573 | Develop |
[c] 프로세스 정보 출력하기..
![]() |
hooni | 2003.04.23 | 6933 |
572 | Develop | [c] 소켓의 세가지 동작모드 | hooni | 2003.04.23 | 6936 |
571 | Develop |
[c] 도메인(호스트)으로 IP정보 알아오기.. (nslookup과 비슷)
![]() |
hooni | 2013.04.23 | 6941 |
570 | Develop | [js] php의 number_format() 함수와 같은.. ㅋㅋ | hooni | 2013.04.23 | 6942 |
569 | Develop |
[c] 숫자 맞추는 게임..
![]() |
hooni | 2013.04.23 | 6946 |
568 | Develop |
[php] 빔 프로젝터 예약 프로그램.. ㅋㅋ
![]() |
hooni | 2013.04.23 | 6947 |
567 | Develop | [c] 단기과정[01/06] sizeof, 실수표현, 메모리, 연산자 | hooni | 2003.04.23 | 6950 |
566 | Develop |
[js] 네이버, 다음의 플레이어 스킨
![]() |
hooni | 2013.04.23 | 6960 |