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

poi xlsx 다운

by -현's- 2013. 12. 2.
반응형

●예제



<%@ page language="java" contentType="text/html; charset=EUC-KR"

    pageEncoding="EUC-KR"%>

    <%@ page import="java.util.*,

java.io.*,

java.util.List,

java.util.HashMap,

org.apache.poi.xssf.usermodel.XSSFCellStyle,

org.apache.poi.xssf.usermodel.XSSFFont,

org.apache.poi.xssf.usermodel.XSSFCell,

org.apache.poi.xssf.usermodel.XSSFRow,

org.apache.poi.xssf.usermodel.XSSFSheet,

org.apache.poi.xssf.usermodel.XSSFWorkbook,

org.apache.poi.xssf.usermodel.XSSFColor,

org.apache.poi.ss.util.CellRangeAddress,

org.apache.poi.ss.usermodel.Font,

org.apache.poi.ss.usermodel.IndexedColors,

com.kait.mms.common.util.*,

com.kait.ims.inven.inven.vo.InvenVo,

com.kait.ims.srch.vo.SrchVo" %>


 

<%




String sFileName = "tem.xlsx";

//String sFileName = "ItemList" + "(" + StrUtil.gfGetCurDateTime() + ")" + ".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;


//워크북 생성

XSSFWorkbook objWorkBook = new XSSFWorkbook();

//워크시트 생성

XSSFSheet objSheet = objWorkBook.createSheet();

//시트 이름

objWorkBook.setSheetName(0 , "시트명" );

//행생성

XSSFRow objRow = objSheet.createRow((short)0);

//셀 생성

XSSFCell objCell = null;

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



objRow = objSheet.createRow((short)0);



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


objCell.setCellValue("aa");




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


objCell.setCellValue("bb");


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


objCell.setCellValue("cc");


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


objCell.setCellValue("dd");


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


objCell.setCellValue("ee");


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


objCell.setCellValue("ff");


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


objCell.setCellValue("gg");


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


objCell.setCellValue("hh");









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

//길이 설정

objSheet.setColumnWidth((short)0,(short)3000);

objSheet.setColumnWidth((short)1,(short)3000);

objSheet.setColumnWidth((short)2,(short)3000);

objSheet.setColumnWidth((short)3,(short)3000);

objSheet.setColumnWidth((short)4,(short)3000);

objSheet.setColumnWidth((short)5,(short)8000);

objSheet.setColumnWidth((short)6,(short)3000);

objSheet.setColumnWidth((short)7,(short)5000);


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



fileOut = response.getOutputStream(); 

objWorkBook.write(fileOut);

fileOut.close();

%>                           








반응형

댓글