본문 바로가기
인터넷

ASP 엑셀파일로 저장하기

by 알 수 없는 사용자 2008. 10. 8.

웹으로 프로그램을 할 때 제일 고민이 "결과를 어떤식으로 보여줄지" 인듯 합니다.

웹으로 프로그램을 하면 언제 어디서나 인터넷만 된다면 사용할 수 있다는 장점이 있는 반면에 일반 C/S 프로그램에 비해서 결과에 대한 출력은 참 고민이 아닐수 없습니다.

참가신청에 대한 결과를 엑셀 파일로 보여준다던지 아님 특정한 값을 엑셀파일로 저장해 달라고 할 때 빈번이 사용되는 방법을 알려드릴려고 합니다. 이제부터 소개드릴 내용은 단순히 제가 사용하는 방법일 뿐입니다.

다른 뛰어난 프로그래머 분들의 더 좋은 방법도 무수히 많다는 것을 상기하시고 참고하시기 바랍니다.

저같은 경우에는 우선 결과를 저장할 엑셀 파일을 먼저 만듭니다. 아님 요청자에게 작성해 달라고 하던지 합니다.
엑셀 파일을 완성하였다면 해당 파일을 저장할 때 저장형식을 웹페이지 형식을 선택하고 저장합니다.


위와 같은 내용으로 엑셀 파일을 만들었을 경우 아래와 같이 html로 저장됩니다.
(엑셀2000기준이며, 다른 버전 사용자라면 아래 내용이 다를 수 있습니다.)

저장된 엑셀파일의 html문서를 열어보면 많이 복잡합니다. 신경쓰지 마시고 빨간색으로 된 부분만 삭제하겠습니다.
사실 삭제를 하지 않아도 전혀 무방합니다.

<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>
<meta http-equiv=Content-Type content="text/html; charset=ks_c_5601-1987">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 9">
<link rel=File-List href="./Book1.files/filelist.xml">
<link rel=Edit-Time-Data href="./Book1.files/editdata.mso">
<link rel=OLE-Object-Data href="./Book1.files/oledata.mso">
<!--[if gte mso 9]><xml>
 <o:DocumentProperties>

...[중간생략]...

</head>

<body link=blue vlink=purple class=xl24>

<table x:str border=0 cellpadding=0 cellspacing=0 width=240 style='border-collapse:
 collapse;table-layout:fixed;width:181pt'>
 <col class=xl24 width=50 style='mso-width-source:userset;mso-width-alt:2133;
 width:38pt'>
 <col class=xl24 width=100 style='mso-width-source:userset;mso-width-alt:4266;
 width:75pt'>
 <col class=xl24 width=90 style='mso-width-source:userset;mso-width-alt:3840;
 width:68pt'>
 <tr height=24 style='mso-height-source:userset;height:18.0pt'>
  <td height=24 class=xl26 width=50 style='height:18.0pt;width:38pt'>번호</td>
  <td class=xl26 width=100 style='border-left:none;width:75pt'>이름</td>
  <td class=xl26 width=90 style='border-left:none;width:68pt'>접수일자</td>
 </tr>
 <tr height=24 style='mso-height-source:userset;height:18.0pt'>
  <td height=24 class=xl25 style='height:18.0pt;border-top:none' x:num>1</td>
  <td class=xl25 style='border-top:none;border-left:none'>홍길동</td>
  <td class=xl27 style='border-top:none;border-left:none' x:num="39727">2008-10-06</td>
 </tr>
 <tr height=24 style='mso-height-source:userset;height:18.0pt'>
  <td height=24 class=xl25 style='height:18.0pt;border-top:none' x:num>2</td>
  <td class=xl25 style='border-top:none;border-left:none'>일지매</td>
  <td class=xl27 style='border-top:none;border-left:none' x:num="39728">2008-10-07</td>
 </tr>
 <![if supportMisalignedColumns]>
 <tr height=0 style='display:none'>
  <td width=50 style='width:38pt'></td>
  <td width=100 style='width:75pt'></td>
  <td width=90 style='width:68pt'></td>
 </tr>
 <![endif]>
</table>

</body>

</html>


엑셀 파일의 저장을 처리할 asp 파일을 하나 생성합니다.
예) excel_save.asp

파일의 내용은 아래와 같습니다.
실질적으로 엑셀파일로 저장을 하기 위한 부분이라고 볼 수 있습니다.

excel_save.asp 파일 내용

<%
filename = "접수결과"

Response.Buffer = TRUE
Response.ContentType = "application/vnd.ms-excel"
Response.AddHeader "Content-disposition","attachment;filename=" & filename & ".xls"
%>

<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>
<meta http-equiv=Content-Type content="text/html; charset=ks_c_5601-1987">
<meta name=ProgId content=Excel.Sheet>
<meta name=Generator content="Microsoft Excel 9">
<!--[if gte mso 9]><xml>
 <o:DocumentProperties>

...[이하생략]


이렇게만 해도 엑셀파일이 생성됩니다.
하지만 하나가 빠졌습니다. 우리가 원하는 데이터를 뿌려줘야 합니다.
위 소스에서 해당하는 부분을 추가해 보겠습니다.

원본 내용

...[이상생략]

 <tr height=24 style='mso-height-source:userset;height:18.0pt'>
  <td height=24 class=xl26 width=50 style='height:18.0pt;width:38pt'>번호</td>
  <td class=xl26 width=100 style='border-left:none;width:75pt'>이름</td>
  <td class=xl26 width=90 style='border-left:none;width:68pt'>접수일자</td>
 </tr>
 <tr height=24 style='mso-height-source:userset;height:18.0pt'>
  <td height=24 class=xl25 style='height:18.0pt;border-top:none' x:num>1</td>
  <td class=xl25 style='border-top:none;border-left:none'>홍길동</td>
  <td class=xl27 style='border-top:none;border-left:none' x:num="39727">2008-10-06</td>
 </tr>
 <tr height=24 style='mso-height-source:userset;height:18.0pt'>
  <td height=24 class=xl25 style='height:18.0pt;border-top:none' x:num>2</td>
  <td class=xl25 style='border-top:none;border-left:none'>일지매</td>
  <td class=xl27 style='border-top:none;border-left:none' x:num="39728">2008-10-07</td>
 </tr>

[이하생략]...



수정된 내용

...[이상생략]

 <tr height=24 style='mso-height-source:userset;height:18.0pt'>
  <td height=24 class=xl26 width=50 style='height:18.0pt;width:38pt'>번호</td>
  <td class=xl26 width=100 style='border-left:none;width:75pt'>이름</td>
  <td class=xl26 width=90 style='border-left:none;width:68pt'>접수일자</td>
 </tr>
<%
 Do While Not Rs.EOF
   cnt = cnt + 1

   name = Rs("name")
   date = Rs("date")
%>
 <tr height=24 style='mso-height-source:userset;height:18.0pt'>
  <td height=24 class=xl25 style='height:18.0pt;border-top:none' x:num><%=cnt%></td>
  <td class=xl25 style='border-top:none;border-left:none'><%=name%></td>
  <td class=xl27 style='border-top:none;border-left:none' x:num="39727"><%=date%></td>
 </tr>
<%
  Rs.MoveNext
 Loop
%>


[이하생략]...



데이터베이스 연결은 각자에게 맡기고 이렇게만 하신다면 원하는 결과를 얻으실 수 있습니다.