php에서 Table을 엑셀(excel)파일로 다운하기/한글 깨짐
먼저 php에서 db자료를 훑어서 필요한 자료를 뽑아내서 view로 보냅니다 <? 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="mso-number-format:'\@'" 상단에 header만 들어가면 엑셀이 다운로드됩니다. 이런 기능을 할때 form을 만들어서 전송해주는게 좋은 거 같아요!
<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); }
엑셀이 다운로드 된답니다! header 세줄을 빼면 어떤 형식의 테이블인지 확인 가능하니 일일이 다운하면서 테스트 안하셔도 됩니다 #php #excel #엑셀 #엑셀다운로드 #download #엑셀파일만들기 #한글깨짐
#엑셀한글깨짐 <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> |