PHP html 엑셀 다운로드 - PHP html egsel daunlodeu

php에서 Table을 엑셀(excel)파일로 다운하기/한글 깨짐

PHP html 엑셀 다운로드 - PHP html egsel daunlodeu


php에서 db자료를 뽑은 다음에 엑셀로 다운할때 자주 사용하는 기능입니다.

먼저 php에서 db자료를 훑어서 필요한 자료를 뽑아내서 view로 보냅니다
(예시 $excelData)

<? header( "Content-type: application/vnd.ms-excel" ); header( "Content-type: application/vnd.ms-excel; charset=utf-8"); header( "Content-Disposition: attachment; filename = test.xls" ); header( "Content-Description: PHP4 Generated Data" ); ?> <table border="1"> <thead> <tr> <th>A</th> <th>B</th> </tr> </thead> <tbody> <?foreach($excelData as $row){?> <tr> <td style="mso-number-format:'\@'"><?=$row->A?></td> <td style="mso-number-format:'\@'"><?=$row->B?></td> </tr> <?}?> </tbody> </table>

이런식으로 view를 만들어줍니다. (/test/down_excel.php)
td에 준 style은 엑셀이 읽어들일때 데이터 타입을 보잖아요?
럴때 그냥 숫자인데 날짜라던가 자기멋데로 해석해서 자료 망치는 것을 방치하기 위해 텍스트 값이라는 것을 설정해 준 것 입니다.

td style="mso-number-format:'\@'"

상단에 header만 들어가면 엑셀이 다운로드됩니다.
test.xls 부분은 파일명이니 필요한 형식으로 수정하시면 됩니다.

이런 기능을 할때 form을 만들어서 전송해주는게 좋은 거 같아요!
여러 방법이 있지만 제가 쓰는 방식은 이렇습니다

PHP html 엑셀 다운로드 - PHP html egsel daunlodeu

<form name="excelF" method="POST" action=""> <input type="hidden" name="eIdx" value=""/> </form> <script> $(document).ready(function(){ $('input[name="down"]').click(function(){ var eIdx = $(this).closest('tr').attr('class').replace('e_',''); $('form[name="excelF"] input[name="eIdx"]').val(eIdx); $('form[name="excelF"]').attr('action','/test/down_excel'); $('form[name="excelF"]').submit(); }) }) </script>

db값을 가져오는데 필요한 변수 날리는 form 만들어서 전송하고

public function down_excel(){ $eIdx = $this->input->post('eIdx'); $this->data['excelData'] = $this->Test_model->load_excel($eIdx); $this->load->view('/test/down_excel',$this->data); }

PHP html 엑셀 다운로드 - PHP html egsel daunlodeu

엑셀이 다운로드 된답니다!

header 세줄을 빼면 어떤 형식의 테이블인지 확인 가능하니 일일이 다운하면서 테스트 안하셔도 됩니다
테이블 모양을 먼저 확인한 다음, 완료되면
header를 추가해서 다운로드 하여 최종확인..!

#php #excel #엑셀 #엑셀다운로드 #download #엑셀파일만들기 #한글깨짐 #엑셀한글깨짐

+만약에 한글이 깨질 경우
문서 저장을 utf-8로 했는지 확인하고 그래도 깨진다면,

<meta http-equiv="Content-Type" content="application/vnd.ms-excel; charset=utf-8">

header 위쪽에 meta 태그를 추가하면 안깨집니다


프로그램/PHP

2013. 2. 7. 10:10

[PHP]엑셀(excel) 출력화면 엑셀로 다운로드 받기 

<?
if($mode == "excel"){
		## 엑셀파일 저장헤드
		$file_name = "exceltest.xls";
    header("Pragma:");
    header("Cache-Control:");
    header("Content-Type: application/vnd.ms-excel");
    header("Content-Disposition: attachment; filename=$file_name");
    header("Content-Description: PHP5 Generated Data");

}

echo "<a href='{$PHP_SELF}?mode=excel'><font color='##3300CC'>[엑셀파일 다운로드]</font></a>";

$borderwid = 1; // 엑셀 파일에서는 border가 있어야 줄이 생김
if($mode != "excel")
{
$borderwid = 0; // 화면 출력에서는 boder 0으로 
}

if($mode != "excel"){			
?>
  파일에서는 이부분 안보임
<?
}
?>
<table width='1200' border='<?=$borderwid?>' cellspacing=1 cellpadding=3 bgcolor="#5B8398">
			<tr bgcolor="65CBFF" align=center>
				<td align="center">번호</td>
				<td align="center">이름</td>
				<td align="center">전화번호</td>
			</tr>	
			<tr bgcolor="#FFFFFF" align=center>
				<td align="center">1</td>
				<td align="center">홍길동</td>
				<td align="center">010-1234-5678</td>
			</tr>	
</table>