메뉴 건너뛰기

프로그램언어

조회 수 20356 추천 수 0 댓글 0
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
?

단축키

Prev이전 문서

Next다음 문서

크게 작게 위로 아래로 댓글로 가기 인쇄
웹페이지 상의 문서를 엑셀이나, 파워포인트, 위드 문서로 변환해 줘야 되는 경우
만약에 엑셀 파일로 변환을 하고 싶으시면

PHP는
<?

header(\"Content-Type: application/vnd.ms-excel\");

?>

JSP는

<%@ page contentType="application/vnd.ms-excel; name='My_Excel'" %>
<%
response.setHeader("Content-Disposition", "inline; filename=myfile.xls");
response.setHeader("Content-Description", "JSP Generated Data");
%>

저 부분을 <HTML> 태그 앞에 넣으면 끝나지요.
저 페이지가 호출이 되면 오피스가 깔려있는 사용자들은 저장할것인지 그냥 열것인지를 물어보구여, 안깔려 있는 사용자들은 파일을 다운받을수 있게 되지요.


그럼 워드 파일은
<?
header(\"Content-Type: application/msword\");
?>

파워포인트 역시 같은 방법으로
<?
header(\"Content-Type: application/vnd.ms-powerpoint\");
?>


그럼 마지막으로 ASP에서는
<%
Response.Buffer = TRUE
Response.ContentType = \"application/vnd.ms-excel\"
%>


다음은 header 내용을 변경해서 excel로 바꾸어 주는 구문입니다.

* excel.php

<?php

header(\"Content-type: application/vnd.ms-excel\");

header(\"Content-Disposition: attachment; filename=test.xls\");

header(\"Content-Description: PHP4 Generated Data\");

?>

<html>

<body>

<table>

<tr>

<td>테스트1</td>

<td>테스트1</td>

<td>테스트1</td>

<td>테스트1</td>

</tr>

<tr>

<td>테스트2</td>

<td>테스트2</td>

<td>테스트2</td>

<td>테스트2</td>

</tr>

</table>

</body>

</html>



실행시켜보세요. 어떻게 되죠? test.xls 이름으로 excel 화일이 다운로드 되죠.

혹 DB내용을 excel형태로 출력해야될 때 유용할거 같습니다








[JSP] 쿼리결과를 엑셀로 추출
<%@ page contentType="application/vnd.ms-excel;charset=euc-kr" import="java.sql.*,java.text.*"%><%

response.setHeader("Content-Disposition", "inline; filename=myfile.xls"); // 파일 이름 지정
response.setHeader("Content-Description", "JSP Generated Data");

Connection con = null ;
Statement st = null ;
ResultSet rs = null ;


try{
con = ## 컨넥션 얻기 ##
st = con.createStatement();
rs = st.executeQuery("## 쿼리 ##");
ResultSetMetaData rsmd = rs.getMetaData();

%><html>
<body bgcolor=white>
<table border=1>
<tr bgcolor="#CACACA">
<% for ( int i = 1 ; i <= rsmd.getColumnCount() ; i ++ ) { %>

<th><%=rsmd.getColumnName(i)%></th>

<% } %>
</tr>
<%
while(rs.next()) {
%>
<tr>
<% for ( int i = 1 ; i <= rsmd.getColumnCount() ; i ++ ) { %>

<td><%

if ( rs.getString(rsmd.getColumnName(i)) == null ) {
out.print("");
} else {

out.print(rs.getString(rsmd.getColumnName(i)));
}

%></td>

<% } %>
</tr>
<% } %>
</table>
</body>
</html>
<%


} catch (Exception eee) {}
finally {
if ( rs != null ) rs.close();
if ( st != null ) st.close();
if (con != null ) con.close;
}
%>

List of Articles
번호 제목 날짜 조회 수
260 PHP error 메시지 출력 file 2015.04.14 20924
259 PHP EXCEL export시 시트 이름 지정하여 여러 시트에 데이터 쓰기 2017.03.06 18266
258 PHP http -> https 로 전환 2023.01.12 236
257 PHP http 를 https 로 전환(redirect), http->https 2019.02.19 2183
256 PHP Mcrypt 라이브러리를 활용한 암호화 시스템 2016.12.22 19972
255 PHP Notice: Use of undefined constant ... assumed ... 오류 2021.03.26 308
254 PHP SimpleHtmlDom Parser로 HTML 파싱하기 2023.01.12 220
253 PHP split()와 explode()의 차이점 2018.10.27 3536
252 PHP str_replace php 문자열치환 2023.01.12 197
251 PHP switch 문 file 2015.04.14 20993
250 php www 붙이기 2023.01.12 234
249 PHP XML 문서파싱 (SAX 방식 , DOM 방식) file 2018.10.27 3585
248 PHP ZIP 압축파일 만들기 file 2023.01.12 251
247 PHP 가변변수 $$ file 2019.02.19 1449
246 php 간단 심플한 달력만들기 file 2023.01.12 241
245 PHP 날짜 함수 2017.04.13 17673
244 PHP 날짜, 시간 관련 함수. date(), mktime() 2017.03.06 16707
243 PHP 날짜/시간 정리 2017.03.07 18629
242 php 내장함수 2017.03.07 17001
241 PHP 네이버블로그 원격 글쓰기 API 소스 file 2018.02.09 12465
Board Pagination Prev 1 2 3 4 5 6 7 8 9 10 ... 17 Next
/ 17

하단 정보를 입력할 수 있습니다

© k2s0o1d4e0s2i1g5n. All Rights Reserved