메뉴 건너뛰기

프로그램언어

조회 수 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
번호 제목 날짜 조회 수
160 PHP 소스코드 인코딩(암호화)하기 2018.07.19 6643
159 DB 연동 4단 셀렉트 박스 2018.09.28 6072
158 fcm 푸시 알림 php 테스트 2018.07.19 6044
157 쿠폰번호 발행 업데이트판. (간단한 클래스화[PHP4 기준] 등...) 2018.07.19 6025
156 PHP 파일크기 단위 붙이기 (용량 변환) file size conversion source code 2018.07.04 5793
155 키를 이용한 암호화/복호화 함수입니다. 2018.07.24 5741
154 gcm 푸시 알림 php 테스트 2018.07.19 5582
153 PHP 특정 디렉토리에 있는 파일 갯수 구하기 2018.07.19 5452
152 방금 INSERT 했던 SQL 문의 PK(primary key)값 가져오기 2018.07.04 5337
151 이미지 땡겨와서 출력하기 2018.09.28 5286
150 날짜, 시간 포맷하기 (PHP) 2018.07.04 5230
149 MySQL테이블의 내용을 엑셀파일(xls)로 다운로드 하기 2018.07.24 4798
148 게시판 내용 숨김 클릭시 내용 출력 [ 참고 ] 2018.07.24 4767
147 PHP 랜덤확률 구하기 2018.10.27 4761
146 마우스 오버시 사진변환, 파일에러시 대체이미지 적용(소스일부) 2018.07.24 4584
145 웹서버조회 소스 2018.07.24 4543
144 날짜계산 몇일까지.. [ ex)4 일전 new 표시 ] 2018.07.24 4523
143 PHP 랜덤 문자열 생성 2018.10.27 4121
142 PHP eregi가 빠를까, strpos가 빠를까? 2018.10.27 4091
141 PHP 휴대폰번호 짜르기 (preg_replace) "-" 넣기. 형식바꾸기 2018.07.04 4057
Board Pagination Prev 1 ... 5 6 7 8 9 10 11 12 13 14 ... 17 Next
/ 17

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

© k2s0o1d4e0s2i1g5n. All Rights Reserved