<%@ page pageEncoding="euc-kr" %>
<%@ page import="java.io.*, org.apache.poi.hssf.usermodel.*" %>
<%
try {
//Excel Workbook을 하나 만듭니다. 이건 하나의 엑셀 파일에 해당
HSSFWorkbook workbook = new HSSFWorkbook();
//Shett를 만듭니다.
HSSFSheet sheet = workbook.createSheet("first sheet");
HSSFSheet sheet2 = workbook.createSheet("first sheet2");
//Font 설정.
HSSFFont font = workbook.createFont();
font.setFontName(HSSFFont.FONT_ARIAL);
//제목의 스타일 지정
HSSFCellStyle titlestyle = workbook.createCellStyle();
titlestyle.setFillPattern(HSSFCellStyle.SOLID_FOREGROUND);
titlestyle.setAlignment(HSSFCellStyle.ALIGN_CENTER);
titlestyle.setFont(font);
//sheet에 행을 하나 만듭니다.
HSSFRow row = sheet.createRow((short)0);
HSSFRow row2 = sheet2.createRow((short)0);
//행에 셀을 3개 만든 후 값 대입
HSSFCell cell1 = row.createCell((short)0);
cell1.setCellValue("제목1");
HSSFCell cell2 = row.createCell((short)1);
cell2.setCellValue("제목2");
HSSFCell cell3 = row.createCell((short)2);
cell3.setCellValue("제목3");
//내용 스타일 지정
HSSFCellStyle style = workbook.createCellStyle();
style.setFont(font);
//내용중 가운데 정렬 추가
HSSFCellStyle styleCenter = workbook.createCellStyle();
styleCenter.setAlignment(HSSFCellStyle.ALIGN_CENTER);
styleCenter.setFont(font);
for (int i=0; i<5;i++){
row = sheet.createRow((short)(i+1));
cell1 = row.createCell((short)0 );
cell1.setCellValue("내용1");
cell1.setCellStyle(styleCenter);
cell2 = row.createCell((short)1);
cell2.setCellValue("내용2");
cell2.setCellStyle(style);
cell3 = row.createCell((short)2);
cell3.setCellValue("내용3");
cell3.setCellStyle(style);
}
//행에 셀을 3개 만든 후 값 대입
HSSFCell cell4 = row2.createCell((short)0);
cell4.setCellValue("number1");
HSSFCell cell5 = row2.createCell((short)1);
cell5.setCellValue("number2");
HSSFCell cell6 = row2.createCell((short)2);
cell6.setCellValue("number3");
for (int i=0; i<5;i++){
row = sheet2.createRow((short)(i+1));
cell4 = row.createCell((short)0 );
cell4.setCellValue("내용1");
cell4.setCellStyle(styleCenter);
cell5 = row.createCell((short)1);
cell5.setCellValue("내용2");
cell5.setCellStyle(style);
cell6 = row.createCell((short)2);
cell6.setCellValue("내용3");
cell6.setCellStyle(style);
}
//엑셀 파일을 만듬
FileOutputStream fileOutput = new FileOutputStream("c:/myFirstWorkBook.xls");
workbook.write(fileOutput);
fileOutput.close();
out.println("Excel File 생성 OK");
}
catch(Exception e) {
out.println(e);
}
%>