<dependency>
<groupId>net.sf.jxls</groupId>
<artifactId>jxls-core</artifactId>
<version>1.0.6</version>
</dependency>
<dependency>
<groupId>org.apache.poi</groupId>
<artifactId>openxml4j</artifactId>
<version>1.0-beta</version>
</dependency>
- 의존성 명시
private InputStream readTemplate(String finalTemplate) throws FileNotFoundException {
String CONTEXT_PATH = getServletContext().getRealPath("");
String templateFilePath = CONTEXT_PATH+ File.separator + excelPath + File.separator + finalTemplate;
return new FileInputStream(templateFilePath);
}
@Override
@SuppressWarnings("rawtypes")
protected void renderMergedOutputModel(Map<String, Object> model, HttpServletRequest request, HttpServletResponse response) throws IOException, InvocationTargetException, SQLException, Exception, InvalidFormatException {
XLSTransformer xls = new XLSTransformer();
Configuration cf = xls.getConfiguration();
cf.setUTF16(true);
xls.setConfiguration(cf);
InputStream is = null;
try {
is = readTemplate((String)model.get("source") );
Map<String, Object> beans = new HashMap<String, Object>();
SimpleDateFormat df = new SimpleDateFormat("yyyy.MM.dd HH:mm");
beans.put("date", df.format( new Date() ) );
beans.put("now", new Date() );
Set key = model.keySet();
for (Iterator iterator = key.iterator(); iterator.hasNext();) {
String keyName = (String) iterator.next();
beans.put( keyName, model.get(keyName) );
if( keyName.endsWith("List") ) {
List list = (List)model.get(keyName);
if( list.size() == 0 ) {
String str = "<script>alert('목록이 존재하지 않습니다.'); history.back(-1);</script>";
response.setContentType("text/html; charset=utf-8");
response.getOutputStream().write(str.getBytes());
return;
}
}
}
writeWorkbook((String)model.get("target") + "_" + sf.format(new Date() ), response, xls.transformXLS(is, beans) );
}finally {
if( is != null ) {
try {
is.close();
}catch (IOException e) {
}
}
}
}
- InputStream & OutputStream 객체를 사용
@SuppressWarnings("unchecked")
@RequestMapping(folderPath + "excelDown.do")
public ModelAndView excelDown(@ModelAttribute("searchVO") VO vo) throws Exception{
ModelAndView mav = new ModelAndView(excelView);
String tit = "제목";
String url = "/excel.xlsx";
List<VO> resultList = (List<VO>) cmmnService.selectList(vo, PROGRAM_ID+".excelDownSelectList" );
mav.addObject("target", tit);
mav.addObject("source", url);
if(resultList.size() > 0){
mav.addObject("result", resultList);
}
return mav;
}