IT/php

php 엑셀 다운로드 상세페이지 쿼리 없이 실행

조원태 2015. 11. 28. 17:13
반응형

작업을 하다 보면 상세페이지를 엑셀로 변환해야 할 경우가 있어요

 

보통 작업 하실 때 엑셀을 출력하는 페이지를 만든 후 거기서 다시 쿼리를 날린 곳 하는데요

그럴 필요 없이 상세페이지 html 을 엑셀 출력 페이지로 넘긴 후 바로 엑셀로 만들면 되요.

 

파일명 view.php

<div class="viewhtml">

<table>

<td>컬럼1</td>

<td>컬럼2</td>

<td>컬럼3</td>

<td>컬럼4</td>

</table>

</div>

<input type="button" name="excelbtn" value="엑셀다운" onClick="exceldown()">

 

<form name="excelform" id="excelform" method="post">

<textarea name="excelcontent" style="display:none"></textarea>

</form> 

 

<script>

function exceldown(){

var f = document.excelform;

f.excelcontent.value = $(".viewhtml").html();

f.action = "./exceldown.php";

f.submit();

}

</script>

 

파일명  exceldown.php

<?php

  header("Content-type:application/vnd.ms-excel; charset=UTF-8");

header( "Content-Type: application/vnd.ms-excel" ); 
header( "Content-Disposition: attachment; filename=airlee.xls" ); 
header( "Content-Description: PHP4 Generated Data" ); 
Header("Cache-Control: cache, must-revalidate"); 
header("Pragma: no-cache"); 
header("Expires: 0"); 

echo '<link href="header.css" rel="stylesheet">';

$_POST['excelcontent'] = str_replace("\\","",$_POST['excelcontent']);
$_POST['excelcontent'] = preg_replace("@<a.*?>@", "", $_POST['excelcontent'] );
die( "<div id='areabox'>".$_POST['excelcontent']."</div>" );

?>

 

php 파일에서 css가 잘 나오도록 수정만 하시면 됩니다.

반응형