게시물 엑셀 다운로드시 한글 깨짐
본문
안녕하세요 게시물을 엑셀로 다운받으려고 하는데
테이블로 만드니까 th부분이 한글이 깨져요..ㅜ
charset을 utf-8, euc-kr 로 해도 깨져요 ㅠㅠ
어떻게 해야할까요..
<?php
include_once "_common.php";
if (!$is_admin) {
alert("관리자만 접근 가능합니다.");
exit;
}
$excel_down = $g5['write_customer'] . $_GET['bo_table']; //엑셀 다운로드 테이블
$wr_id = $_GET['wr_id'];
$hp_filename = "문의현황";
@sql_query("SET CHARACTER SET utf-8"); // 한글깨지면 주석해지
if ($ms =="excel"){
$g5['title'] = "엑셀 문서 다운로드";
header( "Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" );
header( "Content-Disposition: attachment; filename={$hp_filename}.xlsx" );
//header( "Content-Description: PHP4 Generated Data" );
} else if ($ms =="power"){
$g5['title'] = "파워포인트 문서 다운로드";
header( "Content-type: application/vnd.ms-powerpoint" );
header( "Content-Disposition: attachment; filename={$hp_filename}.ppt" );
// header( "Content-Description: PHP4 Generated Data" );
} else if ($ms =="word"){
$g5['title'] = "워드 문서 다운로드";
header( "Content-type: application/vnd.ms-word" );
header( "Content-Disposition: attachment; filename={$hp_filename}.doc" );
//header( "Content-Description: PHP4 Generated Data" );
} else if ($ms =="memo"){
$g5['title'] = "메모 문서 다운로드";
header( "Content-type: application/vnd.ms-notepad" );
header( "Content-Disposition: attachment; filename={$hp_filename}.txt" );
} else {
header( "Content-type: application/vnd.ms-excel" );
header( "Content-Disposition: attachment; filename={$hp_filename}.xls" );
}
header( "Content-Description: PHP4 Generated Data" );
print("<meta http-equiv=\"Content-Type\" content=\"application/vnd.ms-excel; charset=utf-8\">");
// 원글 + 코멘트 다운로드
$temp=sql_fetch_array(sql_query("select count(*) from {$excel_down} "));
$result=sql_query("select * from {$excel_down} order by wr_datetime desc");
// 원글만 다운로드 (코멘트 제외) , 2013-10-21 추가
//$temp=sql_fetch_array(mysql_query("select count(*) from {$excel_down} where wr_is_comment = '0' and wr_content = '{$wr_id}' "));
//$result=sql_query("select * from {$excel_down} where wr_is_comment = '0' and wr_content = '{$wr_id}' order by wr_datetime desc");
$number=$temp[0];
?>
<html>
<head>
<!-- <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> -->
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<style type="text/css">
.txt {
mso-style-parent:style0;
mso-number-format:"\@";
border:.5pt solid windowtext;
}
</style>
</head>
<body>
<table>
<tr>
<th class='txt'>고객명</th>
<th class='txt'>전화번호</th>
<th class='txt'>건물유형</th>
<th class='txt'>입주일</th>
<th class='txt'>요청사항</th>
<!-- <td>필드1</td>
<td>필드2</td>
<td>필드3</td>
<td>필드4</td>
<td>필드5</td>
<td>필드6</td>
<td>필드7</td>
<td>필드8</td>
<td>필드9</td>
<td>필드10</td>
</tr> -->
<?php
while($data=sql_fetch_array($result)) {
echo "
<tr>
<td class='txt'>{$data['wr_subject']}</td>
<td class='txt'>{$data['wr_1']}</td>
<td class='txt'>{$data['wr_2']}</td>
<td class='txt'>{$data['wr_3']}</td>
<td class='txt'>{$data['wr_4']}</td>
</tr>
";
$number--;
}
?>
</table>
</body>
</html>
답변 6
우선 sql fatch에서 select하실때 문자셋이 어떻게 나오는지 부터 확인을 해보신후에..
해당 문자셋으로 변환해서 출력해주셔야 합니다.
대부분이 utf-8, euc-kr 의 문자셋을 사용하고 있으니..경우에 조금씩 다를수도 있습니다.
환경에 따라 latin계열의 문자셋으로도 사용되는 경우도 있습니다.
해당 파일도 utf8로 셋팅 되어 있는지 체크해보세요.
그리고 <meta charset="utf-8"> 이렇게만 해주는게 표준인데...
if ($ms =="excel"){
$g5['title'] = "엑셀 문서 다운로드";
header( "Content-type: application/vnd.openxmlformats-officedocument.spreadsheetml.sheet" );
header( "Content-Disposition: attachment; filename={$hp_filename}.xlsx" );
이부분을
if ($ms =="excel"){ $g5['title'] = "엑셀 문서 다운로드";
header('Content-Type: application/vnd.ms-excel; charset=utf-8');
header( "Content-Disposition: attachment; filename={$hp_filename}.xlsx" );
이렇게 해보세요.
!--><style type="text/css">
.txt {
mso-style-parent:style0;
mso-number-format:"\@";
border:.5pt solid windowtext;
font-family: 'Malgun Gothic', '맑은 고딕', 'Arial', sans-serif; /* 한글 폰트 지정 */
}
</style>
스타일 타입 해보시구요.
만약 않되면 헤더파일 열어서 이렇게 해보세요
header("Content-Type: application/vnd.ms-excel; charset=utf-8");
class = txt 부분이기본 윈도우 텍스트라서 한글이 지원안하는경우 그래서 윗분들말씀처럼
출력과 문자열 셋을 바꾸는것도 도움이 될것 같아요
th부분이 한글이 깨져요....
==
HTML (PHP) 파일 자체 chatset을 확인해 보세요.