Develop
2013.04.23 14:37
[php] 데이터를 엑셀,워드 형태로 변환할 때 헤더(ms-office)
Views 7488 Comment 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를 활용해서 필드명을 계속 가져오게 하면 쉽게 처리 됩니다.
No. | Category | Subject | Author | Date | Views |
---|---|---|---|---|---|
765 | Develop | [c] 프로그래밍의 전반적인 설명 ppt | hooni | 2003.04.23 | 7667 |
764 | Develop | [c] 프로그램 코드(c/c++)를 html 파일로 변환 | hooni | 2013.04.23 | 7677 |
763 | Develop | [c] 프로세스 검사하기 | hooni | 2013.04.23 | 8027 |
762 | Develop | [c] 프로세스 정보 출력하기.. | hooni | 2003.04.23 | 6906 |
761 | Develop | [c] 프로세스간의 통신(파이프) | hooni | 2003.04.23 | 6891 |
760 | Develop | [c] 플러드 필링 (flood filling) 알고리즘.. | hooni | 2013.04.23 | 7319 |
759 | Develop | [c] 하노이탑 - 재귀함수 | hooni | 2003.04.23 | 9153 |
758 | Develop | [c] 학교 건물 최단거리 찾는 웹 연동 프로그램 | hooni | 2013.04.23 | 6800 |
757 | Develop | [c] 학생명단 관리 프로그램 소스 ㅋㅋ 1 | hooni | 2003.04.23 | 7761 |
756 | Develop | [c] 한글 문자열 출력 | hooni | 2015.11.10 | 1675 |
755 | Develop | [c] 함수 목록과 예제 소스 파일 | hooni | 2013.04.23 | 7191 |
754 | Develop | [c] 함수 요약 (검색해서 쓰세요..) | hooni | 2003.04.23 | 8795 |