본문 바로가기
프로그래밍/JSP

jsp poi 엑셀 다운받기

by -현's- 2013. 9. 4.
반응형

 

●엑셀 다운받기 예제

<%@ page language="java" contentType="text/html; charset=EUC-KR"
    pageEncoding="EUC-KR"%>
<%@page import="
org.apache.poi.hssf.usermodel.HSSFCell,
org.apache.poi.hssf.usermodel.HSSFRow,
org.apache.poi.hssf.usermodel.HSSFSheet,
org.apache.poi.hssf.usermodel.HSSFWorkbook ,
org.apache.poi.hssf.util.Region,
java.io.FileOutputStream,
java.util.List,
org.apache.poi.hssf.usermodel.HSSFCellStyle ,
org.apache.poi.hssf.usermodel.HSSFFont,
org.apache.poi.hssf.util.HSSFColor,
java.util.*,
    java.io.*

" %>

<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=EUC-KR">
<title>엑셀 POI 테스트 완료</title>
</head>
<body>

<%

 

 

 

 

String sFileName = "파일이름" + ".xls";
sFileName = new String ( sFileName.getBytes("KSC5601"), "8859_1");


out.clear();
out = pageContext.pushBody();
response.reset();  // 이 문장이 없으면 excel 등의 파일에서 한글이 깨지는 문제 발생.

 

String strClient = request.getHeader("User-Agent");

String fileName = sFileName;

if (strClient.indexOf("MSIE 5.5") > -1) {
 //response.setContentType("application/vnd.ms-excel");
 response.setHeader("Content-Disposition", "filename=" + fileName + ";");
} else {
 response.setContentType("application/vnd.ms-excel");
 response.setHeader("Content-Disposition", "attachment; filename=" + fileName + ";");
}

OutputStream fileOut = null;

 

 

//---------------------------------------------------------------
//엑셀 처리 시작

HSSFWorkbook objWorkBook = new HSSFWorkbook();      //워크북 생성
HSSFSheet objSheet = objWorkBook.createSheet();     //워크시트 생성
HSSFRow objRow = null;        //로우 생성
HSSFCell objCell = null;       //셀 생성


//-----------------------------------------------------------------
//스타일 설정


//스타일 객체 생성
HSSFCellStyle styleHd = objWorkBook.createCellStyle();    //제목 스타일
HSSFCellStyle styleSub = objWorkBook.createCellStyle();   //부제목 스타일
HSSFCellStyle styleBody = objWorkBook.createCellStyle();   //내용 스타일


//제목 폰트
HSSFFont font = objWorkBook.createFont();
font.setFontHeightInPoints((short)15);
font.setBoldweight((short)font.BOLDWEIGHT_BOLD);

//부제목 폰트
HSSFFont font2 = objWorkBook.createFont();
font2.setFontHeightInPoints((short)11);
font2.setBoldweight((short)font.BOLDWEIGHT_BOLD);

 

//제목 스타일에 폰트 적용, 정렬
styleHd.setFont(font);
styleHd.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styleHd.setVerticalAlignment (HSSFCellStyle.VERTICAL_CENTER);

//부제목 스타일 설정

styleSub.setFont(font2);
styleSub.setBorderBottom(HSSFCellStyle.BORDER_THIN);
styleSub.setBottomBorderColor(HSSFColor.BLACK.index);
styleSub.setBorderLeft(HSSFCellStyle.BORDER_THIN);
styleSub.setLeftBorderColor(HSSFColor.BLACK.index);
styleSub.setBorderRight(HSSFCellStyle.BORDER_THIN);
styleSub.setRightBorderColor(HSSFColor.BLACK.index);
styleSub.setBorderTop(HSSFCellStyle.BORDER_THIN);
styleSub.setTopBorderColor(HSSFColor.BLACK.index);
styleSub.setAlignment (HSSFCellStyle.ALIGN_CENTER);
styleSub.setVerticalAlignment (HSSFCellStyle.VERTICAL_CENTER);
styleSub.setWrapText(true);

//내용 스타일 설정
styleBody.setBorderBottom(HSSFCellStyle.BORDER_THIN);
styleBody.setBottomBorderColor(HSSFColor.GREY_50_PERCENT.index);
styleBody.setBorderLeft(HSSFCellStyle.BORDER_THIN);
styleBody.setLeftBorderColor(HSSFColor.GREY_50_PERCENT.index);
styleBody.setBorderRight(HSSFCellStyle.BORDER_THIN);
styleBody.setRightBorderColor(HSSFColor.GREY_50_PERCENT.index);
styleBody.setBorderTop(HSSFCellStyle.BORDER_THIN);
styleBody.setTopBorderColor(HSSFColor.GREY_50_PERCENT.index);
styleBody.setAlignment (HSSFCellStyle.ALIGN_CENTER);
styleBody.setVerticalAlignment (HSSFCellStyle.VERTICAL_CENTER);


//-------------------------------------------------------

//병합
objSheet.addMergedRegion(new Region(0,(short)0,0,(short)4));
objSheet.addMergedRegion(new Region(1,(short)0,1,(short)4));

//1행
objRow = objSheet.createRow((short)0);
objRow.setHeight ((short) 0x200);

objCell = objRow.createCell((short)0);
objCell.setEncoding(HSSFCell.ENCODING_UTF_16);
objCell.setCellValue("공지사항");
objCell.setCellStyle(styleHd);

//2행
objRow = objSheet.createRow((short)1);
objRow.setHeight ((short) 0x90);

objCell = objRow.createCell((short)0);

//길이 설정
objSheet.setColumnWidth((short)0,(short)5000);
objSheet.setColumnWidth((short)1,(short)20000);
objSheet.setColumnWidth((short)2,(short)5000);
objSheet.setColumnWidth((short)3,(short)5000);
objSheet.setColumnWidth((short)4,(short)5000);

 

//-------------------------------------------
//3행
objRow = objSheet.createRow((short)2);
objRow.setHeight ((short) 0x150);

objCell = objRow.createCell((short)0);
objCell.setEncoding(HSSFCell.ENCODING_UTF_16);
objCell.setCellValue("번호");
objCell.setCellStyle(styleSub);

objCell = objRow.createCell((short)1);
objCell.setEncoding(HSSFCell.ENCODING_UTF_16);
objCell.setCellValue("제목");
objCell.setCellStyle(styleSub);

objCell = objRow.createCell((short)2);
objCell.setEncoding(HSSFCell.ENCODING_UTF_16);
objCell.setCellValue("작성자");
objCell.setCellStyle(styleSub);

objCell = objRow.createCell((short)3);
objCell.setEncoding(HSSFCell.ENCODING_UTF_16);
objCell.setCellValue("작성일");
objCell.setCellStyle(styleSub);

objCell = objRow.createCell((short)4);
objCell.setEncoding(HSSFCell.ENCODING_UTF_16);
objCell.setCellValue("조회수");
objCell.setCellStyle(styleSub);

 


out.clear();

out = pageContext.pushBody();

fileOut = response.getOutputStream();
objWorkBook.write(fileOut);
fileOut.close();


 


%>


</body>
</html>

 

 

 

 

 

 

 

반응형

'프로그래밍 > JSP' 카테고리의 다른 글

jsp poi 엑셀 다운 예제  (0) 2013.11.26
jsp 개발환경 세팅 이클립스 톰켓 플러그인  (0) 2013.10.28
jsp poi 폰트, 스타일 적용  (0) 2013.09.03
jsp poi 기본  (0) 2013.09.02
jsp 기본 문법 정리4  (0) 2012.03.12

댓글