728x90
반응형

function fnExcel(){

document.listForm.action="<c:url value='/report.do'/>";

document.listForm.submit();

}

 

JAVA

@RequestMapping(value="/report.do")

public void report(

@ModelAttribute("reportVo") GameDefaultVo searchVO,

HttpServletRequest request,HttpServletResponse response,ModelMap model)

throws Exception{

List<?> resultList = null;

Calendar dateTime = Calendar.getInstance();

SimpleDateFormat formatter = new SimpleDateFormat("yyyyMMdd");

String str = formatter.format(dateTime.getTime());

 

response.reset();

response.setContentType("application/octet-stream");

response.setCharacterEncoding("utf-8");

 

if(searchVO.getSbscrbSttus().equals("300"))

{

response.setHeader("Content-disposition", "attachment; filename=" + "file"+str+".DAT");

response.setHeader("Content-Type", "text/csv; charset=EUC-KR");

service.reportselect(serchVO, response.getWriter());

}

if(searchVO.getSbscrbSttus().equals("310"))

{

response.setHeader("Content-disposition", "attachment; filename=" + "file"+str+".DAT");

service.reportselect(serchVO, response.getWriter());

}

}

 

Service

public void reportselect(Writer writer) throws Exception;

 

ServiceImpl

SuppressWarnings("unchecked")
@Service("ExtnInfReportMngService")
public  class ExtnInfReportMngServiceImpl extends AbstractServiceImpl implements ExtnInfReportMngService {

 @Resource(name="ExtnInfReportMngDao")
    private ExtnInfReportMngDao extnInfReportMngDao;
 
 /** EgovMessageSource */
 @Resource(name="egovMessageSource")
 EgovMessageSource egovMessageSource;
 
    /** EgovProgrmManageService */
 @Resource(name = "ExtnInfReportMngService")
    private ExtnInfReportMngService extnInfReportMngService;

 public void slrStlInfo(GameDefaultVo searchVO, final Writer writer) throws Exception
 {
  // extnInfReportMngDao.slrStlInfo(searchVO);
  try
        {
   extnInfReportMngDao.getSqlMapClientTemplate().queryWithRowHandler("ExtnInfReportMngDao.SlrStlInfo", searchVO, new RowHandler()
         {
          public void handleRow(Object obj)
          {
     Map<?, ?> hm = (Map<?, ?>)obj;

     try
                    {
             writer.write(strPad(String.valueOf(hm.get("ROWNUM"))     ,5,"L","0")); 
             writer.write(strPad(String.valueOf(hm.get("SLR_ID"))     ,8,"L","0"));         
             String str_drno = String.valueOf(hm.get("STL_DRNO"));
             if(str_drno.length()==6){
              str_drno = str_drno.substring(0,4)+'0'+str_drno.substring(4);
             }
             writer.write(strPad(str_drno,7,"L","0"));     
                    }
                    catch(Exception e)
                    {
                     e.printStackTrace();
                    }
    }
         });
        }
     catch(Exception e)
     {
      throw e;
     }
     IOUtils.closeQuietly(writer);
 }

 

 

JSP

<%@ page language="java" contentType="application/vnd.ms-excel; name='excel', text/html; charset=euc-kr" pageEncoding="euc-kr"%><%@ page import="java.util.*,kld.gmn.cmm.utl.fcc.service.EgovStringUtil" %><%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %><%@ taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions" %><%@ taglib prefix="fmt" uri="http://java.sun.com/jsp/jstl/fmt"%><%@ taglib prefix="spring" uri="http://www.springframework.org/tags" %><% response.setHeader("Content-Disposition", "attachment; filename="+request.getAttribute("fileName")); response.setHeader("Content-Type", "application/octet-stream;"); response.setContentType("application/octet-stream; charset=euc-kr"); 
 try { 
  List rResultList = (List)request.getAttribute("rResultList");
  String reportNo = (String)request.getAttribute("reportNo");
  for(int iList=0;iList<rResultList.size();iList++){
   HashMap hm = (HashMap)rResultList.get(iList);

}else if("310".equals(reportNo)){ //판매인정산대금정보
    response.getWriter().write(strPad(String.valueOf(hm.get("ROWNUM"))     ,5,"L","0"));        

response.getWriter().write(strPad(String.valueOf(hm.get("SLR_ID"))     ,8,"L","0")); 

response.getWriter().write(strPad(String.valueOf(hm.get("STL_DRNO"))   ,7,"L","0"));

}

if(iList < rResultList.size()-1 ) response.getWriter().write("\n");
  }
 } catch(Exception ex) { 
  System.out.print("Erro no envio do arquivo"); 
  ex.printStackTrace(); 
 }%><%!
public String strPad(String str,int len,String type,String blank){
 try {
  int totalByteLength = str.getBytes().length;
  int loop = 0;
  if(totalByteLength >= len){
   return str;
  }
  else
  {
   loop = len - totalByteLength;
  }
  
  if("L".equals(type))
  {
   for(int c=0;c<loop;c++){
    str = blank + str;
   }
  }
  else if("R".equals(type))
  {
   for(int c=0;c<loop;c++){
    str = str + blank;
   }
  }
 }
 catch (Exception e)
 {
  e.printStackTrace();
 }
 return str;
}%>

 

728x90
반응형
블로그 이미지

nineDeveloper

안녕하세요 현직 개발자 입니다 ~ 빠르게 변화하는 세상에 뒤쳐지지 않도록 우리모두 열심히 공부합시다 ~! 개발공부는 넘나 재미있는 것~!

,