2013년 6월 25일 화요일

html의 데이타를 excle로 저장하기 간단소스 jsp (html table excel export simple jsp source)

//Data.html-------------data what you want to make to Excel(엑셀로만들 데이타)


<script type="text/javascript">
function excel(){
document.frm.action = "excel.jsp";
document.frm.excel_data.value = document.getElementById("excel_body").outerHTML;
document.frm.submit();

}
<form name="frm" method="post">
       <input type="hidden" name="excel_data" />
</form>
<table id="excel_body">
<caption>list</caption>
<thead>
<tr>
<th>userid</th>
<th>name</th>
<th>mail</th>
</tr>
</thead>
<tbody>
<c:forEach items="${list}" var="data" varStatus="status">
<tr>
<td>
<c:out value='${data.userid}' />
</td>
<td>
<c:out value='${data.username}' />
</td>
<td>
<c:out value='${data.email}' />
</td>
</c:forEach>

</tbody>
</table>



//excel.jsp---------------------use just C&P (그냥복사해서 쓰세요)
<%
request.setCharacterEncoding("utf-8");
response.setContentType("application/vnd.ms-excel");
response.setCharacterEncoding("utf-8");
response.setHeader("Content-Disposition", "attachment; filename=\"excel.xls\"");
response.setHeader("Pragma", "no-cache");
response.setHeader("Cache-Control", "no-cache");
out.print("<meta http-equiv=\"Content-Type\" content=\"application/vnd.ms-excel; charset=utf-8\">");
%>
<html xmlns:o="urn:schemas-microsoft-com:office:office" xmlns:x="urn:schemas-microsoft-com:office:excel" xmlns="http://www.w3.org/TR/REC-html40">
<head>
<style type="text/css">
body {font-family:tahoma;font-size:12px}
table {padding:2px;border-spacing:0px;font-family:tahoma;font-size:12px;border-collapse:collapse}
td {text-align:center}
</style>
</head>
<body>
<% out.print(request.getParameter("excel_data")); %>
</body>
</html>

댓글 1개:

  1. 안녕하세요 좋은 정보 공유 감사합니다.
    혹시 해당 코드로 다중시트를 구현할수 있는 방법이 있는지 문의드립니다.
    답변부탁 드립니다.
    감사합니다.

    답글삭제