본문 바로가기
카테고리 없음

php csv 메모장에서는 정상 엑셀에서는 한글깨지는 현상

by 향유 2021. 1. 19.

php DB내용을 엑셀로 저장하여 서버에 올리는 작업을 진행했다.

 

php인코딩을 아래와 같이 utf-8로 진행하니 웹 사이트에서는 정상적으로 소스가 표출 되었으나,

엑셀로 실행 할 시 ansi로 자동 실행되어 한글이 깨졌다.

메모장으로 실행할 경우 정상적으로 한글이 표출 되었다.

header('Content-Type:text/css;charset=UTF-8;');

 

버그를 잡기위해 삽질을 하다 도달한 결론은

파일 전체 인코딩을 EUC-KR로 하고 한글 요소들을 utf-8로 인코딩을 하는 것이었다.

 

골자소스는 아래와 같다.

header('Content-Type:text/css;charset=EUC-KR;');
header('Expires: 0');
header('Content-Transfer-Encoding: binary');
header('Cache-Control: private, no-transform, no-store, must-revalidate');
//반복행
foreach($arrData['list'] as $key => $val) {
    $csv_dump .= filterData($val['code']).",";
    $csv_dump .= filterData($val['id']).",";
    $csv_dump .= filterData($val['name'])."";
    $csv_dump .= "\r\n";

}
//쉼표 필터 함수, 한글화 처리
function filterData($string) {
    $string=iconv("UTF-8","EUC-KR",($string));
    return $string;
}

 

전체 소스는 깃 링크를 남긴다.

github.com/kys8976/mysqltocsvuploadtoserver

댓글