●poi 폰트, 스타일 적용 예제
<%@ page language="java" contentType="text/html; charset=EUC-KR"
pageEncoding="EUC-KR"%>
<%@ page import="org.apache.poi.* " %>
<%@page import="org.apache.poi.hssf.usermodel.HSSFSheet,
org.apache.poi.hssf.usermodel.HSSFWorkbook,
org.apache.poi.hssf.usermodel.*,
org.apache.poi.hssf.*,
java.io.FileOutputStream,
org.apache.poi.hssf.util.Region,
org.apache.poi.hssf.util.HSSFColor
" %>
<!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>
<%
//--------------------------------------------------------------------
//엑셀 처리 시작
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);
//제목 스타일에 폰트 적용, 정렬
styleHd.setFont(font);
styleHd.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styleHd.setVerticalAlignment (HSSFCellStyle.VERTICAL_CENTER);
//부제목 스타일 설정
styleSub.setBorderBottom(HSSFCellStyle.BORDER_THIN);
styleSub.setBottomBorderColor(HSSFColor.BROWN.index);
styleSub.setBorderLeft(HSSFCellStyle.BORDER_THIN);
styleSub.setLeftBorderColor(HSSFColor.BROWN.index);
styleSub.setBorderRight(HSSFCellStyle.BORDER_THIN);
styleSub.setRightBorderColor(HSSFColor.BROWN.index);
styleSub.setBorderTop(HSSFCellStyle.BORDER_THIN);
styleSub.setTopBorderColor(HSSFColor.BROWN.index);
styleSub.setAlignment (HSSFCellStyle.ALIGN_CENTER);
styleSub.setVerticalAlignment (HSSFCellStyle.VERTICAL_CENTER);
styleSub.setWrapText(true);
//내용 스타일 설정
styleBody.setAlignment (HSSFCellStyle.ALIGN_LEFT);
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);
//--------------------------------------------
//내용
//-----------------------------------------------------
//파일 닫기
//objWorkBook.write(response.getOutputStream());
FileOutputStream fs = new FileOutputStream("d:/aa.xls");
objWorkBook.write(fs);
fs.close();
out.println("엑셀 생성 완료");
//fs.close();
%>
dddddddddd
</body>
</html>
'프로그래밍 > JSP' 카테고리의 다른 글
jsp 개발환경 세팅 이클립스 톰켓 플러그인 (0) | 2013.10.28 |
---|---|
jsp poi 엑셀 다운받기 (0) | 2013.09.04 |
jsp poi 기본 (0) | 2013.09.02 |
jsp 기본 문법 정리4 (0) | 2012.03.12 |
jsp 기본 문법 정리3 (0) | 2012.03.12 |
댓글