프로그램/Web

웹상에서 자바 엑셀파일 다운로드

잡식성초보 2016. 3. 29. 17:53

자바 Excel 파일 다운로드를 공부하면서 구현한 소스이다.

엑셀 다운로드의 방식은 중 poi를 사용하였다. 구글링 해보니 조금은 무겁다고 하지만 다양한 기능이

지원되어있고 poi를 이용하면 Excel뿐만 아니라 다른 파일들도 구현을 할수 있다고 한다.

poi가 아닌 jxl 방법이 있는데 이 jxl의

장점은 대용량 데이터를 엑셀로 입력할때 속도가 빠르다는 점

단점은 제대로 된 엑셀 데이터가 아니라서 조작에 불편함이 있다고 한다.


            BoardData data = new BoardData();

List<BoardDto> list = data.ExcelBoard();

Workbook xls = new HSSFWorkbook();

ServletOutputStream sos = null;

try {

Sheet sheet = xls.createSheet("Excel");


sheet.setColumnWidth(3, 5000); //시트 길이조정  첫번째는 cell , 두번째는 크기

sheet.setColumnWidth(5, 5000);

sheet.setColumnWidth(7, 5000);

// CellStyle style = xls.createCellStyle();

// style.setWrapText(true);

// style.setFillBackgroundColor(HSSFColor.BLUE.index);

// style.setFillForegroundColor(HSSFColor.RED.index);

// style.setFillPattern(CellStyle.BIG_SPOTS);

Row row = null;

Cell cell = null;

row = sheet.createRow(1);

cell = row.createCell(2);

cell.setCellValue("번호");

cell = row.createCell(3);

cell.setCellValue("제목");

cell = row.createCell(4);

cell.setCellValue("글쓴이");

cell = row.createCell(5);

cell.setCellValue("날짜");

cell = row.createCell(6);

cell.setCellValue("조회수");

cell = row.createCell(7);

cell.setCellValue("IP");

for(int i = 0 ; i < list.size(); i++){

BoardDto dto = list.get(i);

row = sheet.createRow(i+2); //세로 칸

cell = row.createCell(2); //가로 칸

cell.setCellValue(dto.getWordNum());

cell = row.createCell(3);

cell.setCellValue(dto.getTitle());

cell = row.createCell(4);

cell.setCellValue(dto.getWordNick());

cell = row.createCell(5);

cell.setCellValue(dto.getWordDate());

cell = row.createCell(6);

cell.setCellValue(dto.getRec());

cell = row.createCell(7);

cell.setCellValue(dto.getIp());

}

resp.setHeader("Content-Disposition", "attachment; filename="+ URLEncoder.encode("ExcelDownload.xls", "UTF-8"));

resp.setContentType("application/vnd.ms.excel");

sos = resp.getOutputStream();

xls.write(sos);

xls.close();

sos.flush();

} catch (Exception e) {

System.out.println(e.getMessage());

} finally{

if(sos != null) sos.close();

}

반응형

'프로그램 > Web' 카테고리의 다른 글

javax.el.PropertyNotFoundException  (0) 2016.09.20
JsonMappingException error  (0) 2016.06.08
Parsing - 태그 사이 값 가져오는 파싱  (0) 2016.03.29
Parsing - <>안 속성값 가져오는 파싱  (0) 2016.03.23
자바 소수점 자르기  (0) 2015.01.22