PHP


게시글 'DB 자료를 엑셀파일로 저장'에 대한 정보
DB 자료를 엑셀파일로 저장
등록일 2010-05-15 조회 4,371

<?
$connect_host = "localhost";
$connect_id = "디비아이디";
$connect_pass = "디비비밀번호";
$connect_db = "디비명";
$connect_table = "table_name"; // 대상 테이블명
$xls_filename = "filename.xls";

$connect=@mysql_connect($connect_host,$connect_id,$connect_pass);
$mysql=@mysql_select_db($connect_db,$connect);

header( "Content-type: application/vnd.ms-excel" );
header( "Content-Disposition: attachment; filename=memberDB.xls" );
?>
<table style="border-width:1; border-color:green; border-style:dotted;">
<tr>
<?
$fields = mysql_list_fields("$connect_db", "$connect_table");
$columns = mysql_num_fields($fields);

for ($i = 0; $i < $columns; $i++) {
$field[$i]=mysql_field_name($fields, $i);
echo "<th>".$field[$i]. "</th>";
}
?>
</tr>

<?
$result=mysql_query("select * from $connect_table");
while($data=mysql_fetch_assoc($result)){
echo"<tr>";

for ($i = 0; $i < sizeof($field); $i++) {
echo "<td style='border-width:1; border-color:green; border-style:dotted;'>".$data["$field[$i]"]."</td>";
}

echo"</tr>";
}
?>
</table>

<?
##한글이 깨지는 경우 아래부분 추가..

header( "Content-type: application/vnd.ms-excel;" );
header( "Content-Disposition: attachment; filename=data.xls" );
?>

<meta http-equiv="Content-Type" content="text/html; charset=euc-kr">